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

Having a problem with instance_destroy()... please help [SOLVED]

N

Ned Madigan

Guest
So heres the deal, I am very new to gml, like very new. I am trying to make a the object destroy when its health is too low. But, when there are multiple of the same object in the room, they will all act as the same thing... they will all be destroyed. Is there a way to make it so only the object affected will be destroyed:
This is in the step event of, obj_enemy.
if healt<0
{
instance_destroy();
}
 

Soso

Member
your code should only destroy the instance calling it. Can you show the code where you deal damage to this object?
 

GMWolf

aka fel666
I assume you are using the 'health' variable.
Unfortunately, the health variable is a global variable shared by all instances.

So rather than 'health', try using 'hp' or 'my_health' or whatever.
It doesn't really matter, as long as it's not a built in variable.
 
N

Ned Madigan

Guest
your code should only destroy the instance calling it. Can you show the code where you deal damage to this object?
players code:
if place_meeting(x+2,y, obj_enemy) && sprite_index= spr_hit
{
obj_enemy.sprite_index= spr_enemyhit
}

enemys code:

if sprite_index= spr_enemyhit
{
healt-=1
}

....
- sprite_index=spr_enemyhit is the animation played when you hit the enemy
 

CloseRange

Member
did you actually use "healt" or did you mean "health"?
if you are using "health" (with an h) then that's a global variable and is always the same no matter what object calls it.

edit. didn't see your last post.
you say obj_enemy.sprite_index and if I'm not mistaken that will change it for EVERY enemy.
this means every enemy has it's image set to hit and all enemies lose their hp.
 
N

Ned Madigan

Guest
I assume you are using the 'health' variable.
Unfortunately, the health variable is a global variable shared by all instances.

So rather than 'health', try using 'hp' or 'my_health' or whatever.
It doesn't really matter, as long as it's not a built in variable.
I purposely made it "healt" so it wouldnt be the health global variable
 
N

Ned Madigan

Guest
did you actually use "healt" or did you mean "health"?
if you are using "health" (with an h) then that's a global variable and is always the same no matter what object calls it.
I made it healt so it woudlnt be global
 

GMWolf

aka fel666
obj_enemy.sprite_index= spr_enemyhit
The is is your problem here.
NEVER use object.variable.
Remember, objects don't store variables, instances do!

Instead, use the collision functions to get the id of the instance you collided with, and use that to get the variable.

Or easier; use the collision events and use 'other' to refer to the instances you collided with.
 

Soso

Member
yeah when you say obj_enemy.(variable) your accessing that var for every instance of obj_enemy

if you put your code in the collision event you can use other.(var) since other is specific to that collision

example
Code:
///Collision event

other.healt -= 1;
also this code here your using will minus 1 from healt every step of the game that sprite index == spr_enemy_hit

if sprite_index= spr_enemyhit
{
healt-=1
}
 
N

Ned Madigan

Guest
The is is your problem here.
NEVER use object.variable.
Remember, objects don't store variables, instances do!

Instead, use the collision functions to get the id of the instance you collided with, and use that to get the variable.

Or easier; use the collision events and use 'other' to refer to the instances you collided with.
Ok i will see if it works thanks
yeah when you say obj_enemy.(variable) your accessing that var for every instance of obj_enemy

if you put your code in the collision event you can use other.(var) since other is specific to that collision

example
Code:
///Collision event

other.healt -= 1;
also this code here your using will minus 1 from healt every step of the game that sprite index == spr_enemy_hit

if sprite_index= spr_enemyhit
{
healt-=1
}
Yes thank you so much it worked, thanks everyone as well, as i said i am very new to gml.
 
Top