GMS 2 delete all objects within specific layer

S

Siracher

Guest
Hi all

I am using the code below to delete all enemies within the layer "enemies" when the object containing this code is created.

unfortunately there are 2 issues:
- the first created enemy will not be deleted
- the code deletes also some objects within other layers (this is really critical and I don't get why the code is doing this...)

does anybody know what the problem is?

Code:
var layerid = layer_get_id("enemies");
var a = layer_get_all_elements(layerid);
for (var i = 0; i < array_length_1d(a); i++;)
   {
    deletme = instance_id[i]
    instance_create_layer(deletme.x,deletme.y,"Instances",obj_bubbles)
     instance_destroy(deletme)
    }
   
audio_play_sound(global.lvl_snd,10,0);

instance_destroy();
 
You are using instance_id when you shouldn't be. instance_id holds the list of every instance in the room, so it makes no sense for you to be trying to get the correct instances from that to destroy - especially as the array a should already be holding the IDs of all of the instances on that layer. Try changing your code to the following (which I put together from your original code and the example in the manual for layer_get_all_elements) and see if this works:

Code:
var layerid = layer_get_id("enemies");
var a = layer_get_all_elements(layerid);

for (var i = 0; i < array_length_1d(a); i++;)
{
   deletme = a[i];
    instance_create_layer(deletme.x,deletme.y,"Instances",obj_bubbles)
    instance_destroy(deletme);
}

audio_play_sound(global.lvl_snd,10,0);

instance_destroy();
 
K

kevins_office

Guest
Element ID is not the same as Instance ID. This is because a sprite, or object, or tilemap can exist on the same layer.
You need to check each element first to see what type it is.
Code:
layer_get_element_type(element_id)
Then if the element type == layerelementtype_instance you have to convert the element ID to an instance ID
Code:
var instance_id = layer_instance_get_instance(element_id);
 
S

Siracher

Guest
Hi Babia and Kevin

thanks a lot for your feedback! I'll give it a try this evening and let you know if it works

cheers
 
S

Siracher

Guest
I just tested it and I got a working solution thanks to you guys!
I was not aware that instance_id is actually an array, so I was always on the wrong path...
The needed function was really as from kevin mentioned: layer_instance_get_instance

here's my final code:

Code:
elements = layer_get_all_elements("enemies");
for (var i = 0; i < array_length_1d(elements); i++)
    {
     if (layer_get_element_type(elements[i]) == layerelementtype_instance)
        {
        var layerelement = elements[i];
        var inst = layer_instance_get_instance(layerelement);
        instance_create_layer(inst.x,inst.y,"Instances",obj_bubbles)
        instance_destroy(inst)
        }
    }
audio_play_sound(global.lvl_snd,10,0);
instance_destroy();
 
K

kevins_office

Guest
I was not aware that instance_id is actually an array
I think you mean element_id is an array. Because instance_id is not an array, its a real (data type) value.


Code:
var layerelement = elements[i];
var inst = layer_instance_get_instance(layerelement);
I know this is just being nit picky, but you know you can consolidate those two lines and remove the need for the game to create an extra variable and added CPU steps
Code:
var inst = layer_instance_get_instance(elements[i]);
 
S

Siracher

Guest
ah yes complete confusion on my end :confused:

thanks for the tip with the shorter code. haha, I think you would see a lot of code to clean up in my game :D

thanks a lot for your help!
 
Top