- Home /
,loop through a hashtable
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)
Answer by jahroy · Jun 19, 2011 at 08:59 AM
Maybe you're not being explicit enough when you create the entries in your hash.
The following works fine for me, so it is possible to use ints as keys:
class FunkyClass
{
var name : String;
var game : String;
function FunkyClass ( theName, theGame )
{
name = theName;
game = theGame;
}
function toString ()
{
return "My name is " + name + " and my game is " + game;
}
}
function doStuff ()
{
var nameList = new Array("Stu", "Karl", "Lenny", "Clyde");
var gameList = new Array("Disco", "Funk", "Boogie", "Jazz");
var funkHash = new Hashtable();
for ( var i : int = 0; i < 4; i ++ ) {
var freshFunk : FunkyClass = new FunkyClass(nameList[i], gameList[i]);
funkHash[i] = freshFunk;
}
for ( var theKey : int in funkHash.Keys ) {
var theFunk : FunkyClass = funkHash[theKey];
print(theFunk.toString());
}
}
No, that won't do it since preyoptions.$$anonymous$$eys is of type Object, I get an error "cannot convert object to int"
I just wrote a quick test program and modified my answer...
Your example only works because you are using dynamic typing, which is less performant than static typing.
Put "#pragma strict" at the top of the script and you will get my error.
Sorry. I guess I'm of no help to you. I know you can do what you need to do with C#, but you probably already know that...
Answer by GuyTidhar · Jun 19, 2011 at 09:02 AM
I'm not sure I understand why do you need the hashtable? Why not just have an array of GameObjects and random the index? e.g:
private var prey : GameObject[];
function StartPursuit()
{
var newPreyIndex: int = Random.Range(0, prey.Length);
currentPrey = prey[newPreyIndex];
}
I don't know the total amount of players and the amount of players in range when I program. It depends on what the players do during the game.
Then just use Array.
private var prey : Array = new Array();
function StartPursuit() { var newPreyIndex: int = Random.Range(0, prey.Count); currentPrey = prey[newPreyIndex]; }
Ohhh, now I get it. No, the problem with this is the following: Imagine I have four players, all in range. Then player 2, who is the current prey, exscapes. The piranha picks a number between 0 and 3, so it might pick number 2. But player two is out of range.
I don't see why it would be slower then a hashtable which need more overhead as its a more complex object. You have the index so you don't have to wait for lookup to be done. Hashtable is needed in cases when you don't know your index.
Your answer
Follow this Question
Related Questions
Picked up Objects pass through walls 2 Answers
Is there an unsorted hashtable-like container? 2 Answers
problem adding element of array 1 Answer
Hashtables and implicit downcasting of Object to int 0 Answers
Find Position in Array or List? 2 Answers