Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by HiPhish · Jun 19, 2011 at 08:55 AM · javascriptloopthroughelementhashtable

,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)

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

2 Replies

· Add your reply
  • Sort: 
avatar image
0

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());
     }
 }
Comment
Add comment · Show 5 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image HiPhish · Jun 19, 2011 at 09:06 AM 0
Share

No, that won't do it since preyoptions.$$anonymous$$eys is of type Object, I get an error "cannot convert object to int"

avatar image jahroy · Jun 19, 2011 at 09:18 AM 0
Share

You could use strings for keys and parse them into ints.

avatar image jahroy · Jun 19, 2011 at 09:36 AM 0
Share

I just wrote a quick test program and modified my answer...

avatar image HiPhish · Jun 19, 2011 at 09:53 AM 0
Share

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.

avatar image jahroy · Jun 19, 2011 at 10:21 AM 0
Share

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...

avatar image
0

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];
 }
Comment
Add comment · Show 10 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image HiPhish · Jun 19, 2011 at 09:07 AM 0
Share

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.

avatar image GuyTidhar · Jun 19, 2011 at 09:11 AM 0
Share

Then just use Array.

private var prey : Array = new Array();

function StartPursuit() { var newPreyIndex: int = Random.Range(0, prey.Count); currentPrey = prey[newPreyIndex]; }

avatar image HiPhish · Jun 19, 2011 at 09:14 AM 0
Share

Aren't javascript Arrays pretty slow?

avatar image HiPhish · Jun 19, 2011 at 09:16 AM 0
Share

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.

avatar image GuyTidhar · Jun 19, 2011 at 09:18 AM 0
Share

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.

Show more comments

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

3 People are following this question.

avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges