Octopus_Tophat
Member
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
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:
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
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?
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]
}
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;
Code:
...
var data;
data[0] = inventory[i].itemType;
data[1] = inventory[i].stackSize;
...
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?