Hi,
The following code gives me the error ‘Object’ does not support slicing :
var ships : Hashtable=new Hashtable();
ships[‘Fighter’]=new Hashtable();
ships[‘Shark’]=new Hashtable();
ships[‘Fighter’][‘Power’]=2;
ships[‘Fighter’][‘Health’]=100;
ships[‘Shark’][‘Power’]=3;
ships[‘Shark’][‘Health’]=85;
How exactly do I make proper “multidimensional arrays” work in UnityScript? I come from web development so having problems with such simple task is quiet new to me.
I found a way where you have to declare evey hashtable as hashtable inline when reading the values, but is there really no CLEAN way of creating deep key+value arrays?
var ships = {‘Test’ : {‘Test1’ : ‘Test’}};
// This works but is not clean at all
print((this.ships[‘Test’] as Hashtable)[‘Test1’]);
Yes, removing #pragma strict fixes this, but again, not a clean way of doing things.
Thanks for any help!
I don’t know the exact syntax of UnityScript. But I can answer for C#:
The most common Data type for key-value types are Dictionaries. Since C# (and UnityScript also I think) is type safe language, you have to define which types you want to store as key / value. (there are some non-typed dictionaries as well like Hashtable but you should use them only if you really have different types as keys / values).
here is how you define a dictionary:
Dictionary<string, int> myDictionary = new Dictionary<string, int>();
you want nested dictionaries:
Dictionary<string, Dictionary<string, int>> ships = new Dictionary<string, Dictionary<string, int>>();
ships["Fighter"] = new Dictionary<string, int>();
ships["Shark"] = new Dictionary<string, int>();
ships["Fighter"]["Power"] = 2;
ships["Fighter"]["Health"] = 100;
ships["Shark"]["Power"] = 3;
ships["Shark"]["Health"] = 85;
PS: In your case it would be better to use an own class for Ship and store it as a Dictionary<string, Ship>
PPS: Instead of strings as identifiers it is always a good idea to use Enums instead. You cannot make mistakes so easily if you use enums.
2 Likes
Well that’s C# so that does not help me much.
Why is UnityScript boycoting JavaScript’s native objects?
var ships = {};
ships[‘Fighter’] = {};
ships[‘Shark’] = {};
ships[‘Fighter’][‘Power’]=2;
ships[‘Fighter’][‘Health’]=100;
ships[‘Shark’][‘Power’]=3;
ships[‘Shark’][‘Health’]=85;
This is a normal JS functional code but does not work in Unity, why?
Because Unity doesn’t use Javascript.
by the way: C# is much cleaner in my opinion than UnityScript. Also much more people are using it. And another benefit: you can do more with it than writing scripts for Unity.
So I recommend to learn C# instead of UnityScript. It is a beautiful language.
Or you could just continue using Unityscript, and use nested Dictionaries.
var ships = new Dictionary.< String, Dictionary.<String, int> >();
Don’t use any untyped collections, such as Hashtable, ArrayList, or JS arrays. Always use typed collections such as Dictionary, List, and array[ ]. And yes, it would be better to make a Ship class instead, and use enums rather than strings.
–Eric