GameMaker syntax for calling nested arrays

L

Lorp

Guest
I read that one can create nested arrays in gms2 with the following code:
var array = [[x1,x2,...],[y1,y2...]];

It works, however the syntax for calling a single point within a nested array does not work with any standard gml method. What is the syntax? I can't seem to find the documentation for this new gms2 feature.

I'm currently using two parallel lists (x's and y's) to store grid path coordinates and iterating through those values to convert it to pixel coordinates(the grid positions dynamically). It works well but it's allot of code and I would rather pass one array per path rather than two lists or 1d arrays. Furthermore, initializing non-linear constant paths is a huge chore with a standard 2d array because it has to be added one at a time.
example:
array[@ 0,0] = 1st x;
array[@ 0,1] = 2nd x;
array[@ 1,0] = 1st y;
array[@ 1,1] = 2nd y;

vs

array = [[1st x, 2nd x],[1st y, 2nd y]];

That is just for 2 positions. Imagine 20 or 30 positions in a predetermined erratic path or dynamically creating a new one.

Thanks in advance....
 
Last edited by a moderator:
You have to pull the inner array out first:
Code:
var arr = [[0,1],[2,3]];

var inner_arr = arr[0];
var x1 = inner_arr[0];
var y1 = inner_arr[1];

var inner_arr = arr[1];
var x2 = inner_arr[0];
var y2 = inner_arr[1]
In that example x1 = 0, y1 = 1, x2 = 2, y2 = 3
 

NightFrost

Member
Reading deeper from nested arrays is on their todo-list. For now, you must squeeze the arrays out of each other sequentially until you are at required depth.
 

FrostyCat

Redemption Seeker
For now, you have to extract the values layer-by-layer or use this workaround. Chaining accessors is a planned feature for GML, but I wouldn't recommend holding your breath for it, given that it has languished on the Roadmap for several years.
 
L

Lorp

Guest
For those who are having the same problem, here is a work around. It converts the nested array ( a referenced 1d array) into a 2d array so one can enjoy all the standard call functionality of a 2d array without the the pages of code populating it or dismantling the nested array to access the hierarchy .

It's only good if all the nested arrays are the same size but good enough for the described purposes.

Code:
///scr_nest_convert_2d(nested_array)
///@param nested_array

var array = argument0;
var columns, rows, array_2d;

columns = array_length_1d(array);
rows = array_length_1d(array[0]);
array_2d = scr_make_array(columns,rows);

for(i = 0; i < columns; i++){
    var column = array[i];

    for(j = 0; j < rows; j++){
        var value = column[j];
        array_2d[i,j] = value;
    }
}

return array_2d
 
Last edited by a moderator:

serup

Member
I noticed that you could create arrays like this :

1589825491911.png

What would then be the best way to access the elements ? -- like the one in the image ?

instead of this :
number = numbers[0];
numberPixelRow = number[1];
numberPixel = numberPixelRow[2]; // the one highlighted in image
 
Last edited:

chamaeleon

Member
What would then be the best way to access the elements ? -- like the one in the image ?
If by best, you mean at all
Code:
var a = [[1, 2], [3, 4]];
var b = a[0];
var c = b[1]; // c will contain 2
GMS 2.3+ will bring much relief in this area with chained accessors. But until you have it, you'll need to unwrap the content yourself using intermediate variables. In other words, no syntax support yet to simplify it.

Edit: Also try to refrain from bumping old threads and create your own.
 

serup

Member
I noticed that you could create arrays like this :

1589827131739.png

What would then be the best way to access the elements ? -- like the one in the image ?

instead of this :
number = numbers[0];
numberPixelRow = number[1];
numberPixel = numberPixelRow[2]; // the one highlighted in image

does GM2 have a general method or should I create a general script to handle above - like this :

1589827859433.png

used like this :
test1 = getXYZelement(numbers,0,1,2);
 

Attachments

Last edited:
Top