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

GameMaker [Solved] layer_get_all_elements not returning array with every instance?

K

Konstamonsta

Guest
Following issue:
I have a global variable that i use to add up the value-variable of every object placed on the value-layer.
However when i look at the debugging process of everything
layer_get_all_elements
only gives back an array with one entry and that entries value goes up with every new element i create.
as a result my "for each element" loop only ever increases my number once instead of for each instance of the object.

Below is my code with comments where they come from. If i've cut out too much and need to post more complete code please tell me so but i think this should be everything concerning the problem.
Only thing besides the creation is that i can move the object around upon creation and it changes depth according to my y-position. Once i click and place the object it sits on depth = 0 though.

Code:
//Creation of the Object in a Draw_GUI event
objID = instance_create_layer(mouse_x,mouse_y,value_layer,obj_SomeObject);
Code:
//"Create"-code of the object
value = 10;
Code:
//Room Creation Code
globalvar value_layer, some_number;
value_layer = layer_get_id("Instances");
some_number = 0;
Code:
//Step Code from an object in the room handling all my game logic (on a different instance layer)
some_number = 0;
elements = layer_get_all_elements(value_layer);
for (var i = 0; i < array_length_1d(elements); i++)
    {
     if (layer_get_element_type(elements) == layerelementtype_instance)
        {
             var layerelement = elements;
             var inst = layer_instance_get_instance(layerelement);
             if instance_exists(inst)
             {
                 var stat = inst.status;
                 var val = inst.value;
                 if stat != "new"
                 {
                      some_number += val;
                 }  
             }
        }
    }
 
I believe Inside your for loop you should be using elements[ i ] because elements is an array and you want to reference each item in the array individually.

Code:
if (layer_get_element_type(elements[ i ]) == layerelementtype_instance)
{
var layerelement = elements[ i ];
 
K

Konstamonsta

Guest
I believe Inside your for loop you should be using elements[ i ] because elements is an array and you want to reference each item in the array individually.

Code:
if (layer_get_element_type(elements[ i ]) == layerelementtype_instance)
{
var layerelement = elements[ i ];
Thanks for looking into my problem.

That's an error that shouldn't have been there; actually had the code right in gms2 too so i just copy-pasted wrong.
Sadly the correct code doesn't run as wished.

I tinkered a bit with my code scanning all layers for the whereabouts of the object instances and they were just not there.
Turns out the fact that i change the depth after creation of the object actually takes them off their respective instance layer and places them on a new one that isn't even covered in "layer_get_all()"

I will mark this thread as solved and open a new one regarding that topic.
 
Top