Hashtable values – ShiVa Engine

Hashtable values

When you want to set a key/value in a hashtable, you have to check if the key already exists before calling hashtable.set (), because if the key doesn’t exists, the function won’t do anything. To solve this problem without making your script unreadable, I suggest you create a function “addToHashtable”, which will automatically do the check and add the key.

--------------------------------------------------------------------------------
function MyGame.addToHashtable ( htHashtable, sKey, vValue )
--------------------------------------------------------------------------------
   if ( hashtable.contains ( htHashtable, sKey ) )
   then
       -- If the key is in the hashtable, update the value
       hashtable.set ( htHashtable, sKey, vValue )
   else
       -- If the key is not in the hashtable, create a new entry in the hashtable, with sKey and vValue as key/value for this entry.
       hashtable.add ( htHashtable, sKey, vValue )
   end
--------------------------------------------------------------------------------
end
--------------------------------------------------------------------------------
--use like:
this.addToHashtable ( this.myHashtable ( ), “myKey”, 3 )



Need more answers?

  • slackBanner