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

Am I doing this "arrays of arrays" correctly?

So I have a script that returns an array of arrays. Not a 2D array, but specifically 2 separate arrays.
My reason for doing this is a bit tricky to explain. It's for inventory management. Basically I am building this array with the script, and then looping through a list of instances, and setting a "data" variable to indexes of this big array.

So it's like
Code:
var arrays = build_array_of_arrays_and_return_it();

for (var i = 0; i < array_length_id(arrays); i++) {
   var inst = instance_create()
   inst.data = arrays[i]
}
Each instances "data" contains an array. Doing this with a 2D array doesn't work. If there was arrays[3,0], just doing arrays[3] won't return the second dimension as another array.

The build array script goes something like this:
Code:
var arrays;
for (var i = 0; i < inventorySize; i++) {
    var data = array_create(2);
    data[0] = inventory[i].itemType;
    data[1] = inventory[i].stackSize;
    arrays[i] = data;
}
return arrays;
And this works as intended. My only concern is it seems weird to me that I have to use array_create(). If I just use
Code:
...
var data;
data[0] = inventory[i].itemType;
data[1] = inventory[i].stackSize;
...
like you normally would to initialize a local array, the same reference is used every iteration and every "data" array in the objects is same array between all of them. So basically, if you repeatedly use var array; in a loop, to initialize a new array, it only does it once. Whereas array_create() will actually create new references each time.

Maybe this seems unnecessary, but it's a very peculiar case for my game. There's multiple ways of expressing an inventory. Sometimes as stacked objects, sometimes as individual. This will return the proper UI data for building each kind of menu.

This also has me wondering about memory. Is this bigger "array" going to be destroyed at some point? I don't really understand how array references and local arrays work.
Does this seem like the right way to do things? Has anyone else run into this quirk?
 

Simon Gust

Member
You don't have to call array_create() as long as you initialize "data" to anything you want inside the loop.

This will create a singular array called data and all indexes of arrays[] will store the exact same array.
Code:
var arrays;
for (var i = 0; i < inventorySize; i++) {
    var data;
    data[0] = inventory[i].itemType;
    data[1] = inventory[i].stackSize;
    arrays[i] = data;
}
return arrays;
This will create a new array everytime because you overwrite the previous refrence with something new.
You can also set data to whatever you like, you can set it to array_create(2) if you'd like, or noone or undefined.
Code:
var arrays;
for (var i = 0; i < inventorySize; i++) {
    var data = 0;
    data[0] = inventory[i].itemType;
    data[1] = inventory[i].stackSize;
    arrays[i] = data;
}
return arrays;
If you don't set it to array_create(2) then it might be worth initializing data[1] before data[0].

To my knowledge, arrays are garbage collected and will be freed when no refrences exist anymore.
This happens if you manually set your array variable to anything again.
Code:
arrays = 0;
unless it's a var array, then it should do it automatically because vars lose their refrences at the end of the clause.
 
Top