• Hey Guest! Ever feel like entering a Game Jam, but the time limit is always too much pressure? We get it... You lead a hectic life and dedicating 3 whole days to make a game just doesn't work for you! So, why not enter the GMC SLOW JAM? Take your time! Kick back and make your game over 4 months! Interested? Then just click here!

GML Help with changing weapons

W

Wicked

Guest
So basically, the code at the top of both spoilers is supposed to have it so when I pick up one weapon, the other is dropped to the floor. However, it only works if I have the gun and try to pick up the axe, if I have the axe and try to pick up the gun it just moves the gun.
Axe Code
if(instance_exists(obj_GunReal))
{
instance_destroy();
instance_create_layer(obj_Player.x,obj_Player.y, "GunLayer", obj_Axe);
}
x=obj_Player.x+5;
y=obj_Player.y-9;
image_angle = point_direction(x,y, mouse_x, mouse_y);
firingdelay = firingdelay - 1;
recoil = max(0,recoil - 1);
if (mouse_check_button(mb_left)) && (firingdelay <0)
{
recoil = 10;
firingdelay = 25;
with(instance_create_layer(x,y,"Bullets",obj_MeleeSwing))
{
speed = 5;
direction = other.image_angle + random_range (-3, 3);
image_angle = direction;
}
if (global.Nails = 1){
recoil = 5;
firingdelay = 11;
repeat (4){
with(instance_create_layer(x,y,"Bullets",obj_Nailjectile))
{
speed = 15
direction = other.image_angle + random_range(-9, 9);
image_angle = direction;
}
}
}
x = x - lengthdir_x(recoil, image_angle);
y = y - lengthdir_y(recoil, image_angle);
if (image_angle > 90) && (image_angle < 270)
{
image_yscale = -1;
}
else
{
image_yscale = 1;
}
}
Gun code
if(instance_exists(obj_AxeHand))
{
instance_destroy();
instance_create_layer(obj_Player.x,obj_Player.y, "GunLayer", obj_GunSprite);
}
x=obj_Player.x+5;
y=obj_Player.y-9;
image_angle = point_direction(x,y, mouse_x, mouse_y);
firingdelay = firingdelay - 1;
recoil = max(0,recoil - 1);
if (mouse_check_button(mb_left)) && (firingdelay <0)
{
recoil = 4;
firingdelay = 10;
with(instance_create_layer(x,y,"Bullets",obj_Bullet))
{
speed = 15;
direction = other.image_angle + random_range (-3, 3);
image_angle = direction;
}
if (global.Nails = 1)
{
recoil = 5;
firingdelay = 11;
repeat (4){
with(instance_create_layer(x,y,"Bullets",obj_Nailjectile))
{
speed = 10
direction = other.image_angle + random_range(-150, 150);
image_angle = direction;
}
}
}


}
x = x - lengthdir_x(recoil, image_angle);
y = y - lengthdir_y(recoil, image_angle);
if (image_angle > 90) && (image_angle < 270)
{
image_yscale = -1;
}
else
{
image_yscale = 1;
}
Pickup code
if (distance_to_object(obj_GunSprite) < 32 ){
instance_create_layer(obj_GunSprite.x, obj_GunSprite.y, "GunLayer", obj_GunReal);
}
if (distance_to_object(obj_Axe) < 32 ){
instance_create_layer(obj_Axe.x, obj_Axe.y, "GunLayer", obj_AxeHand);
}
 

FrostyCat

Redemption Seeker
You have a problem with yin-yang if blocks in your pickup code. You picked up a gun and that creates the axe on the ground, then that newly dropped axe gets picked up next and that recreates the gun on the ground. Put an else before your second if to make it an if-elseif ladder. That way at most only one block executes, even if it enables the condition for other ones down the line.

Also, your current system of having the axe care about the gun (and vice versa) is awful architecture. Every time you add a new weapon, every other weapon has to be changed to account for when the new weapon is put down. The player should be the one caring about dropping the existing weapon, not the pickups. You should re-engineer this part as soon as you can.
 
W

Wicked

Guest
You have a problem with yin-yang if blocks in your pickup code. You picked up a gun and that creates the axe on the ground, then that newly dropped axe gets picked up next and that recreates the gun on the ground. Put an else before your second if to make it an if-elseif ladder. That way at most only one block executes, even if it enables the condition for other ones down the line.

Also, your current system of having the axe care about the gun (and vice versa) is awful architecture. Every time you add a new weapon, every other weapon has to be changed to account for when the new weapon is put down. The player should be the one caring about dropping the existing weapon, not the pickups. You should re-engineer this part as soon as you can.
Alright, thank you!
 
W

Wicked

Guest
You have a problem with yin-yang if blocks in your pickup code. You picked up a gun and that creates the axe on the ground, then that newly dropped axe gets picked up next and that recreates the gun on the ground. Put an else before your second if to make it an if-elseif ladder. That way at most only one block executes, even if it enables the condition for other ones down the line.

Also, your current system of having the axe care about the gun (and vice versa) is awful architecture. Every time you add a new weapon, every other weapon has to be changed to account for when the new weapon is put down. The player should be the one caring about dropping the existing weapon, not the pickups. You should re-engineer this part as soon as you can.
So where should I insert the if-else-if ladder?
 

FrostyCat

Redemption Seeker
So where should I insert the if-else-if ladder?
I told you the problem is in the pickup code, where else could the ladder belong?
Code:
if (distance_to_object(obj_GunSprite) < 32 ) {
  instance_create_layer(obj_GunSprite.x, obj_GunSprite.y, "GunLayer", obj_GunReal);
}
else if (distance_to_object(obj_Axe) < 32 ) {
  instance_create_layer(obj_Axe.x, obj_Axe.y, "GunLayer", obj_AxeHand);
}
Also, stop using [quote] tags for code, start using [code] tags.
 
W

Wicked

Guest
I told you the problem is in the pickup code, where else could the ladder belong?
Code:
if (distance_to_object(obj_GunSprite) < 32 ) {
  instance_create_layer(obj_GunSprite.x, obj_GunSprite.y, "GunLayer", obj_GunReal);
}
else if (distance_to_object(obj_Axe) < 32 ) {
  instance_create_layer(obj_Axe.x, obj_Axe.y, "GunLayer", obj_AxeHand);
}
Also, stop using [quote] tags for code, start using [code] tags.
So I implimented it, but now picking up the gun whilst holding the axe just duplicates the gun on the ground, why do you think thats happening?
 
Top