1. Hello Guest! It's with a heavy heart that we must announce the removal of the Legacy GMC Archive. If you wish to save anything from it, now's the time! Please see this topic for more information.
    Dismiss Notice

GM:S 1.4 Need to attach projectile to weapon [SOLVED]

Discussion in 'Programming' started by pixeltroid, Feb 24, 2019.

  1. pixeltroid

    pixeltroid Member

    Joined:
    Jul 23, 2016
    Posts:
    669
    I have a lighting bolt weapon that is used when the attack button is pressed. It emerges from my players weapon barrel and lasts for 2 seconds before disintegrating.

    The problem is that if I turn away the lighting projectile just stays suspended in mid air -- as shown below -- which i think looks stupid

    flame.jpg



    Heres the code in my shoot button that activates the weapon script:

    Code:
    if (state == 'move' && global.flame > 0 && health > 0 && global.weapon = 2 && !alarm[5] > 0)
    {
    {
    scr_playerlightning_state();
    image_speed = .5;
    sprite_index = spr_playershoot2;
    state = 'playerlightning';
    }
    canlight = false
    alarm[5]=30
    } 
    Heres the code that controls how the weapon is used:

    Code:
    //create lightning
    
    if ducking = true {
        if (image_xscale > 0) {
        l=instance_create(x+16,y,obj_lightning);
        l.direction = 0;
        l.image_angle = l.direction
        l.speed = 0;
        }
        else {
        l=instance_create(x-16,y,obj_lightning)
        l.direction = 180;  
        l.speed = 0;
        l.image_angle = l.direction
        }
        }
      
    else if (image_xscale > 0) {
    l=instance_create(x+16,y-9,obj_lightning);
    l.direction = 0;
    l.speed = 0;
    l.image_angle = l.direction
    } else if (image_xscale < 0){
    l=instance_create(x-16,y-9,obj_lightning)
    l.direction = 180;  
    l.speed = 0;
    l.image_angle = l.direction
    }
    
    global.lightning  -= 1;
    
    
    How do I make it so that the lightning projectile is attached to the player at the co-ordinates specified in the code for as long as its supposed to last.


    Any help would be greatly appreciated!
     
  2. pixeltroid

    pixeltroid Member

    Joined:
    Jul 23, 2016
    Posts:
    669
  3. TheSly

    TheSly Member

    Joined:
    Jan 16, 2017
    Posts:
    1,000
    Firstly, you can simplify your calling code to just this:
    Code:
    if ducking var yy = 0 else yy = -9;
    if (image_xscale > 0) var _dir = 0 else var dir = 180;
    
    var l = instance_create(x + 16*image_xscale, y+yy, obj_lightning);
    l.direction = _dir;
    l.image_angle = l.direction
    l.speed = 0;
    
    //store variables for following
    l.owner = id;
    l.x_offset = 16*image_xscale;
    l.y_offset = yy
    
    Set up the appropriate variables you need and then call them in a single instance create, where we also create a temp variable for l, unless you need it after this step.

    Now to ensure you can have it follow, you can store the id of the player making it, and the offset variables, xx and yy. Now in obj_lightning just update the step event to follow the owner at the offset values:
    Code:
    if instance_exists(owner) {
      x = owner.x + x_offset;
      y = owner.y + y_offset;
    }
    
    You may need to create the variables for owner and offset in the obj_lightning object. Set owner = noone;

    I haven't tested all that, but it should get you started at least.
     
  4. pixeltroid

    pixeltroid Member

    Joined:
    Jul 23, 2016
    Posts:
    669
    Hi. Thanks.

    Your code worked....a bit.

    But when I turn left and use the weapon I got this error:

    Code:
    FATAL ERROR in
    action number 3
    of Key Press Event for P-key Key
    for object obj_player:
    
    local variable _dir(100002, -2147483648) not set before reading it.
     at gml_Script_scr_playerflame_state (line 5) - l.direction = _dir;
    ############################################################################################
    --------------------------------------------------------------------------------------------
    stack frame is
    gml_Script_scr_playerlightning_state (line 5)
    called from - gml_Object_obj_player_KeyPressed_P_3 (line 91) - scr_playerlightning_state();
    
    Whats wrong? What do I do?
     
    Last edited: Feb 27, 2019
  5. TheouAegis

    TheouAegis Member

    Joined:
    Jul 3, 2016
    Posts:
    7,123
    First off,
    Code:
    if instance_exists(owner) {
      x = owner.x + x_offset;
      y = owner.y + y_offset;
    }
    should go in the End Step event, but you need to change it, because TheSly's code there is incomplete.
    Code:
    if instance_exists(owner) {
        image_xscale = owner.image_xscale;
        x_offset = 16 * image_xscale;
        x = owner.x + x_offset;
        y = owner.y + y_offset;
    }
    
    In his main block of code, change the first part to:
    Code:
    if ducking
        var yy = 0
    else
        var yy = -9;
    if image_xscale > 0
        var _dir = 0
    else
        var _dir = 180;
    
    He had two typos there which were easy to miss.
     
  6. pixeltroid

    pixeltroid Member

    Joined:
    Jul 23, 2016
    Posts:
    669
    @ TheouAegis

    It worked ....but theres one issue...

    the projectile shoots in the wrong direction when I'm facing left.

    prob1.jpg
     
  7. pixeltroid

    pixeltroid Member

    Joined:
    Jul 23, 2016
    Posts:
    669
    Nevermind...I made it work.

    I just removed

    Code:
    l.direction = _dir;
    o_O

    What was the issue?
     
  8. TheSly

    TheSly Member

    Joined:
    Jan 16, 2017
    Posts:
    1,000
    Thanks man, was running out the door and didn't have time to double check it all. Appreciate you fixing it up.
    When TheouAegis correctly added this line it made the assignment of _dir obsolete, as you are no longer getting the direction from the original direction, but updating it each step from the players image_xscale.
    Code:
    image_xscale = owner.image_xscale;
    
     
  9. pixeltroid

    pixeltroid Member

    Joined:
    Jul 23, 2016
    Posts:
    669

    ah ok! Thanks to you and TheouAegis !
     

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