1

I'm following this unity tutorial, but converting the script to UnityScript as I go.

Mostly it has been fine, but this line is throwing me.

Color[][] tiles = new Color[numTilesPerRow*numRows][];

I believe I should be doing something like this, but it isn't working.

var tiles = new Color[numTilesPerRow*numRows][];

I get an error:

';' expected. Insert a semicolon at the end.

Edit:

Here is the function I am converting in full:

Color[][] function ChopUpTiles() {
    int numTilesPerRow = terrainTiles.width / tileResolution;
    int numRows = terrainTiles.height / tileResolution;

    Color[][] tiles = new Color[numTilesPerRow*numRows][];

    for (int y=0; y<numRows; y++) {
        for (int x=0; x<numTilesPerRow; x++) {
            tiles[y*numTilesPerRow + x] = terrainTiles.GetPixels(x*tileResolution, y*tileResolution, tileResolution, tileResolution);
        }
    }

    return tiles;
}

Edit 2:

I have worked out how to get it to work, but I get a downcast warning:

var tiles = new Array();

Does the job, but the problem is that I don't know to imply that this is an array of color arrays I get a downcast warning.

CodeSmile
  • 64,284
  • 20
  • 132
  • 217
SystemicPlural
  • 5,629
  • 9
  • 49
  • 74
  • Well, according to the information in the links in my answer, multi-dimensional arrays can be declared now, but jagged arrays are still missing. So you're stuck with writing them in C#, or not using them at all. Why do you use Javascript anyway? C# is better supported and you can use Visual Studio, which is awesome :P Also, I'm not sure about this yet, but it seems that Unity compiles the JS into .NET anyway, so it's mostly about the syntax. – Luaan Dec 10 '13 at 09:48
  • Just out of curiosity, where is the discussion of jagged arrays coming from? Array size will always be `tiles[numTilesPerRow*numRows][tileResolution*tileResolution]`. – Jerdak Dec 10 '13 at 16:26

2 Answers2

1

Jagged arrays don't work like this in Javascript.

Have a look at this solution: http://answers.unity3d.com/questions/54695/how-to-declare-and-initialize-multidimensional-arr.html

Basically, get an array, and then initialize each of the indices to a new array.

Of course, if it's statically initialized, you can use something like this:

[[1, 3, 4], [1, 5, 5], ... ]

Is there a reason why you're using a jagged array rather than a simple multi-dimensional array?

Luaan
  • 62,244
  • 7
  • 97
  • 116
  • Your example you linked is doing the reverse of what I am doing. Whatever I try I always get either an error or a downcast warning. The array is not statically initialized, the data is being stripped from an image texture. I've included the full c# code in an edit to my question. – SystemicPlural Dec 10 '13 at 09:06
  • @SystemicPlural In that case, you're probably going to have to cheat a bit - http://wiki.unity3d.com/index.php?title=JavascriptMultiDimArrays. Unity Javascript apparently *just can't do it*. Even that will not allow you to *return* the array, so you're still screwed. I'd just use C# for this little snippet, and you should be fine. – Luaan Dec 10 '13 at 09:21
  • Luaan, Microsoft is recommending jagged arrays over multidimensional arrays for performance reasons. For example this ['warning'](http://msdn.microsoft.com/en-us/library/ms182277.aspx) and this [question](http://stackoverflow.com/questions/597720/what-is-differences-between-multidimensional-array-and-array-of-arrays-in-c) – Daan Timmer Dec 10 '13 at 09:24
  • @DaanTimmer: Yes, I'm aware of that. But I have no idea how Javascript works in Unity, so it may or may not be relevant. I'm not questioning the use of jagged arrays for texture data in C#. I'm talking about the Javascript version, which may very well have a completely different memory manager. Of course, some performance reasons remain, but it's all up to Unity's JS engine. – Luaan Dec 10 '13 at 09:29
  • @Luaan Oke, my bad, I thought you were questioning his use of jagged arrays within C#. – Daan Timmer Dec 10 '13 at 09:37
0

I managed to prevent the downcast warning by assigning the color array to a variable first. No idea why this is acceptable when direct assignment isn't.

function ChopUpTiles() {
    var numTilesPerRow = terrainTiles.width / tileResolution;
    var numRows = terrainTiles.height / tileResolution;

    var tiles = new Array();

    for(var y : int = 0; y < numRows; y++) {
        for(var x : int = 0; x < numTilesPerRow; x++) {
            var tileColors = terrainTiles.GetPixels(
                x*tileResolution, 
                y*tileResolution, 
                tileResolution, 
                tileResolution
            );
            tiles[y*numTilesPerRow + x] = tileColors;
        }
    }
    return tiles;
}
SystemicPlural
  • 5,629
  • 9
  • 49
  • 74