• 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 using 2d arrays wrong?

RizbIT

Member
Code:
global.item_list=ds_list_create();
ds_list_add(global.item_list,"fireball");
ds_list_add(global.item_list,"icebolt");
ds_list_add(global.item_list,"smoke");

global.item_stats[0,1]=1;
global.item_stats[0,2]=2;
global.item_stats[1,1]=3;
global.item_stats[1,2]=4;
global.item_stats[2,1]=5;
global.item_stats[2,2]=6;

 var i;
  
 var list1n  = ds_list_size(global.item_list);

 for (i=0; i<list1n; i+=1)
  {
  txt[i]=ds_list_find_value(global.item_list,i);
 
  txt[i,1]="Power: "+string(global.item_stats[i,1])+"  -  Range: "+string(global.item_stats[i,2]);
  }
Now the problem is this:
show_message(txt[0,1]) shows value as "fireball" which is wrong as it should show
"Power: 1 - Range: 2"

But strangly show_message(txt[1,1]) does show the right value as
"Power: 2 - Range: 3"

Is this a bug or wrong usage?
 

Simon Gust

Member
Code:
global.item_list=ds_list_create();
ds_list_add(global.item_list,"fireball");
ds_list_add(global.item_list,"icebolt");
ds_list_add(global.item_list,"smoke");

global.item_stats[0,1]=1;
global.item_stats[0,2]=2;
global.item_stats[1,1]=3;
global.item_stats[1,2]=4;
global.item_stats[2,1]=5;
global.item_stats[2,2]=6;

 var i;
 
 var list1n  = ds_list_size(global.item_list);

 for (i=0; i<list1n; i+=1)
  {
  txt[i]=ds_list_find_value(global.item_list,i);
 
  txt[i,1]="Power: "+string(global.item_stats[i,1])+"  -  Range: "+string(global.item_stats[i,2]);
  }
Now the problem is this:
show_message(txt[0,1]) shows value as "fireball" which is wrong as it should show
"Power: 1 - Range: 2"

But strangly show_message(txt[1,1]) does show the right value as
"Power: 2 - Range: 3"

Is this a bug or wrong usage?
you are forced to treat 2d Arrays as 2d Arrays and not as 1d Arrays.
in the first line you set txt which is how you would set a 1d Array.
But on the second line you set txt[i, 1] which is how a 2d Array is set.
2d Arrays aren't actually 2d, they are more like 1d Arrays in 1d Arrays but accessible like a grid.

If I use this code and print the txt array
Code:
show_debug_message(txt);
I get the Array with some undefined entries, in your case probably the name of the item.
Why don't you add this little code at the end and see what it prints in your console.
 
Top