1. Hey! Guest! The 35th GMC Jam will take place between November 28th, 12:00 UTC - December 2nd, 12:00 UTC. Why not join in! Click here to find out more!
    Dismiss Notice

GMS 2 Making a dynamic laser

Discussion in 'Programming' started by RobotoSkunk, Dec 29, 2018.

  1. RobotoSkunk

    RobotoSkunk Member

    Joined:
    Dec 24, 2018
    Posts:
    41
    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)
     
  2. FrostyCat

    FrostyCat Member

    Joined:
    Jun 26, 2016
    Posts:
    4,612
    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.
     
  3. RobotoSkunk

    RobotoSkunk Member

    Joined:
    Dec 24, 2018
    Posts:
    41
    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
     
  4. TsukaYuriko

    TsukaYuriko Q&A Spawn Camper Forum Staff Moderator

    Joined:
    Apr 21, 2016
    Posts:
    1,712
    Since you have not yet granted us remote access to your system, please post the full error message.
     
  5. RobotoSkunk

    RobotoSkunk Member

    Joined:
    Dec 24, 2018
    Posts:
    41
    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
     
  6. TsukaYuriko

    TsukaYuriko Q&A Spawn Camper Forum Staff Moderator

    Joined:
    Apr 21, 2016
    Posts:
    1,712
    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.
     
    Relic likes this.

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice