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

Delete all instances of an object... only deletes half?

W

whale_cancer

Guest
Hello!

This is part of my map loading code. I need to destroy all of the currently placed doors before placing the doors from the map save file. I thought this would work...
Code:
//delete all existing doors
tR = instance_number(obj_door)
show_debug_message(string(tR)+' doors to destroy');
dC = 0
for (i = 0; i < tR; i += 1)
{
    with (instance_find(obj_door,i))
    {
        instance_destroy();
        show_debug_message('Destroying door at '+string(x)+','+string(y))
        other.dC += 1
    }
}
show_debug_message(string(dC)+' doors actually destroyed.');
However, this only destroys HALF of the of the instances each time I run it! I don't get it. Any help?

Edit: Console output:
Code:
4 doors to destroy
Destroying door at 64,64
Destroying door at 112,96
2 doors actually destroyed.
 
M

Multimagyar

Guest
You could simply use with (obj_door) {instance_destroy();} instead of counting and going through them. With should apply to one or more objects. You don't need a loop to destroy them all.
 

jo-thijs

Member
What Multimagyar said.

Also, the reason your code is only destroying half of the instances, is because you destroy instance_find(obj_door, 0),
making every instance move 1 place in instance_find, putting another instance as instance_find(obj_door, 0).

However, you increase i and let that instance exist.
When you destroyed half the instances, i will be out of bounds and no instances will be destroyed.
 
W

whale_cancer

Guest
You could simply use with (obj_door) {instance_destroy();} instead of counting and going through them. With should apply to one or more objects. You don't need a loop to destroy them all.
That worked, great!
What Multimagyar said.

Also, the reason your code is only destroying half of the instances, is because you destroy instance_find(obj_door, 0),
making every instance move 1 place in instance_find, putting another instance as instance_find(obj_door, 0).

However, you increase i and let that instance exist.
When you destroyed half the instances, i will be out of bounds and no instances will be destroyed.
Thanks for explaining why that was happening. Useful information going forward. Thanks!
 
Top