Jump to content

  • Log In with Google      Sign In   
  • Create Account


We have 4 x Pro Licences (valued at $59 each) for 2d modular animation software Spriter to give away in this Thursday's GDNet Direct email newsletter.

Read more in this forum topic or make sure you're signed up (from the right-hand sidebar on the homepage) and read Thursday's newsletter to get in the running!


Member Since 28 Feb 2010
Offline Last Active Jan 30 2013 02:29 PM

#4975786 Handling many objects

Posted by marc40000 on 02 September 2012 - 12:14 PM

Yes, for example in php the associative array is implemented as a hashtable or map. In java, there is probably a hashtable implementation in the framework - but I haven't used java for years so I don't know what it's called. In .net for example it's called a Dictionary so it might not be called hashtable in java.

If each frame Update() has to be called on every gameobject, there is no way around actually calling it. I'm not sure what you mean with "recursively controlling itself". Instead of a hashtable or additional to it, you can have a logical tree structure where you put your objects in if you want. I think Unity does this. However, you probably want a hashtable next to it as well so you can get an object given its unique-id.

#4975765 Handling many objects

Posted by marc40000 on 02 September 2012 - 10:40 AM

I think you need something like this. While an array might not be the best choice, you need a place where all you game objects are put in so you can do things with all of them like your move() or a general gamelogicupdate() or a render() etc.

I prefer to have a hashtable to store my game objects in. This comes with some handy features:
- you can still iterate over all your objects
- every object you create can get a unique id and you can store it in the hastable by that unique id. With an array you could use the index for that, but as the game continues, you get higher and higher ids while old objects get removed and you get holes in your array. OR you could put new objects at the places of removed objects, but then, your index-ids aren't unique anymore.
- each object having a unique id allows objects to reference each other without tricky sideeffects. For eaxmple imagine a unit A chase another unit B. You could add a reference of B in A and that way A can check B's location in the gamelogicupdate() and knows where to move. However, what if B gets destroyed? It get removed from the scene, but it keeps existing because A has a reference to it. If A only knew B's id, it can do the same as above by checking the hashtable of B and get B's position. When B gets destroyed, A will notice because the hashtable lookup will fail. Also note that references/pointers are only valid locally. When you add network play, you absolutely need some kind of unique ids.

Instead of a hashtable one could use a map. Besides that, you probably want a spatial datastructure as well to put your game objects in so you can do queries like "give me alle objects within a radius of 5m of point p.". One might be able to combine these datastructures somehow, I don't know thought. I always used those two next to each other.