Jump to content

  • Log In with Google      Sign In   
  • Create Account


Handling many objects


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
7 replies to this topic

#1 Froyo   Members   -  Reputation: 201

Like
1Likes
Like

Posted 02 September 2012 - 06:52 AM

I'm currently making a 2d RPG in Java and its engine is coming along quite nicely.. however I am a bit confused on how I should handle a certain situation. I have a class Unit which is any game character (subclasses for NPCs, Player, etc). The problem is there are always going to be several instances of this class and each one will need to update independently each time the canvas is rendered.

What's the best way to manage this? Currently each time gameUpdate() runs (optimally 60 times per second), it loops through an ArrayList that contains each Unit. Is there any way to forego using an array to handle each object? It seems like using an array like this will come back to haunt me when the game is more complex.

This is what I have now and I'd like to improve it or scrap it altogether.
[source lang="java"]package engine;import java.util.*;public class ObjectHandler extends ArrayList<GameObject> { private static final long serialVersionUID = -8598610051757901108L; public ObjectHandler() { System.out.println("ObjectHandler loaded."); } public static ObjectHandler getObjectHandler() { if (ref == null) ref = new ObjectHandler(); return ref; } // This method is ran every time // the game is updated. gameUpdate() public void update() { for (int i = 0; i < size(); i++) { get(i).move(); } } // This method is ran every time // the game is updated. gameRender() public void drawList() { String name; for (int i = 0; i < size(); i++) { name = null; name = get(i).name; } } /* OBJECTS ADD THEMSELVES IN THEIR CONSTRUCTOR * public void add(String unitName) { Unit unit = new Unit(unitName); //System.out.println(unit.name); add(unit); }*/ private static ObjectHandler ref;}[/source]

Sponsor:

#2 Uzumakis   Members   -  Reputation: 130

Like
0Likes
Like

Posted 02 September 2012 - 07:52 AM

Acc to the current situation that goes without any doubt that you code will get more and more complex with the increase in the number of the units there will be more loops more array and so on insted of using loops for ur job why dont u give a try to a time class makin and independet method for each class or making a sinlge one and adjusting all the elements acc to that single timer results way easier then that for loops

#3 Froyo   Members   -  Reputation: 201

Like
1Likes
Like

Posted 02 September 2012 - 08:33 AM

Acc to the current situation that goes without any doubt that you code will get more and more complex with the increase in the number of the units there will be more loops more array and so on insted of using loops for ur job why dont u give a try to a time class makin and independet method for each class or making a sinlge one and adjusting all the elements acc to that single timer results way easier then that for loops

Are you saying I should make each object update on its own (either by making it runnable or using a while loop)? I guess I'm not understanding how this would work where the object would still be able to interact with everything else that is updating on a different cycle.

#4 marc40000   Members   -  Reputation: 131

Like
1Likes
Like

Posted 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.
Confrontation Unlimited - MMORTS - http://www.confrontation-unlimited.net

#5 Froyo   Members   -  Reputation: 201

Like
1Likes
Like

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!

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.

Edited by Froyo, 02 September 2012 - 11:31 AM.


#6 marc40000   Members   -  Reputation: 131

Like
1Likes
Like

Posted 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.
Confrontation Unlimited - MMORTS - http://www.confrontation-unlimited.net

#7 Froyo   Members   -  Reputation: 201

Like
1Likes
Like

Posted 02 September 2012 - 08:57 PM

After some research I found that HashTable is included in a Java package "Java.util.Dictionary", but it is legacy now. Using a HashMap seems to be the way to go, so I'll try that for now.

I looked into some spacial data structures and I think I'd like to avoid them for now if possible. R-Trees and Quadtrees are the two I looked at and to be honest they seem pretty daunting to me at a glance. I think I'd be better off using conditionals to check the coordinates of an object on the game map.

#8 marc40000   Members   -  Reputation: 131

Like
0Likes
Like

Posted 03 September 2012 - 06:07 AM

When I wrote my prvious post, I did a quick google of hashtable and java anf also found HashTable and HashMap. While HashMap looked more appealing than HashTable to me, I'ld prefer a generic version.

Spatial Data structures may look complicated at first sight. (Hashtables as well.) But if you don't have to implement them yourself and can use an existing working implementation, it's actually quite simple, as they do what you otherwise have to solve with loops over all objects and check for their positions. It's actually simplyfing your code while being faster. If you don't have many objects in your scene, the speed point might be neglectable though. From a theoretical point of view, assuming you have n objects in your scene and each does a spatial query that has to cycle through all objects, one gamelogic update will be O(n^2). That's really bad for large n. f you use a spatial datastructure, a spatial query goes down to O(log n) and for n objects, that gives us O(n log n) which is way better for large n. Assume n==1000, first scenario, that's about 1000000 "steps" while it's only about 10000 for the spatial data structure version. That's an acceleration of factor 100!
Confrontation Unlimited - MMORTS - http://www.confrontation-unlimited.net




Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS