- Home /
Converting to Builtin Array for #pragma strict in javascript
Hi All
How do I convert this js script to be #pragma strict friendly?
I keep getting the error the 'Object' does not support slicing. So I understand that the object in the array should be predefined to a string so that when it is pulled out the program knows how to treat it. All my efforts have failed please help:) Thanks!
scores = new Array(); // All scores are in this array - new is used to reset array
scores.Push(new Array()); // Create a new array in a array
// scores => new array
scores[0].Push(new Array()); // Now add another array in to the 0 index place of an last array, this array has index 0
scores[0].Push(new Array()); // Now add another array, this array has index 1
scores[0][1].Push(PlayerPrefs.GetString("sc"+cp.ToString()+" -"+lvl.ToString()+"-1 - Time","9999.99"));
scores[0][1].Push(PlayerPrefs.GetString("sc"+cp.ToString()+" -"+lvl.ToString()+"-1 - Name","New Player"));
scores[0][1].Push(PlayerPrefs.GetString("sc"+cp.ToString()+" -"+lvl.ToString()+"-1 - i",""));
scores[0][1].Push(PlayerPrefs.GetString("sc"+cp.ToString()+" -"+lvl.ToString()+"-1 - q",""));
scores[0][1].Push(PlayerPrefs.GetString("sc"+cp.ToString()+" -"+lvl.ToString()+"-1 - a",""));
scores[0][1].Push(PlayerPrefs.GetString("sc"+cp.ToString()+" -"+lvl.ToString()+"-1 - c",""));
scores[0][1].Push(PlayerPrefs.GetString("sc"+cp.ToString()+" -"+lvl.ToString()+"-1 - k",""));
Answer by Eric5h5 · May 31, 2013 at 01:45 PM
Don't ever use the JS Array class. Only use built-in arrays or generic Lists; that way there won't be issues with casting, plus built-in arrays and Lists are faster and have more features.
Hi Eric I've seen you post this before in other related questions, but the problem is i have all my playerpref write/reads in this array format and i'd like to keep using arrays if possible. As you say, to get it in built-in format will be better and #pragma friendly.. but i'm struggling mate..
You really need to get rid of Array. It's quite simple to convert Array to List; see the link I posted for some more info.
i'll have a read. Will this definitely work with #pragma strict? cheers
Yes, the point of generic Lists is that they can only use a particular type.
got it working with generic lists. Now to convert the other 2000 lines:( Thanks mate!
Answer by PAHeartBeat · May 31, 2013 at 01:00 PM
try this
scores[0,0] = new Array(); // Now add another array in to the 0 index place of an last array, this array has index 0
scores[0,1] = new Array(); // Now add another array, this array has index 1
scores[0,1].Push(PlayerPrefs.GetString("sc"+cp.ToString()+" -"+lvl.ToString()+"-1 - Time","9999.99"));
thanks PAHeartBeat but how do I read from from this? Currently, I use this code to read from my score playerprefs:
var time = scores[0][1][0];
var playerName = scores[0][1][1];
//etc