2D Array

I’m having trouble with the syntax in Unity’s version of javascript. I’m trying to create a 2d array for a grid of size (ROWS x COLS). Here’s what I’ve got so far. Any advice on where I’m going wrong would be helpful.

var ROWS : int;
var COLS : int;
var Maze = [[0], [0]];

function init_cells()
{
    var i : int;
    var j : int;

    // create a grid of cells
    Maze = new Array(ROWS);
    for (i = 0; i < ROWS; i++)
		Maze[i] = new Array(COLS);
}

This code returns an error on the last line (Grid*…) stating that it cannot convert ‘Array’ to ‘(int)’*

The fastest way to do it is to use a 1D array but treat it as 2D, like so:

var rows : int;
var cols : int;
private var maze : int[];

function Start () {
	maze = new int[rows*cols];
	// set a random column of each row to 1
	for (y = 0; y < rows; y++) {
		maze[y*cols + Random.Range(0, cols)] = 1;
	}
}

Although it uses a bit of math, this is a lot faster than using arrays of arrays.

–Eric

Just to lay it all out there:

Size of 1 dimensional array is width * height
or number_of_rows * number_of_columns

formula to get a particular x and y is:

[x + y * width]

or

[x + y * number_of_columns]

It seems weird at first but then you wonder why you ever cared. :slight_smile:

3 dimensions goes basically the same way:
size is width * height * depth
access like this:
[x + y * width + z * width * height]

This is thoroughly frustrating. I had this code working beautifully in Unity. Once I opened it in Unity iPhone, it exploded with 22 errors in one script alone. I guess that’s what I get for expecting code to function the same over two platforms.

Anyway, I need it as a 2d array because I access the dimensions independantly later in the code.

example:

                if ((Maze[r][c]  North)  (Maze[r][c]  East)  (Maze[r][c]  South)  (Maze[r][c]  West))
                {
                    // not visited, so add to possible next cells
                    next[j][0] = r;
                    next[j][1] = c;
                    j++;
                }

I’d rather not spam the forum with the entire 200 lines of code in this script, but I could zip it if that would help.

Sounds like a frustrating experience, but let me assure you:
The method Eric and I explained above is exactly the same as using a “two” dimensional array and there aren’t any limitations I know of and it is significantly faster than the method you described. Not sure how to better explain it.

It does mean changing your array code but it shouldn’t be too bad.

finding and replacing every “][” with " + number_of_columns * "
would do it for the accessing part. (one step)

por ejemplo:

	if ((Maze[r + number_of_columns * c]  North)  (Maze[r + number_of_columns * c]  East)  (Maze[r + number_of_columns * c]  South)  (Maze[r + number_of_columns * c]  West)) 
	{ 
		// not visited, so add to possible next cells 
		next[j + number_of_columns * 0] = r; 
		next[j + number_of_columns * 1] = c; 
		j++; 
	}

It’s because Unity iPhone doesn’t use dynamic typing. If you put “#pragma strict” on your script in regular Unity, it will also fail. I rarely advocate C# for anything ;), but in this case, if you really want 2D arrays without using the method described above, then C# is the way to do it. It’s only partially implemented in Javascript.

using UnityEngine;
using System.Collections;

public class Maze : MonoBehaviour {
	public int rows;
	public int cols;
	int[,] maze;
	
	void Start () {
		maze = new int[rows, cols];
		// set a random column of each row to 1 
		for (int y = 0; y < rows; y++) {
			maze[y, Random.Range(0, cols)] = 1;
		}
	}
}

Although, translating everything to C# would probably be harder than using the 1D array method.

–Eric

Thanks for the response, time, and patience. I’ve been dismantling and rebuilding my code to fit this new structure. It will take a while but I think I can get it.

Hey guys, I know this is a pretty old thread, but I have a question —

In theory – if I have a 3 rows * 3 columns, I’ll have a list of 9.
so, continuing with the example:

maze = new int[rows*cols];
maze = new int[9];

So if I’m trying to access spot [0,2] in this pseudoarray, it would go something like this?

maze[0,2] == maze[(row+columnnumberofColumns)] == maze[(0+29)] == 18 ???

It’s out of range of the array. I don’t quite understand — please forgive me if this is a very silly question.

numberofColumns is 3, not 9. (Also, it would be numberofRows rather than numberofColumns.)

Should be

rows = 3;
cols = 3;
maze = new int[rows * cols];
thisRow = 0;
thisCol = 2;
value = maze[thisRow + thisCol * rows];

–Eric

ah, of course, my bad.
Thank you for taking the time to respond.