x


trouble with 2 dimensional arrrays in javascript

I am having trouble declaring and using a 2 dimensional array in javascript using unity 3.3

var x2: int[,]; no error but then errors later  
var x2:int[5,5]; //error excpecting semicolon at the end.

x2[4][4]=0;  

Debug.Log(x2[4][4]); //error BCE0109: Array 'self.x2' is rank '2', not rank '1'.and
                       type int does not support slicing.

So many errors? I'm following examples other people have posted about the multidimensional arrays in javascript in 3.2

Any ideas? thanks Dan

more ▼

asked Aug 02 '11 at 06:42 PM

dansav gravatar image

dansav
373 104 131 154

Please don't cross-post both here and on the forums.

Aug 02 '11 at 08:41 PM Eric5h5
(comments are locked)
10|3000 characters needed characters left

1 answer: sort voted first

Javascript 2d arrays are tricky. I ended up looking up C# 2d arrays and then moved the syntax to javascript and it worked. Here's what I found worked:

var myArray : int[,] = new int[5,5];
myArray[0,1] = 42;
for(var X : int = 0; X < myArray.Length(0); X++)
    for(var Y: int = 0; Y < myArray.Length(1); Y++)
    {
        myArray[X,Y] = X+Y;
        Debug.Log(myArray[X,Y]);
    }

That should be all the information you need to work with and loop through 2d arrays.

more ▼

answered Aug 02 '11 at 08:11 PM

SilverTabby gravatar image

SilverTabby
1.9k 3 6 18

Yep. That works. They should update the docs under arrays with your suggestions about how to use them.

Aug 03 '11 at 06:35 PM dansav

Fascinating that the "length" is a kind of a function across the dimensions!! Would never have guessed, and it's totally undocumented of course. Cheers.

Jun 02 '12 at 02:30 PM Fattie

The length calls in unityscript gives me... BCE0077: It is not possible to invoke an expression of type 'int'. What's up with that?

Mar 11 at 06:32 PM Jason.King

It should get GetLength, not Length.

Mar 11 at 08:48 PM Eric5h5

Thanks Eric5h5! GetLength compiles. But where is that documented? Even after knowing the proper semantics, I'm having trouble finding a definitive entry within language docs. I assume there may be other functionality attached to multidimensional arrays. Where can I find the complete set available to unityscript?

Mar 11 at 10:13 PM Jason.King
(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x1356
x61
x29
x21
x1

asked: Aug 02 '11 at 06:42 PM

Seen: 2545 times

Last Updated: Mar 12 at 12:27 AM