instance_destroy() doesn't work on parent objects

Masstertron

Member
Hi everyone!

(GMS 2.3)

I've noticed that using instance_destroy() doesn't work for children objects.

For example, when I use instance_destroy(obj_enemy), my expectation is that all enemies, including child-object enemies (eg: obj_enemy_bird, obj_enemy_fish) are destroyed.

What happens is no enemies are destroyed, because all of the instances in the room are instances of obj_enemy_bird and obj_enemy_fish.

The documentation on instance_destroy() doesn't touch on this subject, but does anyone know if I'm missing something, or if I should simply use this:

GML:
layer_destroy_instances("lyr_enemies");
The above line works nicely, but instance_destroy() seems more elegant and reliable long-term (if it worked).

Cheers :)
 

Roldy

Member
Hi everyone!

(GMS 2.3)

I've noticed that using instance_destroy() doesn't work for children objects.

For example, when I use instance_destroy(obj_enemy), my expectation is that all enemies, including child-object enemies (eg: obj_enemy_bird, obj_enemy_fish) are destroyed.

What happens is no enemies are destroyed, because all of the instances in the room are instances of obj_enemy_bird and obj_enemy_fish.

The documentation on instance_destroy() doesn't touch on this subject, but does anyone know if I'm missing something, or if I should simply use this:

GML:
layer_destroy_instances("lyr_enemies");
The above line works nicely, but instance_destroy() seems more elegant and reliable long-term (if it worked).

Cheers :)
You will need to show an example of this not working. I just made a test program with two objects: oParent, oChild and called instance_destroy(oParent) which resulted in all oChild instances being destroyed. I tested with the runner on MacOS.


The documentation on instance_destroy() doesn't touch on this subject, but does anyone know if I'm missing something, or if I should simply use this:
This manual page specifically states this should work. So if you have a reproducible case where it doesn't you should report as a bug.

EDIT: link to proper manual page.
 
Last edited:

Roldy

Member
I don't think the manual page is clearly articulating that child object instances will be destroyed.
Doh.. I linked to wrong page: this page

Whenever you target a parent object in code, the code will also apply to the "children" of the parent object too. This happens when, in an action, you indicate that the action must be applied to instances of a certain object, and in code it happens when you use the with() statement. It will work like this too when you call code functions, like instance_position(),instance_number(), etc... where - if you supply a parent object - all instances of the parent and child instances will be included in the checks.
 

Roldy

Member
That reads better, of course. And I can confirm that it does work. I'm not sure why I did not argue that it would work, instead of reaching for the with() statement.
Well it would be a good test for OP.. If he changed to using 'with' and it still didn't work.
 

Masstertron

Member
Thanks for you input folks :)

I came back to this issue, and after a bit of debugging, I've realized that it's because I'm trying to destroy the enemies immediately after unpausing the game.

My logic is:
> Pause the game, thus deactivate all appropriate objects
> Click the "leave mission" button
> Reactivate all appropriate objects, destroy all enemies, open mission select screen

I'm using instance_activate_layer and instance_deactivate_layer to pause / resume all non-UI objects in the game.
I was aware of this line in the documentation for instance_activate_layer, but I wasn't careful enough.

Note too that activation is not instantaneous, and an instance that has been activated in this way will not be considered to be active until the end of the event in which the function was called.

Now I'm actually not sure why layer_destroy_instances() worked at all, but alas I'll rework my mission-leave logic so that at least one frame passes between the time the button is clicked, and mission-leave is executed.

All the best!
 

Nocturne

Friendly Tyrant
Forum Staff
Admin
Moderator
Now I'm actually not sure why layer_destroy_instances() worked at all, but alas I'll rework my mission-leave logic so that at least one frame passes between the time the button is clicked, and mission-leave is executed.
The most likely answer is that it goes through a different code pipeline than the instance_destroy() function. It might be a good idea to actually file a bug report about this inconsistency and have YYG check that things are working as intended. I would honestly have expected that function to be simply calling instance_destroy() "behind the scenes" on the instances on the layer, and that the behaviour would be the same.
 

TheouAegis

Member
It might not even be a different pipeline. Maybe GM maintains a data struct for layers, but not depths, and thus has access to the ids of all instances on that layer, allowing it to bypass the "active" state requirement of instance_destroy(). It would be a simple test to check, just pass the id of a deactivated instance as the argument to instance_destroy(), then reactivate the instance. If the instance is still here, then yeah maybe you need to file bug report. If the instance is gone, that means it work as intended.
 
Last edited:
Top