Tri dimensional arrays

Hello

Is it possible to create and manipulate a tri-dimensional array in javascript for the iPhone?

The idea is to write and read a data structure like this… Data[×][y][z]

Thanks

Unity’s JavaScript implementation doesn’t allow for multidimensional or jagged arrays (yet). It’s why I switched to C# one day, a few months ago, after using JS for a couple years, actually.

You can use them, you just can’t directly declare them for some reason. But see the Javascript note here for a workaround using type inferencing. Also you can use arrays of Arrays in Javascript with no issues, aside from the fact that dynamic arrays are slower.

–Eric

Wow, that’s really goofy. :?

Bug/oversight I guess, since there are no issues working with them once you manage to set them up, so clearly the necessary bits are in place.

–Eric

Hello

Thank you for the Wiki link. It is very explanatory.

The problem was solved by implementing the solution at the end of the article: Using a C definition of the multi-dimensional array, and then declaring it on the Java scripts.

Hello again

As explained before, this solution is implemented for the iPhone (Unity 1.5).

It took several hours to identify a crash error when running it on the iPhone and coincidentally it boils down to the MultiDim declaration.

A C script placed on the Standard Assets folder is as follows:

using UnityEngine;

public class MultiDim : MonoBehaviour {
    public static int[,,] IntArray3D (int x, int y, int z) {
        return new int[x,y,z];
    }
}

And the declaration on the JS script is as follows:

var foo = MultiDim.IntArray3D(10,10,10);

The iPhone crashes when it runs the JS script. And Xcode shows the following line… GDB: Program received signal: “SIGABRT”

Sidenote :

Technically, according to MSDN docs, array[×][y][z] is called (tri) jagged array, not a multidimensional array.

multidimensional is array[x,y,z].

Thanks n0mad

Do you know how to create a jagged array [×][y][z] that works on the iPhone?

What are you trying to accomplish? Nested arrays can get ugly on the iPhone:

//create nested array
var nestedArray = Array();
nestedArray.Add( Array() );
(nestedArray[0] as Array).Add( Array() );

//add element at 0,0,0
((nestedArray[0] as Array)[0] as Array).Add("element 0,0,0");

//print element at 0,0,0 to console
Debug.Log(((nestedArray[0] as Array)[0] as Array)[0]);

I would recommend that you figure out a different way to do what you need. Either create your own data structure or use a hashtable.

Bleck!

I never used JavaScript arrays even when I was using Unity’s JavaScript. Just make good use of CopyTo, and I think you’ll have a lot cleaner results. I do.

Thanks tonyd

The last line of your code needs to be replaced by:

Debug.Log(((nestedArray[0] as Array)[0] as Array)[0]);

What I am trying to do is to port our current MacJong game to the iPhone. What we currently have is a jagged array (did not now of multidim before) that stores the value of each tile, the x,y,z relate to the position of each tile over the table.

Thanks, I fixed it!

If you’re simply storing x, y, and z values, the easiest way would be to use an array containing vector3’s.

var tiles : Vector3[];
tiles = Array(Vector3(10,10,10), Vector3(20,10,10), Vector3(30,10,10));
Debug.Log(tiles[2].x); //Print last tile's x value

Hello tonyd

I am not storing the x,y,z values. What I am using is a tri-dimensional grid accessed via x,y,z with the values of each tile on each position.

I followed your link, I get what you’re going for now.

You could always use the ‘as array’ method, but write a wrapper function to do the work for you. Something like:

function Get(myArray, x, y, z){
    return ((myArray[x] as Array)[y] as Array)[z]; //might want to it return as String
}

The call it with:

tileType = Get(tiles, 10, 4, 1);

BTW, that’s one impressive looking Mahjong game! I’ve thought about doing one myself, but mine wouldn’t look that good…

Thanks for the comment tonyd

It took quite some time but all the code is now using your suggestion. There is no need to use the wrapper function.

A multidimensional 3D array would in fact be the most efficient way to do this. But you’re right, it seems to generate an error on the device, although it works in the editor. A 2D array works fine on the device, so this must be a bug, which I’m reporting.

–Eric

(Also, jagged arrays are faster than multidim arrays.)

It’s the other way around: “rectangular arrays are generally superior to jagged arrays in terms of structure and performance”. Also they are easier to deal with.

–Eric

Argh, which one to believe ? :wink: