Declaring and using array of arrays in Javascript

Hi,

I’m sorry if the question has been asked before, I searched through Google and the one link that seems to be used by everyone (unifycommunity) is dead right now.

Here’s the situation:
I’m coding in Javascript.

I’m using an array of int to define a map (the array can have a length of 4096 max.)
Now I want to give the possibility to undo or redo changes made to the map, up to 100 undos.

My idea of doing that was to declare an array (with a length of 100) containing arrays the size of the map.
Every time the user would validate a change, the map array would get copied in the undo array.
I think I saw someone on Answers saying that Javascript doesn’t allow you to declare an array like this:
var undoMap: int[][];
so I tried
var undoMap = new Array();
undoMap[0] = new Array();
I get no errors from the compilers doing that, but I can’t access the different elements. I tried doing it like this (map[] has been declared already):
for(i=0;i < map.Length;i++)
{
undoMap[0] = map*;*
}
But I get an error “Object reference not set to an instance of an object” so my guess is that I got the syntax wrong… Or maybe it’s not possible to do ?
Can anybody help me ? Or propose a better idea ? :wink:

What you are using here is JS array and should be avoided.

Better solution is built-in array. But for you, list would be more suitable since you are about to change size many times. List avoid the need of transfering the content of one array to another because it gets shrunk or bigger. Lists change size depending on how many objects it contains

var myList = new List.<Type>();

2D list goes like this I would think

var myList = new List.<List.<Type> >();

To be confirmed…

Hi, thanks for the answers !
I found the reason why it didn’t work: I simply forgot to declare the length of the array.
I’m aware of the limitations of the JS array and only used it as a last resort.

The List seems like the way to go, but I don’t really understand how they work.

This is what I get when I try to use it:
BCE0005: Unknown identifier: ‘List’.

Just a quick update:
I can’t get the List to work in my script, I don’t understand how they work. I reverted back to JS arrays, I’m ok with sacrificing performance in this context (a map editor) as the actual gameplay will use .NET arrays (faster than JS, did I get this right ?)

As an exemple, here’s the function which registers modifications of the map (map is an array which length is defined by two int: x and z):

function AddUndo()
{
 undoPos++;
 if(undoPos > maxUndo)
 {
 undoPos = 0;
 loopCount++;
 }
 mapUndo[undoPos] = new Array();
 o = map.Length + 3;
 mapUndo[undoPos].length = o;
 mapUndo[undoPos][0] = map.Length;
 mapUndo[undoPos][1] = x;
 mapUndo[undoPos][2] = z;
 o = 0;
 for (i = 3 ; i < (map.Length + 3) ; i++)
 {
 mapUndo[undoPos] *= map[o];*

o++;
}
}
It may not be very efficient, but it works. I set the limit of possible undos at 50, so even in a worst case scenario, my script would register 204800 int inside the mapUndo array.
Now of course, if someone has a more beautiful solution, I’ll gladly take it :slight_smile: