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

How to destroy a specific instance?

A

Alpha128

Guest
Hello.
I am making a game (mostly under the Shaun Spaulding tutorials) and I just want to make the bullet disappear when it hits the enemy. I'm not too sure how to go about this, most of the time it just destroys all of the bullets instead of just the one that touched the enemy. I am new at this and this is my first game as you can probably see. Any help is appreciated :D
 

Simon Gust

Member
You would have to show us what code you're dealing with.
Most likely, you're calling something along those lines
Code:
instance_destroy(obj_bullet);
when you should be calling something more like this
Code:
instance_destroy(id);
But of course, without seeing your code, we can't really apply a result.
 

GMWolf

aka fel666
In the bullet object, add a collision event with the enemy.
In that event, destroy the instance using instance_destroy()
 
A

Alpha128

Guest
Ok here is my code so far. But I have tried using instance_destroy(id); before and it still destroys everthing.

Code:
with(other)
{
hp = hp - 0.5;
}

Btw haven't added anything because it hasn't worked.
And thank you for replying so quick! :D
 
Z

zecton

Guest
obj_bullet-->step-->execute a piece of script:

Code:
if place_meeting(x,y, obj_enemy)
{
    with (obj_enemy)
    {
          instance_destroy();
    } 
    instance_destroy();
}
The bullet touch the enemy, then the enemy which has the contact get destroyed and the bullet too!

You´ll have a better explanation in this video:

 
A

Alpha128

Guest
obj_bullet-->step-->execute a piece of script:

Code:
if place_meeting(x,y, obj_enemy)
{
    with (obj_enemy)
    {
          instance_destroy();
    }
    instance_destroy();
}
The bullet touch the enemy, then the enemy which has the contact get destroyed and the bullet too!

You´ll have a better explanation in this video:

Thanks! It worked... but now there is a new problem... now all the enemies are dying if I kill one. :(
 
A

Alpha128

Guest
You would have to show us what code you're dealing with.
Most likely, you're calling something along those lines
Code:
instance_destroy(obj_bullet);
when you should be calling something more like this
Code:
instance_destroy(id);
But of course, without seeing your code, we can't really apply a result.
I have posted my code. Forgot to reply instead, sorry!
 
Z

zecton

Guest
Thanks! It worked... but now there is a new problem... now all the enemies are dying if I kill one. :(
hmm.. delete all the "with obj_enemy" lines of code and then go to obj_enemy -->step-->execute a piece of code:
Code:
if place_meeting(x,y,obj_bullet)
{
    instance_destroy();
}
Or dont erase and try this is obj_bullet:
Code:
with obj_enemy
{
 instance_destroy(id);
}
 
A

Alpha128

Guest
hmm.. delete all the "with obj_enemy" lines of code and then go to obj_enemy -->step-->execute a piece of code:
Code:
if place_meeting(x,y,obj_bullet)
{
    instance_destroy();
}
Or dont erase and try this is obj_bullet:
Code:
with obj_enemy
{
 instance_destroy(id);
}
HMMM... now its not recognising a variable (my enemy's health). It claims it was not set up, but I have made a create code for the enemy. Waaaaa?

and i modified the code btw for health deduction -
if place_meeting(x,y, obj_enemy1)

Code:
if place_meeting(x,y, obj_enemy)
{
    hp = hp - 1;
    instance_destroy();
}
 
Last edited by a moderator:
Z

zecton

Guest
HMMM... now its not recognising a variable (my enemy's health). It claims it was not set up, but I have made a create code for the enemy. Waaaaa?
does the enemy have a health system?
obj_enemy-->code
Code:
if health >0
{
    health += -1;
}

if health <=0
{
  instance_destroy();
}

If gamemaker tells you that the code is not set up, then in some part of your code (step, alarm...) one variable has been wrote wrong, check your step event and the create event (use ctrl +f will be faster) and also check the error message which tells you where the error happen.
 
A

Alpha128

Guest
does the enemy have a health system?
obj_enemy-->code
Code:
if health >0
{
    health += -1;
}

if health <=0
{
  instance_destroy();
}

If gamemaker tells you that the code is not set up, then in some part of your code (step, alarm...) one variable has been wrote wrong, check your step event and the create event (use ctrl +f will be faster) and also check the error message which tells you where the error happen.
I have made the health system. The error popup says along these lines -
FATAL error in action number 1 of Step_Evetobj_enemy for object obj_bullet. Variable obj_bullet.hp (some numbers) not set before reading it...
So I think gamemaker thinks the hp variable is for obj_bullet?
 

Simon Gust

Member
Let's try to do this properly.
Code:
var enemy_hit = instance_place(x, y, obj_enemy);
if (enemy_hit != noone)
{
    enemy_hit.hp -= 1;
    instance_destroy(id);
}
 

GMWolf

aka fel666
In a collision event, the keyword 'other' represents the instance you collided with.

So you can do something like this:
In obj_enemy collide with bullet:
Code:
hp -= 1; //take damage
if (hp <= 0)
{
    instance_destroy();//die when out of health
}
instance_destroy(other); //destroy the bullet

Never, EVER, try to refer to an instance using the object name.
with(obj_bullet) will select all bullets.
instance_destroy(obj_bullet) will destroy all bullets.
And obj_bullet.x will get the X value of an undefined (random) bullet.

when dealing with instances, stick to instance IDs. And if you can't keep track of them using 'other', keep them around in your own variables if you have to.


[Edit]
@Simon Gust s solution will work nicely as well if you would rather use the collision functions rather than the collision events. (Two different, equally valid approachs)
 
Last edited:
A

Alpha128

Guest
Let's try to do this properly.
Code:
var enemy_hit = instance_place(x, y, obj_enemy);
if (enemy_hit != noone)
{
    enemy_hit.hp -= 1;
    instance_destroy(id);
}
In a collision event, the keyword 'other' represents the instance you collided with.

So you can do something like this:
In obj_enemy collide with bullet:
Code:
hp -= 1; //take damage
if (hp <= 0)
{
    instance_destroy();//die when out of health
}
instance_destroy(other); //destroy the bullet


Never, EVER, try to refer to an instance using the object name.
with(obj_bullet) will select all bullets.
instance_destroy(obj_bullet) will destroy all bullets.
And obj_bullet.x will get the X value of an undefined (random) bullet.

when dealing with instances, stick to instance IDs. And if you can't keep track of them using 'other', keep them around in your own variables if you have to.


[Edit]
@Simon Gust s solution will work nicely as well if you would rather use the collision functions rather than the collision events. (Two different, equally valid approachs)
Which one do I use?
EDIT:
No idea how where to incorporate either so I just replaced my old code with one of them. Tried both, both gave me pretty much the same error... :(
 
A

Alpha128

Guest
Oh well @Simon Gust + @GMWolf , \\ ..(I'm probably really dumb) Don't even know where to put all of you codes. In the bullet code or the enemy code?.

HOWEVER, I managed to get rid of the error... I was being dumb don't worry but...
 
Last edited by a moderator:
A

Alpha128

Guest
Oh well @Simon Gust + @GMWolf , \\ ..(I'm probably really dumb) Don't even know where to put all of you codes. In the bullet code or the enemy code?.

HOWEVER, I managed to get rid of the error... I was being dumb don't worry but...
OMG sorry again. Another error of the like comes up again... I don't even know anymore
 
Z

zecton

Guest
I have made the health system. The error popup says along these lines -
FATAL error in action number 1 of Step_Evetobj_enemy for object obj_bullet. Variable obj_bullet.hp (some numbers) not set before reading it...
So I think gamemaker thinks the hp variable is for obj_bullet?
Has the hp variable been written in the obj_bullet create event?
Anyway, if the bullet hits the enemy and you want the bullet to be destroyed, don´t use the hp variable.
If you set the code inside the obj_bullet, then don´t write "hp" , write "obj_player.hp" if you want that the computer understand that the hp variable is related to the player and not the bullet.
 
A

Alpha128

Guest
Has the hp variable been written in the obj_bullet create event?
Anyway, if the bullet hits the enemy and you want the bullet to be destroyed, don´t use the hp variable.
If you set the code inside the obj_bullet, then don´t write "hp" , write "obj_player.hp" if you want that the computer understand that the hp variable is related to the player and not the bullet.
No I set HP in my enemy(ies). But I have ridden myself of that problem. Now its back to destroying all enemies.
 
A

Alpha128

Guest
YEEEEAAAAHHH
@zecton @Simon Gust @GMWolf
I have successfully done it. With your guys input of course. Thank you guys for responding so quickly with relevant and helpful code and explanations.;):D
Heres the end code:

Code:
with(other)
{
    hep -= 1; //take damage
    if (hep <= 0)
    {
        instance_destroy();//die when out of health
    }
    instance_destroy(other); //destroy the bullet
}
- 99.9999999% GMWolf's code
 

GMWolf

aka fel666
YEEEEAAAAHHH
@zecton @Simon Gust @GMWolf
I have successfully done it. With your guys input of course. Thank you guys for responding so quickly with relevant and helpful code and explanations.;):D
Heres the end code:

Code:
with(other)
{
    hep -= 1; //take damage
    if (hep <= 0)
    {
        instance_destroy();//die when out of health
    }
    instance_destroy(other); //destroy the bullet
}
- 99.9999999% GMWolf's code
Nice!
Now notice how the entire code has a with(other) block around it.
That means you can put the code in the opposite collision event (so instead of in A collision with B, in B collision with A), without the with block. That should make it easier to follow (especially because of 'other' within with block in collision event, that gets a little messy)


It's a small thing, but trying to clean up code after you got it working is generally good practice.
 
J

jamie_simpson

Guest
for those of you who ended up here because of the same problem and still haven't gotten a solution here's what worked for me:
bullet_obj --> event: collision with enemy -->

Code:
//event: collision with enemy

instance_destroy(instance_id);
i assume that using 'id' as the argument still refers to the id of the object, the same way using 'self ' or 'bullet_obj' does.
but if you use 'instance_id' instead, it refers to the id of instance that code is being executed in.
(please correct me if i am wrong)

i hope this works and becomes good use to you.
 

TsukaYuriko

☄️
Forum Staff
Moderator
If this code works when using instance_id like that, that's a coincidence at best and wrong at worst.

instance_id is (to quote the manual) a read only array (which) holds all the ids of every active instance within the room.

This could work if you're using a version of GameMaker that refers to the first index of an array when used in variable context and the instance in question happens to be the first one. In all other cases, this will either destroy the wrong instance or crash.


id refers to the instance that is under the current execution scope.
self used to be equivalent to the value -1, which was interpreted as equivalent to id in some functions. Now, it is equivalent to id.
An object's name does not refer to the calling instance, but either to the first instance of an object in a reading context, or to all instances of an object in a writing context. If an object ID is interpreted as the instance you meant to refer to, it is either because that instance is the only instance of that object, or a sheer coincidence.


Use id. Or, better yet, just call instance_destroy() without any arguments - then it will automatically apply to itself.
 
Last edited:
  • Like
Reactions: Yal

Joe Ellis

Member
As frosty cat would say, I think you don't know or realize the difference between objects and instances.
"id" refers to the id of an instance, of an object. The objects are what you predefine in the editor and set certain events in them. The instances are the live versions of them, each with their own variables. So if you apply something to an object, it applies it to all instances of it. Whereas if you apply something to an instance, it only does it to that one. This is quite a big problem that beginners often get wrong and make mistakes with. And its understandable, and it should be made more clear really, I guess frosty cat is doing that. But it should be lesson 1 before anyone even begins programming gml
 
  • Like
Reactions: Yal
J

jamie_simpson

Guest
i managed to fix my problem, i do understand the difference between an instance and an object, the problem was that i have a penetration variable for my bullets, so each time they hit an enemy they loose 1 penetration and if they have 0 penetration they get destroyed
the problem came in when dealing with that penetration variable, when an enemy collided with a bullet it subtracted 1 penetration from that bullet (or so i thought)
to do that i used this code:
zombieO --> collision with bulletO -->
Code:
with (bulletO)
{
    penetration -- ;
}
this subtracted 1 penetration for all the bullets in the room
i fixed this by using 'with other' instead of 'with (bulletO)' which only subtracted 1 penetration with the bullet it was colliding with.
anyways thanks for your responses
 
Top