Legacy GM Need to attach projectile to weapon [SOLVED]

pixeltroid

Member
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!
 

Slyddar

Member
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.
 

pixeltroid

Member
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.
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:

TheouAegis

Member
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.
 

Slyddar

Member
should go in the End Step event, but you need to change it, because TheSly's code there is incomplete.
Thanks man, was running out the door and didn't have time to double check it all. Appreciate you fixing it up.
the projectile shoots in the wrong direction when I'm facing left.
Nevermind...I made it work.
I just removed
Code:
l.direction = _dir;
o_O
What was the issue?
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;
 

pixeltroid

Member
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;

ah ok! Thanks to you and TheouAegis !
 
Top