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.
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.
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.