- Home /
UnityScript equivalent of JavaScript objects
In JavaScript you can use objects as simple dictionaries, and there is a concise syntax for expressing object literals.
E.g.
var someLookUp = {
foo: 2,
bar: {
a: 1,
b: 2,
c: 3
}
};
someLookUp['bar']['b']; // 2
How can I do these kinds of things with UnityScript?
Answer by Eric5h5 · Apr 19, 2012 at 08:01 PM
Maybe you're looking for classes and/or structs?
class Bar extends System.ValueType { // struct
var a : int;
var b : int;
var c : int;
function Bar (a : int, b : int, c : int) {
this.a = a; this.b = b; this.c = c;
}
}
class Something {
var foo : int;
var bar : Bar;
function Something (foo : int, bar : Bar) {
this.foo = foo; this.bar = bar;
}
}
// ...
var lookup = new Something (2, Bar(1, 2, 3));
print (lookup.bar.b);
I had not encountered System.ValueType
and this is useful to know, however what if I wanted to look up an object member by key? You example uses lookup.bar.b
but is there a way to access these, or an alternative, whereby a variable key can be used e.g. if my example had been var key = 'foo'; someLookUp[key]; // 2
Answer by Mr Brent · Oct 24, 2013 at 03:52 AM
I realize this question is old as dirt, but what you're looking for are Hashtables.
http://docs.unity3d.com/412/Documentation/ScriptReference/Hashtable.html
Generic Dictionaries, rather. Dictionary does everything Hashtable does, except better/faster/etc.
They aren't really equivalents of JOs, as you have to define the type of variables inside, where you don't with a Hashtable. If you have a single type, then yes, go for it, Dictionaries are better.
You don't have to define the type, or rather you can use Object as the type, which amounts to the same thing. It's rare that you'd actually do that however; normally you use a consistent type. The reasons you shouldn't use Hashtable are 1) Dictionary has more functionality and is faster, 2) since Hashtable is untyped, you need to cast values when retrieving them (which also applies to Dictionary if you use Object as the type), 3) if you care about WP8 or Windows store publishing, you can't use obsolete collections such as ArrayList and Hashtable, since they're not supported on those platforms.
A Hashtable isn't a direct equivalent to a Javascript object either, so it's best avoided considering the downsides.
Thankyou so much, you've just saved me a massive ballache. Why in the name of god are unity putting this in their own tutorials? This is what it says:
//JavaScript does not have a List or Dictionary in the same
//way that C# does. Ins$$anonymous$$d, JavaScript has either non-typed
//'Objects' or an 'Array' object. You can acheive the same
//level of functionality using these, however the implementation
//is too different from this lesson to include here.
Yeah, that's just...wrong. I've submitted a bug report; it took maybe 2 $$anonymous$$utes to convert the scripts from C# to JS. (And in no way should they be recommending non-typed Objects or Array. That's web Javascript, not Unity.)
Your answer
Follow this Question
Related Questions
Closure does not work in Unity JS functions? 1 Answer
Collide detection with tag [JS] 0 Answers
What should i learn for unity C# or UnityScript? 4 Answers