• Announcements

    • khawk

      Download the Game Design and Indie Game Marketing Freebook   07/19/17

      GameDev.net and CRC Press have teamed up to bring a free ebook of content curated from top titles published by CRC Press. The freebook, Practices of Game Design & Indie Game Marketing, includes chapters from The Art of Game Design: A Book of Lenses, A Practical Guide to Indie Game Marketing, and An Architectural Approach to Level Design. The GameDev.net FreeBook is relevant to game designers, developers, and those interested in learning more about the challenges in game development. We know game development can be a tough discipline and business, so we picked several chapters from CRC Press titles that we thought would be of interest to you, the GameDev.net audience, in your journey to design, develop, and market your next game. The free ebook is available through CRC Press by clicking here. The Curated Books The Art of Game Design: A Book of Lenses, Second Edition, by Jesse Schell Presents 100+ sets of questions, or different lenses, for viewing a game’s design, encompassing diverse fields such as psychology, architecture, music, film, software engineering, theme park design, mathematics, anthropology, and more. Written by one of the world's top game designers, this book describes the deepest and most fundamental principles of game design, demonstrating how tactics used in board, card, and athletic games also work in video games. It provides practical instruction on creating world-class games that will be played again and again. View it here. A Practical Guide to Indie Game Marketing, by Joel Dreskin Marketing is an essential but too frequently overlooked or minimized component of the release plan for indie games. A Practical Guide to Indie Game Marketing provides you with the tools needed to build visibility and sell your indie games. With special focus on those developers with small budgets and limited staff and resources, this book is packed with tangible recommendations and techniques that you can put to use immediately. As a seasoned professional of the indie game arena, author Joel Dreskin gives you insight into practical, real-world experiences of marketing numerous successful games and also provides stories of the failures. View it here. An Architectural Approach to Level Design This is one of the first books to integrate architectural and spatial design theory with the field of level design. The book presents architectural techniques and theories for level designers to use in their own work. It connects architecture and level design in different ways that address the practical elements of how designers construct space and the experiential elements of how and why humans interact with this space. Throughout the text, readers learn skills for spatial layout, evoking emotion through gamespaces, and creating better levels through architectural theory. View it here. Learn more and download the ebook by clicking here. Did you know? GameDev.net and CRC Press also recently teamed up to bring GDNet+ Members up to a 20% discount on all CRC Press books. Learn more about this and other benefits here.
Sign in to follow this  
Followers 0
Froyo

Handling many objects

7 posts in this topic

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 [i]Unit[/i] 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 [i]Unit[/i]. 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]
1

Share this post


Link to post
Share on other sites
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
0

Share this post


Link to post
Share on other sites
[quote name='Uzumakis' timestamp='1346593939' post='4975725']
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
[/quote]
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.
1

Share this post


Link to post
Share on other sites
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.
1

Share this post


Link to post
Share on other sites
[quote name='marc40000' timestamp='1346604037' post='4975765']
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.
[/quote]
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 [i]GameObject.Update()[/i] 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
1

Share this post


Link to post
Share on other sites
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.
1

Share this post


Link to post
Share on other sites
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.
1

Share this post


Link to post
Share on other sites
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!
0

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0