GMS 2 Making a dynamic laser

RobotoSkunk

Member
Hello, I try to create a beam that suits all types of surface in Game Maker Studio 2, so far I have the following code:
Code:
while(!collision_line(x, y, x+lengthdir_x(long, image_angle), y+lengthdir_y(long, image_angle), obj_block, 1, 0)){
    long++;
    if (long > room_width+room_height) then break;
}
The problem with this is that the laser does not decrease its length when an object is superimposed on the laser. (Sorry if I have grammatical errors, I don't speak English)
 

FrostyCat

Member
You should recalculate from scratch every step if you want it to account for new unexpected obstacles.

And if you really want to save time, you should be opting for a logarithmic-time algorithm, not by trying to take bad shortcuts with a linear-time setup.
 

RobotoSkunk

Member
You should recalculate from scratch every step if you want it to account for new unexpected obstacles.

And if you really want to save time, you should be opting for a logarithmic-time algorithm, not by trying to take bad shortcuts with a linear-time setup.
I also opted for that option, but the problem is that the laser before detecting the player draws a dotted line animated with the following code:
Code:
if (image_index != image_number-1){
    draw_set_alpha(0.5);
    draw_line_width(x+lengthdir_x(20, image_angle), y+lengthdir_y(20, image_angle), x+lengthdir_x(long, image_angle), y+lengthdir_y(long, image_angle), image_xscale);
    draw_set_alpha(1);
} else if (!detected and image_index = image_number-1){
    a = lengthdir_x(6, image_angle);
    b = lengthdir_y(1, image_angle);

    for(var i = 0; i < ((long/3)/image_xscale)-3; i++)
    if (!(i & 1)){
        draw_sprite_ext(spr_pointed_line, s, x+(a*(i/2+1.5))*image_xscale, y+(b*(i*3+10))*image_yscale, image_xscale, image_yscale, image_angle, image_blend, image_alpha);
    }
    
} else if (detected and image_index = image_number-1){
    draw_set_alpha(submg);
    draw_line_width(x+lengthdir_x(20, image_angle), y+lengthdir_y(20, image_angle), x+lengthdir_x(long, image_angle), y+lengthdir_y(long, image_angle), image_xscale);
    draw_set_alpha(1);
}
After that Game Maker detects me an error
 

TsukaYuriko

šŸŒ 
Forum Staff
Moderator
Since you have not yet granted us remote access to your system, please post the full error message.
 

RobotoSkunk

Member
Since you have not yet granted us remote access to your system, please post the full error message.
How?

The object works like this:
Create event:
Code:
/// @description Valores iniciales por defecto
depth = 1;
image_speed = 0;
image_index = image_number-1;
clock = 0;
submg = 0;
long = 0;
size = 0;
detected = false;
s = 0;
bb = 0;
alarm[0] = 1;
Step event:
Code:
/// @description Procesador
//Pausa
if (pause){
    submg = 1;
    image_index = imag;
    exit;
}
imag = image_index;
//Longitud calculada
while(!collision_line(x, y, x+lengthdir_x(long, image_angle), y+lengthdir_y(long, image_angle), obj_block, 1, 0)){
    long++;
    if (long > room_width+room_height) then break;
}

//Detectar jugador
player_detect = collision_line(x, y, x+lengthdir_x(long, image_angle), y+lengthdir_y(long, image_angle), obj_player, 1, 0);
if (player_detect and image_index = image_number-1){
    detected = true;
}

//Crear laser
if (detected and image_index = image_number-1){
    clock++;
} if (clock > 30){
    image_speed = 0.5;
    image_index = 0;
    new_inst = instance_create(x+lengthdir_x(20*image_xscale, image_angle), y+lengthdir_y(20*image_xscale, image_angle), obj_laser);
    new_inst.image_angle = image_angle;
    new_inst.image_xscale = long/14;
    new_inst.image_yscale = 2*image_yscale;
    new_inst.sp = -0.2*image_yscale;
    //Detener
    clock = 0;
    detected = 0;
}

//Atascar sprite
if (image_index = image_number-1){
    image_speed = 0;
    image_index = image_number-1;
}

//Otro
if (s < 5){
    s += 0.5;
} else s = 0;
Draw event:
Code:
/// @description Debug y otras cosas
//draw_self();
//Advertencias
if (detected and image_index = image_number-1){
    draw_sprite_ext(spr_laser_gun_line, 0, x, y, 1, 1, image_angle, -1, submg);
}
if (image_index != image_number-1){
    draw_set_alpha(0.5);
    draw_line_width(x+lengthdir_x(20, image_angle), y+lengthdir_y(20, image_angle), x+lengthdir_x(long, image_angle), y+lengthdir_y(long, image_angle), image_xscale);
    draw_set_alpha(1);
} else if (!detected and image_index = image_number-1){
    a = lengthdir_x(6, image_angle);
    b = lengthdir_y(1, image_angle);

    for(var i = 0; i < ((long/3)/image_xscale)-3; i++)
    if (!(i & 1)){
        draw_sprite_ext(spr_pointed_line, s, x+(a*(i/2+1.5))*image_xscale, y+(b*(i*3+10))*image_yscale, image_xscale, image_yscale, image_angle, image_blend, image_alpha);
    }
   
} else if (detected and image_index = image_number-1){
    draw_set_alpha(submg);
    draw_line_width(x+lengthdir_x(20, image_angle), y+lengthdir_y(20, image_angle), x+lengthdir_x(long, image_angle), y+lengthdir_y(long, image_angle), image_xscale);
    draw_set_alpha(1);
}

//IlusiĆ³n
draw_sprite_ext(spr_laser_gun, image_index, x, y, image_xscale, image_yscale, image_angle, image_blend, image_alpha);

//Debug
if (debug){
    draw_set_color(c_fuchsia);
    draw_line_width(x, y, x+lengthdir_x(long, image_angle), y+lengthdir_y(long, image_angle), image_xscale);
    draw_set_color(-1);
}
I donĀ“t know how fix it
 

TsukaYuriko

šŸŒ 
Forum Staff
Moderator
In order to post an error message, first select the entire error message's text. You can do this by either placing the caret into the text field containing the error message, pressing and holding the Control (or CTRL) key on your keyboard and then pressing the A key, or by placing your mouse cursor at the start of the error message, then clicking and holding your left mouse button while dragging the mouse cursor to the end of the error message. You might also be able to perform a right click using your mouse and then selecting the "Select All" option from the drop-down menu that pops up. You can then copy the message either by holding the Control key and pressing C, or by right-clicking inside the error message text field and selecting Copy.

In the case of error messages being presented to you in a standard window rather than in a text field in a window, you can often focus the error message window by clicking on it and then holding Control and pressing C.

You can then paste the error message into a reply by selecting the reply text field and either holding Control and pressing V, or by right-clicking inside the reply text field and selecting Paste. Continue writing your reply as usual.

You may also be unable to copy the error message at all. In this case, rewriting the error message manually is usually considered the best practice to preserve its widely striven for text format.

Let me know if you require any further instructions.
 
Top