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

My enemies die weirdly

8

82499

Guest
When i program my enemies to die, some dont die at all

heres my code
///step
if (obj_enemy_orc.hp < 1) {
instance_destroy();
obj_player.score +=15;
}

///collision with ammo
hp -=1;
destroy the instance
 
S

spoonsinbunnies

Guest
refrencing an objects name in another object will only check one instance of the object. if the death check is in if obj_enemy_orc you can just say hp
 

samspade

Member
To add more to spoonsinbunnies's comment, saying obj_enemy_orc.hp references an object not an instance. GM effectively picks an object at random (I think it's based upon creation order but I'm not sure) and checks that one. So if it picks the wrong orc, e.g. an orc who is not having its hp decreased, then even though you are decreasing the correct instances hp, it is not going to be destroyed.

I'm not sure who's step event is referenced by step, but if it is in the orc object, then you don't even need to reference the object id, you can just check if hp < 1. If it is in some other object, you will need to check differently. In this case, I would recommend the with statement:

Code:
with (obj_enemy_orc) {
    if (hp < 1) {
        instance_destroy();
        obj_player.score += 15;
    }
}
That will loop through all instances of object obj_enemy_orc and check each one individually to see if the hp is < 1.

Here are some things that might be helpful to read:
 
Top