# Hashtables

**URL:** https://discussions.unity.com/t/hashtables/423449
**Category:** Scripting
**Tags:** Scripting
**Created:** [September 20, 2010, 12:50pm UTC](https://discussions.unity.com/t/hashtables/423449 "2010-09-20T12:50:03Z")
**Posts on this page:** 20
**Page:** 1

<div class="post-metadata">

### Author: ![AkilaeTribe](https://dub1.discourse-cdn.com/unity/user_avatar/discussions.unity.com/akilaetribe/32/153898_2.png) [@AkilaeTribe](https://discussions.unity.com/u/AkilaeTribe)
#### Post date: [September 20, 2010, 12:50pm UTC](https://discussions.unity.com/t/hashtables/423449/1 "2010-09-20T12:50:03Z")

</div>

Greetings,

I am discovering the joy of hashtables, but something is on my mind.

Is there a way to instantiate / create a hashtable and instantly give it values ?

For example, when you want to make a second array based on a previous array, you can write array2 = new Array (array1);  
Or when you want to create an array with already existing values, you write list = new Array (1, 10, 100);

I can’t manage to do the same with hashtables. Must I really write table = new Hashtable (); and give it values after that (table.Add ()) ?

---

<div class="post-metadata">

### Author: ![appels](https://dub1.discourse-cdn.com/unity/user_avatar/discussions.unity.com/appels/32/495192_2.png) [@appels](https://discussions.unity.com/u/appels)
#### Post date: [September 20, 2010, 1:02pm UTC](https://discussions.unity.com/t/hashtables/423449/2 "2010-09-20T13:02:16Z")

</div>

i think so yes, look for the java or C# command ref.

---

<div class="post-metadata">

### Author: ![AkilaeTribe](https://dub1.discourse-cdn.com/unity/user_avatar/discussions.unity.com/akilaetribe/32/153898_2.png) [@AkilaeTribe](https://discussions.unity.com/u/AkilaeTribe)
#### Post date: [September 20, 2010, 1:22pm UTC](https://discussions.unity.com/t/hashtables/423449/3 "2010-09-20T13:22:28Z")

</div>

> **[Hashtable Class (System.Collections)](https://learn.microsoft.com/en-us/dotnet/api/system.collections.hashtable?view=net-9.0&redirectedfrom=MSDN)**
>
> Represents a collection of key/value pairs that are organized based on the hash code of the key.

> **[Hash Tables in Javascript](http://www.mojavelinux.com/articles/javascript_hashes.html)**

Even in the examples, it first instantiates, then defines by code the content.

My main problem is that the content must be initialized by code (Awake or Start, then). When I worked with LUA, I remembered I could write something like that, upon declaring a variable :

```auto
var table = 
{
    "keyOne" = "valueOne",
    "keyTwo" = "valueTwo",
    ...
}

```

> [@](#):
>
> java

…script ? (Javascript)

> [@](#):
>
> ref

I don’t want to look dumb, but I guess it can’t be helped : I don’t see how ref could help. From the old posts, I understood it is used with an argument of a function, and allow to pass the variable reference as the argument, instead of its value. I highly doubt it would help me in defining the content of a hashtable upon its creation.

---

<div class="post-metadata">

### Author: ![AkilaeTribe](https://dub1.discourse-cdn.com/unity/user_avatar/discussions.unity.com/akilaetribe/32/153898_2.png) [@AkilaeTribe](https://discussions.unity.com/u/AkilaeTribe)
#### Post date: [September 20, 2010, 1:37pm UTC](https://discussions.unity.com/t/hashtables/423449/4 "2010-09-20T13:37:05Z")

</div>

After a little more testing (in the dark), I managed to write something :

```auto
public var table : Hashtable = new Hashtable ({"test" : "sop", "test2" : "soppy"});

function Start ()
{
	Debug.Log (table ["test"]);
	Debug.Log (table ["test2"]);
}

```

It seems to works…

---

<div class="post-metadata">

### Author: ![Chris-Sinclair](https://dub1.discourse-cdn.com/unity/user_avatar/discussions.unity.com/chris-sinclair/32/241371_2.png) [@Chris-Sinclair](https://discussions.unity.com/u/Chris-Sinclair)
#### Post date: [September 20, 2010, 5:09pm UTC](https://discussions.unity.com/t/hashtables/423449/5 "2010-09-20T17:09:50Z")

</div>

That’s similar to the C# 3.0+ syntax for Dictionaries, so what you got is probably fine:

```auto
Dictionary<string, int> test = new Dictionary<string, int>() 
{ 
	{ "asdf", 1 }, 
	{ "zxcv", 2 },
	{ "qwerty", 3},
};

```

You’ll probably want to use a Dictionary instead of a Hashtable for type-safety. (though I don’t know how much that matters for a UnityScript version)

---

<div class="post-metadata">

### Author: ![AkilaeTribe](https://dub1.discourse-cdn.com/unity/user_avatar/discussions.unity.com/akilaetribe/32/153898_2.png) [@AkilaeTribe](https://discussions.unity.com/u/AkilaeTribe)
#### Post date: [September 20, 2010, 8:05pm UTC](https://discussions.unity.com/t/hashtables/423449/6 "2010-09-20T20:05:50Z")

</div>

Well, the type-safety is a plus (as long as the guy writing has the type in mind, no problem should arise).

I tried to give it a try, but it is not getting somewhere.

[http://rick.measham.id.au/javascript/hash.htm](http://rick.measham.id.au/javascript/hash.htm)

```auto
public var table : Hash = new Hash
({
	"test" : "sop"
});

function Start ()
{
	Debug.Log (table ["test"]);
}

```

Error message :

> [@](#):
>
> Assets/Hashtables/HashtablesScript.js(1,20): BCE0004: Ambiguous reference ‘Hash’: Boo.Lang.Hash, Boo.Lang.Hash.

Sigh…anyone have a good idea, please ? (I can still go on with Hashtable, but I would like to see the ending of this)

---

<div class="post-metadata">

### Author: ![Chris-Sinclair](https://dub1.discourse-cdn.com/unity/user_avatar/discussions.unity.com/chris-sinclair/32/241371_2.png) [@Chris-Sinclair](https://discussions.unity.com/u/Chris-Sinclair)
#### Post date: [September 20, 2010, 9:12pm UTC](https://discussions.unity.com/t/hashtables/423449/7 "2010-09-20T21:12:07Z")

</div>

“Hash” is not a built in type for JavaScript (let alone UnityScript, which apparently is reserved by Boo). Plus it’s a pretty piss-poor name for a HashTable class (it’s a HashTable, not a Hash).

Try using System.Collections.HashTable instead. Or you can use the UnityScript “Hashtable”: [Unity - Scripting API: Hashtable](http://unity3d.com/support/documentation/ScriptReference/Hashtable.html)

---

<div class="post-metadata">

### Author: ![tonyd](https://dub1.discourse-cdn.com/unity/user_avatar/discussions.unity.com/tonyd/32/437055_2.png) [@tonyd](https://discussions.unity.com/u/tonyd)
#### Post date: [September 20, 2010, 9:30pm UTC](https://discussions.unity.com/t/hashtables/423449/8 "2010-09-20T21:30:39Z")

</div>

Hashtables are built in to Unityscript.

Just do this:

> [@](#):
>
> myhash = {“name”:“john”, “age”:“24”, “sex”:“male”};

---

<div class="post-metadata">

### Author: ![tonyd](https://dub1.discourse-cdn.com/unity/user_avatar/discussions.unity.com/tonyd/32/437055_2.png) [@tonyd](https://discussions.unity.com/u/tonyd)
#### Post date: [September 20, 2010, 9:41pm UTC](https://discussions.unity.com/t/hashtables/423449/9 "2010-09-20T21:41:35Z")

</div>

And note that you don’t need to use Add() to add values.

You can assign values like so:

```auto
myhash["key"] = "value";

```

and retrieve values this way:

```auto
Debug.Log(myhash["key"]);

```

---

<div class="post-metadata">

### Author: ![AkilaeTribe](https://dub1.discourse-cdn.com/unity/user_avatar/discussions.unity.com/akilaetribe/32/153898_2.png) [@AkilaeTribe](https://discussions.unity.com/u/AkilaeTribe)
#### Post date: [September 20, 2010, 10:01pm UTC](https://discussions.unity.com/t/hashtables/423449/10 "2010-09-20T22:01:36Z")

</div>

?

I think I wasn’t clear : I know now how to use a Hashtable (from the blind tests, and, from now, your advices ;)). I am now trying to make a Dictionary, like FizixMan suggested (to lock the types).

In UnityScript ! 😄

---

<div class="post-metadata">

### Author: ![Chris-Sinclair](https://dub1.discourse-cdn.com/unity/user_avatar/discussions.unity.com/chris-sinclair/32/241371_2.png) [@Chris-Sinclair](https://discussions.unity.com/u/Chris-Sinclair)
#### Post date: [September 20, 2010, 10:39pm UTC](https://discussions.unity.com/t/hashtables/423449/11 "2010-09-20T22:39:10Z")

</div>

If I recall correctly, since UnityScript (in version 2.6.x) doesn’t support generics, you might be out of luck.

Although… _puts on his handy dandy reflection haxx0z00rz0r0z0x0rz0 cap_ brb

---

<div class="post-metadata">

### Author: ![AkilaeTribe](https://dub1.discourse-cdn.com/unity/user_avatar/discussions.unity.com/akilaetribe/32/153898_2.png) [@AkilaeTribe](https://discussions.unity.com/u/AkilaeTribe)
#### Post date: [September 20, 2010, 10:51pm UTC](https://discussions.unity.com/t/hashtables/423449/12 "2010-09-20T22:51:08Z")

</div>

The chicken walks away, sadly.

No one loves him … even UnityScript. ☹

---

<div class="post-metadata">

### Author: ![Chris-Sinclair](https://dub1.discourse-cdn.com/unity/user_avatar/discussions.unity.com/chris-sinclair/32/241371_2.png) [@Chris-Sinclair](https://discussions.unity.com/u/Chris-Sinclair)
#### Post date: [September 20, 2010, 11:06pm UTC](https://discussions.unity.com/t/hashtables/423449/13 "2010-09-20T23:06:01Z")

</div>

VICTORY

sort of

```auto
import System.Reflection;

function Start()
{

	var dictionaryType = System.Type.GetType("System.Collections.Generic.Dictionary`2[[System.String, mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089],[System.Int32, mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089]]");
	//shorter version should work fine as well
	//var dictionaryType = System.Type.GetType("System.Collections.Generic.Dictionary`2[[System.String, mscorlib],[System.Int32, mscorlib]]");
	var constructor = dictionaryType.GetConstructor(System.Type.EmptyTypes);
	var dynamicDictionary = constructor.Invoke(null);
	
	print("IS NULL: " + (dynamicDictionary == null));
	print("TYPE: " + dynamicDictionary.GetType().FullName);
	try
	{
	
		var addMethod = dictionaryType.GetMethod("Add");
		var arguments = new System.Object[2];
		arguments[0] = "asdf";
		arguments[1] = 1;
		addMethod.Invoke(dynamicDictionary, arguments);
		
		var countProperty = dictionaryType.GetProperty("Count");
		print("COUNT: " + countProperty.GetValue(dynamicDictionary, null));
	
		//all of the below caused unity to completely crash and burn, hard; try/catch had no effect
		//var count = dynamicDictionary.Count;
		//dynamicDictionary["asdf"] = 1;
		//dynamicDictionary.Add("asdf", 1);
		//dynamicDictionary.Add("qwerty", 2);
	
		//print("CHECK: " + dynamicDictionary.Count);
	}
	catch (ex)
	{
		print("ERROR: " + ex.Message + ", TRACE: " + ex.StackTrace);
	}
		
}

```

Bottom line: theoretically possible to write a UnityScript wrapper for generic dictionaries. However, performance will be abysmal with all the reflection hax. All members of the dictionary have to be accessed via reflection or Unity implodes. Furthermore, you pretty much lose the primary benefit anyway (type-safety code reusability through generics). For all the pain, better off to write a fixed-type wrapper around Hashtable, waiting for Unity 3, or switch to C#.

---

<div class="post-metadata">

### Author: ![tonyd](https://dub1.discourse-cdn.com/unity/user_avatar/discussions.unity.com/tonyd/32/437055_2.png) [@tonyd](https://discussions.unity.com/u/tonyd)
#### Post date: [September 20, 2010, 11:16pm UTC](https://discussions.unity.com/t/hashtables/423449/14 "2010-09-20T23:16:09Z")

</div>

Maybe I don’t understand what you guys are trying to accomplish, but if you’re just trying to create a dictionary class with fixed types, you can create a class that inherits from Hashtable, and only accepts/returns values that are strings (or whatever type you wish).

I’ve done this before to create an iPhone friendly hashtable, otherwise you have to cast everything ‘as String’.

---

<div class="post-metadata">

### Author: ![AkilaeTribe](https://dub1.discourse-cdn.com/unity/user_avatar/discussions.unity.com/akilaetribe/32/153898_2.png) [@AkilaeTribe](https://discussions.unity.com/u/AkilaeTribe)
#### Post date: [September 21, 2010, 9:09am UTC](https://discussions.unity.com/t/hashtables/423449/15 "2010-09-21T09:09:28Z")

</div>

> [@""](#):
>
> Maybe I don’t understand what you guys are trying to accomplish,

First, I was trying to make the Hashtable work, but FizixMan (always him) dangled the happiness before my eyes, and I was taken into the quest of grabbing it.

> [@""](#):
>
> but if you’re just trying to create a dictionary class with fixed types, you can create a class that inherits from Hashtable, and only accepts/returns values that are strings (or whatever type you wish).

Of course you can always cheat with Unity, but I’d rather love to use built in solutions 😉 (simpler and cleaner)

> [@""](#):
>
> ```auto
> import System.Reflection;
> 
> function Start()
> {
> 
> var dictionaryType = System.Type.GetType("System.Collections.Generic.Dictionary`2[[System.String, mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089],[System.Int32, mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089]]");
> //shorter version should work fine as well
> //var dictionaryType = System.Type.GetType("System.Collections.Generic.Dictionary`2[[System.String, mscorlib],[System.Int32, mscorlib]]");
> var constructor = dictionaryType.GetConstructor(System.Type.EmptyTypes);
> var dynamicDictionary = constructor.Invoke(null);
>    
> print("IS NULL: " + (dynamicDictionary == null));
> print("TYPE: " + dynamicDictionary.GetType().FullName);
> try
> {
>    
> var addMethod = dictionaryType.GetMethod("Add");
> var arguments = new System.Object[2];
> arguments[0] = "asdf";
> arguments[1] = 1;
> addMethod.Invoke(dynamicDictionary, arguments);
>       
> var countProperty = dictionaryType.GetProperty("Count");
> print("COUNT: " + countProperty.GetValue(dynamicDictionary, null));
>    
> //all of the below caused unity to completely crash and burn, hard; try/catch had no effect
> //var count = dynamicDictionary.Count;
> //dynamicDictionary["asdf"] = 1;
> //dynamicDictionary.Add("asdf", 1);
> //dynamicDictionary.Add("qwerty", 2);
>    
> //print("CHECK: " + dynamicDictionary.Count);
> }
> catch (ex)
> {
> print("ERROR: " + ex.Message + ", TRACE: " + ex.StackTrace);
> }
>       
> }
> 
> ```
> 
> Bottom line: theoretically possible to write a UnityScript wrapper for generic dictionaries. However, performance will be abysmal with all the reflection hax. All members of the dictionary have to be accessed via reflection or Unity implodes. Furthermore, you pretty much lose the primary benefit anyway (type-safety code reusability through generics). For all the pain, better off to write a fixed-type wrapper around Hashtable, waiting for Unity 3, or switch to C#.

:shock: _gaping_

The second. 😄

I tried to have a look anyway. You look like you are trying to retrieve the constructor of a type (that can change, explaining the looseliness of the variables type), retrieve its Add function, and add by it some arguments.

Geh…

> [@""](#):
>
> VICTORY

Yes and no …

> [@""](#):
>
> sort of

Yes 😄

It is so unreal I should keep that in mind, for later (?!) use. Thanks you all ! (a wink for FizixMan , heh ;))

---

<div class="post-metadata">

### Author: ![Chris-Sinclair](https://dub1.discourse-cdn.com/unity/user_avatar/discussions.unity.com/chris-sinclair/32/241371_2.png) [@Chris-Sinclair](https://discussions.unity.com/u/Chris-Sinclair)
#### Post date: [September 21, 2010, 12:26pm UTC](https://discussions.unity.com/t/hashtables/423449/16 "2010-09-21T12:26:26Z")

</div>

Yeah, that’s pretty much the jist of it. It’s relatively safe to use reflection when you know your types aren’t going to change (like the built-in .NET types, such as Dictionary).

> [@](#):
>
> var dictionaryType = System.Type.GetType(“System.Collections.Generic.Dictionary`2[[System.String, mscorlib],[System.Int32, mscorlib]]”);

This grabs a reference to the generic Dictionary\<string, int\> (string keys, integer values) by using the string representation of its type name. It looks kind of funky but that’s how generics type strings are formed.

> [@](#):
>
> var constructor = dictionaryType.GetConstructor(System.Type.EmptyTypes);  
> var dynamicDictionary = constructor.Invoke(null);

That looks for a parameterless constructor, then invokes it.

Then yeah, everything else you need to use reflection to grab the other methods and properties. If I try to use UnityScript’s dynamic lookup to access them (by just hitting up dynamicDictionary.Add(), dynamicDictionary.Count) I get an error with no message, then a second error that causes the Unity editor to crash and close.

It’s not type-safe at compile time because you could pass in an in compatible object (say, a boolean) via reflection, and have it throw a run-time error.

I’m 93.8% sure that I heard that Unity 3 gives UnityScript generics support. Hopefully that does comes to pass.

---

<div class="post-metadata">

### Author: ![AkilaeTribe](https://dub1.discourse-cdn.com/unity/user_avatar/discussions.unity.com/akilaetribe/32/153898_2.png) [@AkilaeTribe](https://discussions.unity.com/u/AkilaeTribe)
#### Post date: [September 21, 2010, 12:31pm UTC](https://discussions.unity.com/t/hashtables/423449/17 "2010-09-21T12:31:00Z")

</div>

> [@""](#):
>
> > [@](#):
> >
> > var dictionaryType = System.Type.GetType(“System.Collections.Generic.Dictionary`2[[System.String, mscorlib],[System.Int32, mscorlib]]”);
> 
> This grabs a reference to the generic Dictionary\<string, int\> (string keys, integer values) by using the string representation of its type name. It looks kind of funky but that’s how generics type strings are formed.

Yeah, as soon as I read this line, I knew it was over (for my brain)…

Out of the topic but isn’t it 8:30 AM at Canada ? You wake up early… 🙂

---

<div class="post-metadata">

### Author: ![Chris-Sinclair](https://dub1.discourse-cdn.com/unity/user_avatar/discussions.unity.com/chris-sinclair/32/241371_2.png) [@Chris-Sinclair](https://discussions.unity.com/u/Chris-Sinclair)
#### Post date: [September 21, 2010, 12:38pm UTC](https://discussions.unity.com/t/hashtables/423449/18 "2010-09-21T12:38:47Z")

</div>

Well… Canada spans 6 time zones… \>.\>

But yes, 8:30 here and liek… some of us have day jobs. 😄 _normally up at 7, but this chicken is sick ;\_;_

All types have a string representation.  
string = System.String  
int = System.Int32 (32 because it’s a 32 bit integer)  
HashTable = System.Collections.HashTable (at least in C#, not sure if the UnityScript one points to the same)

Generics just add a bit more information because for any given Dictionary, it has two more nested types (for key and value)

P.S. switch to C shaaaaaaaaaaaaarrrrrrrrp

---

<div class="post-metadata">

### Author: ![AkilaeTribe](https://dub1.discourse-cdn.com/unity/user_avatar/discussions.unity.com/akilaetribe/32/153898_2.png) [@AkilaeTribe](https://discussions.unity.com/u/AkilaeTribe)
#### Post date: [September 21, 2010, 12:47pm UTC](https://discussions.unity.com/t/hashtables/423449/19 "2010-09-21T12:47:13Z")

</div>

😉

If I were to switch to C# now, I would have to rewrite all my currents scripts, which are written in UnityScript 😄 I don’t even want to think about it now.

After this project, maybe ?

Maybe…?

Maybe…?

---

<div class="post-metadata">

### Author: ![Chris-Sinclair](https://dub1.discourse-cdn.com/unity/user_avatar/discussions.unity.com/chris-sinclair/32/241371_2.png) [@Chris-Sinclair](https://discussions.unity.com/u/Chris-Sinclair)
#### Post date: [September 21, 2010, 12:51pm UTC](https://discussions.unity.com/t/hashtables/423449/20 "2010-09-21T12:51:17Z")

</div>

Maybe…?

[Next page](https://discussions.unity.com/t/hashtables/423449.md?page=2)
