SOLVED The Curse of "Instance_destroy()"

Null-Z

Member
Moving this to a thread to bookmark after solving.

context-
" while creating the Galaga clone to break in GMS2 I run into the same problem with "instance_destroy()" where it destroys all instances in the room and I never remember how to remedy it."
to put it another way, I want the bullet to only destroy the instance it collides with, not all of that instance in the room.


" nacho_chicken
@Neptune Don't even have to do that any more, as now instance_destroy takes an argument! instance_destroy(inst_id); "

wouldn't that requier calling each unique instance ID possible for each instance created?
 

chamaeleon

Member
Define "collides with". Collision event? Then you have the current instance, and other, destroy the appropriate one or both (using instance_destroy() and instance_destroy(other)). If collision means using instance_place() or instance_place_list() which returns an id the current instance is colliding with (if there is one) or a list of ids. Just plug in the id or iterate over the the list and plug in all or some ids in the instance_destroy() call, and you won't be destroying all instances, just those the function calls indicate are collisions.
 

Null-Z

Member
ok, that's a new one by me. how do I call the id of the instance in the code "instance_destroy()" ?
 

chamaeleon

Member
ok, that's a new one by me. how do I call the id of the instance in the code "instance_destroy()" ?
What does "call the id" mean to you? If you want to destroy the instance currently executing code, don't provide anything at all in the call, or use id. If you want to destroy the instance returned by a function, or other in a collision event, just use that return value or other. instance_destroy() without an argument destroys the current instance, given an argument it destroys the given instance.
 

Null-Z

Member
I know how to code an instance to destroy itself, what I want to know is how to make an object destroy an instance under the condition of instance_place() and not destroy every instance of that instance in the room. what I was confused about your previous statement was how do I get that one instance id for that.
 

chamaeleon

Member
I know how to code an instance to destroy itself, what I want to know is how to make an object destroy an instance under the condition of instance_place() and not destroy every instance of that instance in the room. what I was confused about your previous statement was how do I get that one instance id for that.
Store the return value of instance_place() in a variable and pass that variable to instance_destroy().
 

chamaeleon

Member
Elaborate?

is this like var Hit = instance_place(obj_Enemy)
Yes, if you write that, you can pass Hit as an argument to instance_destroy(). Typically you would have an if statement checking if Hit is not noone and destroy the instance if you did get an instance back (and possibly do other things as well, depending on what your program requires in when this destruction takes place). When you are unfamiliar with some function you are trying to use, I strongly recommend reading the manual entry for it. For instance, the entry for instance_place() has an example that is more or less what you want.

Since you are asking about things that are essentially fundamental programming constructs with no special meaning with respect to collisions, I would perhaps also recommend watching @samspade's video series on GML programming.
 

TsukaYuriko

☄️
Forum Staff
Moderator
When you're unsure how to use something, middle-click it to open its manual page.

Sometimes, those manual pages even contain usage examples.

Sometimes, they contain usage examples of exactly the thing that's being discussed in this topic.

Sometimes... ;)
 

Null-Z

Member
Solution found.
var hit = instance_place(x,y,obj_NME)
if (hit != noone)
{
instance_destroy(hit)
instance_destroy();
}
 

Mert

Member
This is not just you.
Collision Event and instance_destroy does not work for some objects, which I would have to use instance_place to cover that part.
I think it may be a major bug. I thought it was only for me tho.
 
This is not just you.
Collision Event and instance_destroy does not work for some objects, which I would have to use instance_place to cover that part.
I think it may be a major bug. I thought it was only for me tho.
That sounds like it's just down to your development rather than a GM issue.
 

Mert

Member
Works fine for me. The instance with the collision event defined is destroyed when colliding with an instance of the object type the collision is defined for.
In most cases, it also works for me. But in some rare cases, it doesn't work (Especially with some physics objects)
 

chamaeleon

Member
In most cases, it also works for me. But in some rare cases, it doesn't work (Especially with some physics objects)
I'd assume it's due to the collision event not firing (due to whatever object settings makes it not check physics instance with non-physics instance collisions, etc.), not instance_destroy() not destroying the instance.
 

kburkhart84

Firehammer Games
I personally have never had an instance not get destroyed if I call instance_destroy()...for all the bugs and defects I've ran across in Gamemaker over the years...this one has simply never been one of them.
 
Top