deleting multiple objects - shortcut

shmurl

Member
At the end of a level, I want to delete a bunch of different objects from the controller.

I have been doing
with (obj_1){instance_destroy};
with (obj_2){instance_destroy};
with (obj_3){instance_destroy};
etc.

is there a shortcut to do that faster by putting all the objects in one with statement?
 

Hyomoto

Member
With makes the instance_destroy() code run on objects of that type. So what you can do, if you have lots of objects that need to be destroyed, is to give them all the same parent object, then call with on THAT. Check out parents in the help guide for more information.
 
B

bojack29

Guest
Code:
with(parent){
     instance_destroy();
}
Assuming they are all object of the same parent
 
You can also add all the object types you want to destroy into an array/list and loop through them, roughly:

Code:
var objs = noone, c1 = 0;

objs[0] = obj_solid;
objs[1] = obj_enemy;
objs[2] = obj_powerup;

repeat (array_length_1d(objs))
{
    with (objs[c1++])
    {
        instance_destroy();
    }
}
 

Imperial

Member
Code:
for(i = 0; i < number_of_objects; i++)
{
    Target = asset_get_index("obj_"+string(i));
    if instance_exists(Target)
    {
        with(Target) instance_destroy();
    }
}
 

shmurl

Member
Thanks all

In this case, I don't want to use a parent.

@stainedofmind that should work. thanks.
@Imperial Will that destroy everything in the room? I only want to destroy specific objects, not all objects

If anyone has a shorter way of doing it, keep it coming.

Thanks
 

Imperial

Member
Thanks all

In this case, I don't want to use a parent.

@stainedofmind that should work. thanks.
@Imperial Will that destroy everything in the room? I only want to destroy specific objects, not all objects

If anyone has a shorter way of doing it, keep it coming.

Thanks
no only with the object you specify with same name and different numbers
 

shmurl

Member
so where would I add the other objects that I want to destroy?

Code:
for(i = 0; i < number_of_objects; i++)
{
    Target = asset_get_index("obj_"+string(i));//WOULD I ADD THE OTHER OBJECTS HERE?
    if instance_exists(Target)
    {
        with(Target) instance_destroy();
    }
}
 

FrostyCat

Redemption Seeker
so where would I add the other objects that I want to destroy?

Code:
for(i = 0; i < number_of_objects; i++)
{
    Target = asset_get_index("obj_"+string(i));//WOULD I ADD THE OTHER OBJECTS HERE?
    if instance_exists(Target)
    {
        with(Target) instance_destroy();
    }
}
Nowhere --- Imperial assumed that your objects are actually labelled like obj_0, obj_1, obj_2, etc. and that you wanted every one of them gone.

Imperial's answer is simply misguided compared to stainedofmind's, which I also recommend.
 
Top