Can't access specific element in multidimensional array (JavaScript)
Hello, i cannot access a specific element in a multidimensional array, all i have in return when i debug is an empty object ( [ ] ).
This is what i am doing:
1- create a series of scores formed of two elements ( a numeric a string ("1.0, 1.2, 1.6, 0.7" etc) and an integer number).
var score1 = [string,number]; for example ["1.0, 1.2, 1.6, 0.7 " , 154]
var score2 = [string,number];
var score3 = [string,number];
...and so on.
I then group those score under another array:
var scorepattern1 = [[score7],[score5],[score3]];
var scorepattern2 = [[score6],[score4],[score2]];
var scorepattern3 = [[score5],[score3],[score1]];
i create another array containing the pattern i created above:
var scorepatternlibrary = [[scorepattern1], [scorepattern2], [scorepattern3]];
and finally i pick a random pattern:
var scorepatternToUse = scorepatternlibrary[Math.floor(Math.random() * scorepatternlibrary.length)];
Now, in this example, let's assume the randomly chosen pattern is scorepattern 3. I want to access the score at index 2 ([score1]), and get the parameter at index 0 (the string), so "1.0,1.2,1.6, 0.7 ".
What i am doing is:
Debug.Log(patternToUse[2[0]];
Am i doing wrong? What is the right sintax to access a specific value in an array located in another array? Is this approach the correct one?
P.S. I am using GameSpark CloudCode (that uses JS), but the sintax might be a bit different. I am also new to jscript so i might be using unconventional approaches...be patient!
Thanks for your help!
Answer by hexagonius · Jun 25, 2017 at 10:33 AM
The official Microsoft MSDN documentation helps a lot:
https://docs.microsoft.com/en-us/dotnet/csharp/programming-guide/arrays/jagged-arrays
$$anonymous$$y code is in Java, not C#, Is there a guide detailed as that one using js?
Thanks
Not really. C# is the actual standard now and it's recommended to learn it. Not to hard co$$anonymous$$g from UnityScript. But this answer has examples and following the syntax you should be able to figure out how deeper accesses work
http://answers.unity3d.com/questions/54695/how-to-declare-and-initialize-multidimensional-arr.html
I am more confident with C# but the GameSparks service i use for the multiplayer features of my game rely on JS. Not much i can do on that side really.
Your answer
Follow this Question
Related Questions
How to make a player move up then forward using ontrigger enter collider? 0 Answers
Top Down Game - Camera Follow Player and General Scripting Question 0 Answers
Why can't I move player's game piece on 2nd turn? 0 Answers
Unity doesn't recognize Java version on Mac 0 Answers
OnCollisonEnter2D Not Firing after checking collider 1 Answer