|
I'm trying to instantiate a grid using cubes with this shape
I'm trying to use a multidimensional array for this but I have problems initializing the array with set values.
Above is the first method I've tried but it throws the following error
I've also tried storing an array in a array
But this shows the following error:
I suspect a syntax error but I haven't been able to find what. Thanks in advance.
(comments are locked)
|
|
Declaring an array the way you did initially results in a jagged array. If you leave off the type, type inference will type it as a jagged array automatically:
(The type for this is int[][], not Array.) Then you can do
instead of
Jagged arrays aren't quite the same thing as multidimensional arrays, but it would work well enough. If there's an easy syntax to initialize multidimensional arrays in JS by filling them with initial values, I don't know what it is. If you really need a "proper" multidimensional array, you might have to do it the hard way: Thanks. This at least got me as far as that I am able to replicate my image with cubes. So type inferencing will make it a jagged array. But if I set the type as int[][] I get an error. So I can't set the type myself?
Mar 27 '11 at 05:45 PM
Joeri
For jagged arrays you can't explicitly set the type yourself in JS, which is an oversight, but it's irrelevant whether the type is derived from type inferencing or explicitly stating it--the compiled code is exactly the same either way. Note that type inferencing has nothing to do with dynamic typing, which is a different subject entirely, if that's what you're concerned about.
Mar 27 '11 at 06:03 PM
Eric5h5
a somewhat similar problem .. ? http://answers.unity3d.com/questions/286682/2d-array-persists-during-multiple-game-sessions.html
Jul 19 '12 at 04:27 PM
Fattie
(comments are locked)
|
You can also turn this into a for loop to set the values. Thanks! in my case I could not use either jagged arrays, nor Array or List classes, and it kept telling me I needed a semicolon. On the other hand, if I just said boolean[,], not all of my GUI would show up and none of the script would work. I never thought of this. Thanks!
Jan 27 at 10:59 PM
Zarenityx
(comments are locked)
|
|
Nikhilesh Kumar Patidar(Asp.net code):-
(comments are locked)
|
|
Nikhilesh Patidar:- function displayResult() { var myarray=new Array(4); var myarray1=new Array(4); myarray1[0]="Apple"; myarray1[1]="Pear"; myarray1[2]="Banana"; myarray1[3]="Orange"; var L=100; for(i =0; i<myarray1.length;i++) { myarray[i] = new Array(2); myarray[i][0]=myarray1[i]; myarray[i][1]=L+100; L=L+100; } var x=document.getElementById("mySelect"); var a=document.getElementById("Select1"); var option=document.createElement("option"); var b=''; var k; option.text="450"; var d=0; var g=0; d=x.selectedIndex; b=x.options[d].value; var n=0; var m=''; var c=''; for (i=0;i<=a.length+1;i++) { a.remove(a.options[i]); }
(comments are locked)
|
|
JavaScript doesn't actually have support for multi-dimensional arrays, but you can nest arrays. For most programming purposes, nested arrays are equivalent to multi-dimensional arrays. Therefore we are not going to worry about the differences between them here. To create a nested array, you just define an array element as being an array.
When using array literals, the same effect can be achieved by nesting square brackets. Thanks a lot for the fast reply. I read that 3.2 added the support for multidimensional arrays. I have been able to use it to instantiate a grid of cubes using a nested for loop. Just haven't been able to initialize it with set values. I see your using the javascript Array class in this example. How could I use this with build in arrays and what's the syntax for this? I read those are much faster then javascript arrays.
Mar 27 '11 at 04:31 PM
Joeri
JS does have support for multidimensional arrays. There's very little reason to ever use the Array class; built-in arrays or generic Lists are almost always a better idea.
Mar 27 '11 at 05:15 PM
Eric5h5
My bad :-( Guess my JS knowledge is a bit out of date then.
Mar 27 '11 at 05:24 PM
Meltdown
That's why I asked. Regardless though I'm having trouble accessing the arrays in this example. var outerA = [ [0,1,1,0], [1,0,0,1], [1,0,0,1], [0,1,1,0] ]; How would I go about accessing this in a for loop?
Mar 27 '11 at 05:29 PM
Joeri
Just to be nitpicky, JS always had support for multi-d arrays, but it wasn't possible to declare the type explicitly until Unity 3.2. This meant it was possible to use various workarounds involving type inference; see here: http://www.unifycommunity.com/wiki/index.php?title=JavascriptMultiDimArrays
Mar 27 '11 at 05:44 PM
Eric5h5
(comments are locked)
|

