Problem with multidimensional arrays (Solved)

olavman

Member
I'm trying to make a map editor and have arrays for each axis X, Y & Z and also an array for attributes for the tile on that spesific slot.
The problem I have is that when I try to change a spesific slot in the arrays, then all the slots in the first 2 arrays gets changed.

This is how i created the 4D array (in object oRender):
var arrayAttributes = array_create(4, 0);
var arrayZ = array_create(MAP_D, arrayAttributes); //MAP_D = 4
var arrayY = array_create(MAP_H, arrayZ); //MAP_H = 11
map = array_create(MAP_W, arrayY); //MAP_W = 16

And this is how i try to change a spesific slot (from within oTileCreater):
if (mouse_check_button_pressed(mb_left))
{
oRender.map[gridX][gridY][gridZ][TILE.SPRITE] = blockType; //TILE is an enum and SPRITE = 0
}

I've also tried just changing all the variables to hardcoded numbers just to check if that had something to do with it, but i still get the same result.
The entire X & Y grid on the selected Z grid fills with the expected blocktype
 

Nocturne

Friendly Tyrant
Forum Staff
Admin
When creating dimensions 2, 3, and 4, you are referencing previous arrays, not creating new ones. So, for example, arrayZ has a length of 4 and each one simply references the arrayAttributes array, instead of holding a unique 4 length array of it's own. Basically you are simply storing pointers to other arrays within each dimension rather than storing multiple arrays. I don't usually work with arrays greater than 2 dimensions, but I suspect you'll have to do something (horrible) like this:
GML:
array_create(MAP_W, array_create(MAP_H, array_create(MAP_D, array_create(array_create(4, 0)))));
 

olavman

Member
When creating dimensions 2, 3, and 4, you are referencing previous arrays, not creating new ones. So, for example, arrayZ has a length of 4 and each one simply references the arrayAttributes array, instead of holding a unique 4 length array of it's own. Basically you are simply storing pointers to other arrays within each dimension rather than storing multiple arrays. I don't usually work with arrays greater than 2 dimensions, but I suspect you'll have to do something (horrible) like this:
GML:
array_create(MAP_W, array_create(MAP_H, array_create(MAP_D, array_create(array_create(4, 0)))));
Unfortunatly still the same result :/
 

Ommn

Member
try show_debug_message and show us your results please
GML:
if (mouse_check_button_pressed(mb_left))
{
show_debug_message(oRender.map);
oRender.map[gridX][gridY][gridZ][TILE.SPRITE] = blockType; //TILE is an enum and SPRITE = 0
show_debug_message(oRender.map);
}
 

olavman

Member
try show_debug_message and show us your results please
GML:
if (mouse_check_button_pressed(mb_left))
{
show_debug_message(oRender.map);
oRender.map[gridX][gridY][gridZ][TILE.SPRITE] = blockType; //TILE is an enum and SPRITE = 0
show_debug_message(oRender.map);
}
The result was quite messy and big so i made the macros: MAP_W and MAP_H = 2 instead and this was the result:
[ [ [ [ 0,0,0,0 ],[ 0,0,0,0 ],[ 0,0,0,0 ],[ 0,0,0,0 ] ],[ [ 0,0,0,0 ],[ 0,0,0,0 ],[ 0,0,0,0 ],[ 0,0,0,0 ] ] ],[ [ [ 0,0,0,0 ],[ 0,0,0,0 ],[ 0,0,0,0 ],[ 0,0,0,0 ] ],[ [ 0,0,0,0 ],[ 0,0,0,0 ],[ 0,0,0,0 ],[ 0,0,0,0 ] ] ] ]
[ [ [ [ 35,0,0,0 ],[ 0,0,0,0 ],[ 0,0,0,0 ],[ 0,0,0,0 ] ],[ [ 35,0,0,0 ],[ 0,0,0,0 ],[ 0,0,0,0 ],[ 0,0,0,0 ] ] ],[ [ [ 35,0,0,0 ],[ 0,0,0,0 ],[ 0,0,0,0 ],[ 0,0,0,0 ] ],[ [ 35,0,0,0 ],[ 0,0,0,0 ],[ 0,0,0,0 ],[ 0,0,0,0 ] ] ] ]
 

gnysek

Member
I've mentioned it few days ago in profile posts, but:

var a = aarray_create(50, []); creates only two arrays, and put 50 same references of [] into every field of a. It does same as:

GML:
var b = [];
var a = array_create(50, undefined);

for(var i = 0; i < 50; i++) {
  a[i] = b;
}
So, in your code, you have only 4 arrays in fact.

What you probably want is:

GML:
var a = array_create(MAP_W, undefined);

for(var i = 0; i < MAP_W; i++) {
  a[i] = array_create(MAP_H, undefined);
  for(var j = 0; j < MAP_H; j++) {
      a[i][j] = array_create(MAP_D, undefined);
      for(var k = 0; k < MAP_D; k++) {
          a[i][j][k] = array_create(4, 0);
      }
  }
}
 

olavman

Member
I've mentioned it few days ago in profile posts, but:

var a = aarray_create(50, []); creates only two arrays, and put 50 same references of [] into every field of a. It does same as:

GML:
var b = [];
var a = array_create(50, undefined);

for(var i = 0; i < 50; i++) {
  a[i] = b;
}
So, in your code, you have only 4 arrays in fact.

What you probably want is:

GML:
var a = array_create(MAP_W, undefined);

for(var i = 0; i < MAP_W; i++) {
  a[i] = array_create(MAP_H, undefined);
  for(var j = 0; j < MAP_H; j++) {
      a[i][j] = array_create(MAP_D, undefined);
      for(var k = 0; k < MAP_D; k++) {
          a[i][j][k] = array_create(4, 0);
      }
  }
}
This fixed it!! Thank you so much! :D
 
Top