Jump to content

  • Log In with Google      Sign In   
  • Create Account


#ActualFroyo

Posted 02 September 2012 - 11:31 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.

This is what I was looking for. I've never dealt with hashtables or maps in Java so I'm gonna go ahead and look at some references on those now. I appreciate the help!

Edit: At a glance, I notice that hashtables are similar to associative arrays in other languages. I take it that I'd still have to iterate through the hashtable and call GameObject.Update() on each iteration. Do you think this is better than an object recursively controlling itself? I'm trying to choose the best method to handle objects before I expand and cause a jumbled mess for myself.

#1Froyo

Posted 02 September 2012 - 11:27 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.

This is what I was looking for. I've never dealt with hashtables or maps in Java so I'm gonna go ahead and look at some references on those now. I appreciate the help!

PARTNERS