Storing gameobjects, and calling them through events

Hello Guys, New member here, but have read many posts here also ;D.

I’m working on a 2D tile based game in which the player interacts with other game objects (chests, AI, Doors, Houses etc…). The entire map will be stored in a file which I can read. When loading the tilemap, it will find any tile with the ID that represents a gameobject and store it in a hashmap (right data structure I think?).

    private static HashMap<Integer, Class<GameObject>> gameObjects = new HashMap<Integer, Class<GameObject>>();

How exactly would I go about calling, and checking for events? I figure that I would just call the update, render and input methods of each gameobject using the hashmap. Should I got towards a Minecraft/Bukkit approach (sorry only example I can think of), where the user registers an event, and it gets called whenever that event happens, and where should I go as in resources to learn about that type of programming, (Java, LWJGL).

Or should I just loop through the entire hashmap looking for an event that fits?
Thanks waco

Couple of thoughts:

  1. Tile-map

If you have a tile-map, why organise the data as a hash-map indexed by ID?

Seems more logical have a 2D array (or similar data-structure) that represents each tile and have those reference your game objects?

e.g.


class Tile {
    private final Set<GameObject> objects = ...
}

class World {
    private final Tile[][] map = ...
}

No need for any IDs or look-ups since (presumably) you know which tile(s) you are dealing with at and the above tells you which objects are on each tile.

Or am I missing the point completely here?

  1. Events

I assume you are referring to user input events here? Or are you also talking about other events such as timers, AI, random events, etc?

In either case, presumably you know which tile(s) the event applies to so it’s just a matter of handling that event in some sort of controller class and updating the state of the game object(s) on that tile?

  • stride

Thanks for the reply,

  1. The tilemap stores the graphical code to draw the screen, as in the floor tiles, player, trees, etc (in 3 layers). Currently each different type of tile is stored in an enum. What I want is to be able have an event system that interacts with the game object manager. Each game object class will have an event like object move event. What I’m thinking of is what I think is called registration in Java?

Still not entirely sure what you are asking for.
Also registration isn’t a Java (or OO) concept AFAIK so not sure what you mean by that.
Might be worth posting some of your code, or maybe a rough diagram to illustrate your design?

Hi sorry for the confusion, basically what I want to know is the best and fastest way to store many, many game objects and call events from them, render methods, and update methods.
All gameobjects will extend the GameObject class.
For my gameObjectManagerClass, I currently have

public class GameObjectManager {
	private static ArrayList<GameObject> gameObjects = new ArrayList<GameObject>();
	public static void addGameObject(GameObject gameObject){
		gameObjects.add(gameObject);
	}
	public static void removeGameObject(GameObject gameObject){
		gameObjects.remove(gameObject);
	}

	public void update(){
		for(GameObject i : gameObjects){
			i.update();
		}
	}
	public void render(){
		for(GameObject i : gameObjects){
			i.render();
		}
	}
}

Is what I have the best way of storing gameobjects?

ALSO, I’ve been wanting to implement an event system similar to what bukkit has.

Ex.

@EventHandler
  public void onLogin(PlayerLoginEvent event) {
    getLogger().log(Level.INFO, "Player " + event.getPlayer().getName() + " is logging in!");
  }
public final class LoginListener implements Listener {
    public LoginListener(LoginPlugin plugin) {
        plugin.getServer().getPluginManager().registerEvents(this, plugin);
    }
 
    @EventHandler
    public void normalLogin(PlayerLoginEvent event) {
        // Some code here
    }
 
    @EventHandler(priority = EventPriority.HIGH)
    public void highLogin(PlayerLoginEvent event) {
        // Some code here
    }
}

This is a nice event system I would eventually like to work through…

And when I mentioned registering, I meant like saving the Gameobject class (Player that extends GameObject) in the gameobjectmanager class and calls methods in them.

Thanks waco