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

Make Two Objects Destroy Each Other

Kind of running into a slight problem

I'm trying to have a bullet that destroys itself when hits anything using if place_meeting. The issue being due to the way executing code in steps works only the first object to run it's code is destroyed when it hits something that's also programed to destroy itself when hit by the bullet.

Since the other one is failing to return true on a place_meeting with an instance that no longer exists.
 

woods

Member
can you do a instance_destroy other() and then instance_destroy_self() say .. in the bullet object only.. instead of destroy self in both?
 

woods

Member
this wont work? ;/
bullet step event
if place_meeting(x,y,obj_box)
{
instance_destroy(other); // destroy the box
instance_destroy(); // destroy the bullet..self
}
 
Use instance_place instead of place_meeting to return the instance ID of the object it's colliding with.

Code:
inst = instance_place(x,y,obj_thing);
if (inst != noone) with (inst) instance_destroy();
instance_destroy();
If I may, can you format that as standard if statement code with curly brackets so I can read what's going on?

I don't quite know how to read that style of compressed/shorthand syntax
 
Last edited:
If I may, can you format that as standard if statement code with curly brackets so I read what's going on?

I don't quite know how to read that style of compresse/shorthand syntax
GML:
inst = instance_place(x,y,obj_thing);
if (inst != noone) {
    //with (inst) {
    //    instance_destroy();
    //}
    // The above, while directly equivalent, is commented out because the following is a much better way of doing it:
    instance_destroy(inst);
}
instance_destroy(); // EDIT: As brought out below, this should go inside the inst != noone check. This is out here because it's what that code would read as.
 
Last edited:

Mk.2

Member
If I may, can you format that as standard if statement code with curly brackets so I read what's going on?

I don't quite know how to read that style of compresse/shorthand syntax
GML:
inst = instance_place(x,y,obj_thing); //Store the instance ID of the object that the bullet is colliding with as the variable "inst".

if  (inst != noone) //First check that inst actually exists
{
      with (inst) //This is the equivalent of using "with (other)" in a collision event. Everything inside the curly brackets applies to inst.
                       //Some additional info, you can actually use "other" inside these brackets too, to refer to the bullet object.
      {
               instance_destroy();
      }
}

instance_destroy(); //Destroy bullet
 

Mk.2

Member
// The above, while directly equivalent, is commented out because the following is a much better way of doing it:
instance_destroy(inst);
This is true. I included the with statement to show that other things can be done to the target object after storing its instance ID, but the OP did only mention wanting to destroy it so fair enough.
 
GML:
inst = instance_place(x,y,obj_thing);
if (inst != noone) {
    //with (inst) {
    //    instance_destroy();
    //}
    // The above, while directly equivalent, is commented out because the following is a much better way of doing it:
    instance_destroy(inst);
}
instance_destroy();

I think I get it. If it hits the bullet in any step it stores that bullet's ID in insta

Then if the insta variable ever returns anything but empty it destroys the object with the stored ID

But shouldn't the destroy self code also be in the brackets since it's conditional on contacting the bullet?


Another quick question though, what's with the eclipses some of you use around the variable being checked? I've never seen that done in GML in any tutorial before so I'm unfamiliar with it.
 

TsukaYuriko

☄️
Forum Staff
Moderator
Correct - and catching that means you can rest assured that you understood how this works. :)

Edit: Whoa, that's an overwhelming amount of confirmation there... :D
 
I think I get it. If it hits the bullet in any step it stores that bullet's ID in insta

Then if the insta variable ever returns anything but empty it destroys the object with the stored ID

But shouldn't the destroy self code also be in the brackets since it's conditional on contacting the bullet?
Yes. Good catch! That's what the code would've read as, so I didn't check for errors. It should go inside the inst != noone braces.
 
Top