Hello
I need to put the entries of a hashtable into an array so I can pick one at random.
Here is the idea:
-instntiate an array that is as long as the hashtable has elements
-loop though all of the hashtable’s elements and put them each into one slot of the array
-pick a random number between 0 and the array’s length
-shoose the onject inside that slot
My hashtable stores player characters (Gameobject) as values and their unique player number (int) as keys.
Here is my code:
var currentPrey: GameObject;
var preyOptions: Hashtable = new Hashtable();
function StartPursuit(){
var i: int = 0;
//pick a random player (WORK IN PROGRESS)
var playerIndices: int[] = new int[preyOptions.Count];
for(var myPlayer in preyOptions.Keys){
i = myPlayer;
playerIndices[i] = preyOptions[i];
}
var newPreyIndex: int = Random.Range(0, preyOptions.Count);
currentPrey = preyOptions[newPreyIndex] as GameObject;
}
The idea is to have a piranha chase a player in range. In multiplayer when the target escapes the piranha should pick one of the other players in range. So the piranha keeps track of all players in a hashtable.
Problem:
preyOptions.Keys is of type Object, but I need an int (all keys in this hashtable are int). Is there a way to tell the hashtable that all of its entries are int-Gameobject pairs? I wouldn’t mind if anyone can point me to a better alternative than hashtables (I use javaScript)