0

Given two strings, say hashKey and hashVal, I add the pair to a hash object. In this example hashVal is a string representing an integer, and thus I parse it as such before storing it in the table.

Now here's the problem. The value stored in the hash table is actually an int32 object, which makes it troublesome to use later inside expressions. After much time looking, I have been unable to find an easy way to either store an actual int or extract the value stored as an int instead of an int32 object.

Below is an example of what I'm trying to do:

var myHash : HashObject;
var intTemp : int;
var hashKey : String;
var hashVal : String;
hashKey = "foobar";
hashVal = "123";

if(System.Int32.TryParse(hashVal,intTemp)) 
{
    intTemp = int.Parse(hashVal);
    myHash.Add(hashKey,hashVal);
}

// later, attempt to retrieve and use the value:

var someMath : int;
someMath = 456 + myHash["foobar"];

this generates a compile time error:
BCE0051: Operator '+' cannot be used with a left hand side of type 'int' and a right hand side of type 'Object'.

If I try to cast the object, I instead get the runtime error:
InvalidCastException: Cannot cast from source type to destination type.

I know that I can first store the value retrieved inside a new int before using it, but that would be a very lengthy and inelegant solution for the quantity of math and number of key-value pairs I'll be using and thus mostly negate the benefits of using a hash table in the first place.

Any ideas?

Jerdak
  • 3,997
  • 1
  • 24
  • 36
Dave
  • 1,896
  • 2
  • 14
  • 15
  • 1
    It's javascript used within unity. Jared: will try that, I'll be back in a few mins. – Dave Mar 12 '13 at 00:41
  • @bfavaretto Unity likes to say that it is javascript, but it shares very little with javascript. someone has re-tagged it appropriately to unityscript. (I know I have written games in javascript and tried to port them to Unity's 'javascript' it turned into a massive headache.) – Ryan Mar 12 '13 at 00:56
  • 1
    @bfavaretto I went and added some to it, but it was pretty decent already. – Ryan Mar 12 '13 at 05:46
  • When you use `hashVal`, it's a String. When you try to extract it, it's still a String. It has to be an int instead. – Joetjah Mar 14 '13 at 12:26

3 Answers3

0

Why not store a tuple of the hashVal and the intTemp inside the table instead of just the hashVal? Then you can just access the number value directly from the lookup

if(System.Int32.TryParse(hashVal,intTemp)) {
    intTemp = int.Parse(hashVal);
    myHash.Add(hashKey, { hashValue : hashVal, intValue : intTemp });
}

var someMath : int;
someMath = 456 + myHash["foobar"].intValue;
JaredPar
  • 733,204
  • 149
  • 1,241
  • 1,454
  • Sadly, it appears the "javascript" used in unity/mono doesn't support tuples. [link](http://answers.unity3d.com/questions/381993/does-unity-4-mono-support-tuples.html) – Dave Mar 12 '13 at 01:05
0

I'm not familiar with the "HashObject" in unity script. Can you use a HashTable instead?:

var myHash: Hashtable;

function Start() {
    myHash = new Hashtable();
    myHash.Add("one",1);
    myHash.Add("two",2);
}
function Update () {
    var val = myHash["one"] + myHash["two"] + 3;
    Debug.Log("val: " + val);
}

Also in your original example you are assigning the string value to your hash table, intTemp is never used.

Jerdak
  • 3,997
  • 1
  • 24
  • 36
0
C# : The easiest hash solution in Unity is the HashSet:
https://msdn.microsoft.com/en-us/library/bb359438(v=vs.110).aspx

(You have to include the System.Collections.Generic library)

Very simple usage, O(1) speed

// create - dont even worry setting the size it is dynamic, it will also do the hash function for you :) 

private HashSet<string> words = new HashSet<string>();

// add- usually read from a file or in a for loop etc

words.Add(newWord);

// access via other other script such as

if (words.Contains(wordToCheck)) 
  return true;
Justin Hirsch
  • 319
  • 1
  • 3
  • 8