Platformer: Weapon Mask Follows Player; The Sprite Does Not

E

EAT_DIRT

Guest
I am working on a Multiplayer platform game that utilizes various choices of weapons and characters. When the player attacks, the weapon mask creates itself and follows the players movement. However, when I draw the sprite of the actual weapon it stays in the same spot from when the action was executed.

The Mask's Creation Event

Code:
////Creation Event

sprite_index =obj_player_self.weaponA_var

The Mask's Draw Event

Code:
////DRAW EVENT

self.x = other.id.x
self.y = other.id.y
draw_sprite(sprite_index,image_index,x,y)

image_xscale = other.image_xscale;
image_speed=other.image_speed

If I were to remove the "sprite_index" from the creation event; the mask is drawn and it follows the player like I mentioned above. If I try to add a sprite to the mask's index, it does not work. Or, if "obj_player_self" is changed to "other.id", it wont recognize the "weaponA_var" variable and I will get a FATAL ERROR saying "not set before reading it"

Also, if multiple players(player1 and player2) were in the same room and I change "other.id" to "obj_player_self" in the draw event; it will only spawn next to player 1.








////////////PARENT OBJECTS FOR REFFERENCE


OBJ_PLAYER_1 < weaponA_var = spr_weaponA
OBJ_PLAYER_2 < weaponA_var = spr_weaponA
OBJ_PLAYER_SELF////PARENT OBJECT

////////////////////////////////////////////
OBJ_WEAPON_MASK

OBJ_PLAYER_HITBOX_PARENT //////// PARENT OBJECT
 

TheouAegis

Member
other has no value.

Creation Event
sprite_index = obj_player_self.weaponA_var;
shooter = obj_player_self;

End Step Event:
x = shooter.x;
y = shooter.y;
image_xscale = shooter.image_xscale;
image_speed = shooter.image_speed;
 
E

EAT_DIRT

Guest
other has no value.

Creation Event
sprite_index = obj_player_self.weaponA_var;
shooter = obj_player_self;

End Step Event:
x = shooter.x;
y = shooter.y;
image_xscale = shooter.image_xscale;
image_speed = shooter.image_speed;

Almost got it, the weapon follows the player. Now, The image_speed is going slower and is not matching with the player sprite. Also, Player 2 seems to be spawning the weapon to Player 1. I'm guessing it's because there are 2 obj_player_self in the room but since Player 1 is the first of that parent in the room its only spawing to it; how do I confirm the correct ID?
 

TheouAegis

Member
To handle multiplayer, you need the player to assign the variables. But since your player can change direction at any time.

In the Create Event of the player:
Code:
my_gun = noone;
When the player creates the gun:
Code:
my_gun = instance_create(x,y,weaponA)   //or whatever
my_gun.image_xscale = image_xscale;
my_gun.image_speed = image_speed;
In the End Step Event of the player:
Code:
with my_gun {
x = other.x;
y = other.y;
image_xscale = other.image_xscale;
image_speed = other.image_speed;
As for the animation being too slow or whatever, are you using Studio 2? image_speed is handled differently there I guess, so you need to make sure your gun sprite and player sprite have the correct animation speed settings (in the sprite resources).
 
E

EAT_DIRT

Guest
Player 1 and 2 are parents of OBJ_PLAYER_SELF. Would I have to make a separate "my_gun" variable for each player? I am using Studio 1
 
E

EAT_DIRT

Guest
To handle multiplayer, you need the player to assign the variables. But since your player can change direction at any time.

In the Create Event of the player:
Code:
my_gun = noone;
When the player creates the gun:
Code:
my_gun = instance_create(x,y,weaponA)   //or whatever
my_gun.image_xscale = image_xscale;
my_gun.image_speed = image_speed;
In the End Step Event of the player:
Code:
with my_gun {
x = other.x;
y = other.y;
image_xscale = other.image_xscale;
image_speed = other.image_speed;
As for the animation being too slow or whatever, are you using Studio 2? image_speed is handled differently there I guess, so you need to make sure your gun sprite and player sprite have the correct animation speed settings (in the sprite resources).
Player 1 and 2 are parents of OBJ_PLAYER_SELF. Would I have to make a separate "my_gun" variable for each player? I am using Studio 1

Sorry for the bump, I am not sure if I properly sent the last reply.
 
E

EAT_DIRT

Guest
To handle multiplayer, you need the player to assign the variables. But since your player can change direction at any time.

In the Create Event of the player:
Code:
my_gun = noone;
When the player creates the gun:
Code:
my_gun = instance_create(x,y,weaponA)   //or whatever
my_gun.image_xscale = image_xscale;
my_gun.image_speed = image_speed;
In the End Step Event of the player:
Code:
with my_gun {
x = other.x;
y = other.y;
image_xscale = other.image_xscale;
image_speed = other.image_speed;
As for the animation being too slow or whatever, are you using Studio 2? image_speed is handled differently there I guess, so you need to make sure your gun sprite and player sprite have the correct animation speed settings (in the sprite resources).
Alright, I allowed the object to create it's own weapon sprite. Now, when I assign it to the specific object it does not follow the player. (EX: The player jumps in the air and performs an attack, the weapon animation plays but stays stagnant in the air away from the player.)
 

TheouAegis

Member
Did you put the code I labeled "In the End Step Event of the player" inside the player object's END STEP event?

Where is your jumping code located?

Is the gun following the player when walking left or right?

If you are using physics, then you'd need to change x and y to phy_position_x and phy_position_y.
 

TheouAegis

Member
Player 1 and 2 are parents of OBJ_PLAYER_SELF. Would I have to make a separate "my_gun" variable for each player? I am using Studio 1

Sorry for the bump, I am not sure if I properly sent the last reply.
Didn't see this post when I wrote the other reply. lol

All your code could be in OBJ_PLAYER_SELF if you wanted, or whatever. But each instance of your players need their own gun variable.
 
E

EAT_DIRT

Guest
Didn't see this post when I wrote the other reply. lol

All your code could be in OBJ_PLAYER_SELF if you wanted, or whatever. But each instance of your players need their own gun variable.

Lol no worries I thought my initial response didn't go through. Anyway, I am not using physics and the weapon only appears when the player attacks. I did follow your instructions about EndStep and it worked(by default). Once I tried to incorporate a second player using a different sprite index for the weapon mask object it would only stick to player1 even if player2 was performing the attack. I use state scripts
 

TheouAegis

Member
If it's sticking to player 1 inside of player 2, then it sounds like you're using object referencing in the gun. Make sure your gun doesn't have any code moving it, still. Remember, all the code should be in the player objects so that they're manipulating their own guns. Also remember each player should only have the id of the gun that player created (which is what instance_create() returns).
 
E

EAT_DIRT

Guest
If it's sticking to player 1 inside of player 2, then it sounds like you're using object referencing in the gun. Make sure your gun doesn't have any code moving it, still. Remember, all the code should be in the player objects so that they're manipulating their own guns. Also remember each player should only have the id of the gun that player created (which is what instance_create() returns).
I think I understand now, the code you provided after I mentioned multiple players is old and should be erased? I have code in my weapon parent obj and obj_player_self.
 
E

EAT_DIRT

Guest
I think I understand now, the code you provided after I mentioned multiple players is old and should be erased? I have code in my weapon parent obj and obj_player_self.
Alright I removed the movement code from the weapon object; and now the players are spawing their own weapon objects. However, they are STILL not following the player. I have no idea how I keep running into this issue. I placed the "x = other.x in the end step event for OBJ_PLAYER_SELF and I'm still not getting results.

Code:
////END STEP EVENT FOR OBJ_PLAYER_SELF

event_inherited()

with Weapon_var{
x = other.x;
y = other.y;
image_xscale = other.image_xscale;
image_speed = other.image_speed;

}
 

TheouAegis

Member
Yeah. So you also wouldn't want the sprite being set in the gun either. Pretty much no code should be in the gun at all. Make obj_player_self handle everything, pretty much.

So you set Weapon_var like

Weapon_var = instance_create(x,y, WeaponA_var);

or something along those lines?
 
E

EAT_DIRT

Guest
Yeah. So you also wouldn't want the sprite being set in the gun either. Pretty much no code should be in the gun at all. Make obj_player_self handle everything, pretty much.

So you set Weapon_var like

Weapon_var = instance_create(x,y, WeaponA_var);

or something along those lines?
Fixed it, the weapon was not following the player because it was placed in the ENDSTEP not STEP.
 
E

EAT_DIRT

Guest
Thank you for the support, I can now continue making my game; what a pain in the ass lol. Would it be possible if I include you in my credits?
 
Top