[SOLVED]Hashtables broken?

I think I found another bug, this one involves nested hashtables/dictionaries.

The following code works fine in regular Unity, but gives an error in Unity iPhone.

var dict = {"name":{"first":"John", "last":"Smith"}};
Debug.Log(dict["name"]["first"]);

Hopefully this is a known bug which will be fixed in 1.1, as I’ve pretty much GOT to have nested hashtables…

Try casting dict[“name”]…

(dict[“name”] as HashTable)[“first”]

No, that doesn’t work either.

I wondered if Unityscript might be getting confused by the colon. Rather than assigning the nested hashtable to name, it thinks I’m telling it what type “name” is.

But if that were the case, I would expect this to work:

var dict = {};
dict.Add("name", {"first":"John", "last":"Smith"});

Unfortunately, that doesn’t work either.

It’s not a bug, it’s to do with not having dynamic typing in Unity iPhone. If you put “#pragma strict” at the top of the script in regular Unity, you get the exact same error.

–Eric

I would still consider it a bug, since my examples are not using dynamic typing!

The following should also work, but doesn’t:

var nested = {"first":"John", "last":"Smith"};
var dict = {"name":nested};
dict = new Hashtable();
dict["name"] = new Hashtable();
dict["name"]["first"] = "John";

Smag, this actually works if “Hashtable” is capitalized correctly :), but it doesn’t work if the hashtables are more than 2 levels deep.

For instance, the following doesn’t work:

var dict = {"level1":{"part2":{"enemy": "soldier"}}}; 
var myenemy = (dict["level1"] as Hashtable)(["part2"] as Hashtable))["enemy"];
Debug.Log(myenemy);

Any idea how to make it work?

Thanks…

I never used Unityscript so it was just a guess… But I think your braces are incorrect… this may do the trick (not tested though):

var myenemy = ( (dict[“level1”] as Hashtable)[“part2”] as Hashtable)[“enemy”];

Also, a little question: does anyone know if c# has some kind of json-like object notation for creating hashtables? (ie the {[ ]}-stuff).

Smag, you rock!

It’s ugly, but it works! :slight_smile:

Maybe I could extend the Hashtable class so I could do something like this:

dict.GetValue("level1", "path1", "enemy");

I’m not much of a C# programmer, but all the examples I’ve seen on the web use the Add(“Key1”, “Value1”) notation. But I believe C# supports operator overloading, so you might be able to create something similar.