3
\$\begingroup\$

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.

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

I'm using Java and LWJGL.

Thanks waco

\$\endgroup\$
4
  • \$\begingroup\$ It's not really clear what you're asking. If you're using the correct data structure or not depends on how you're using it, we can't answer that. And what kind of events are you talking about? What's the problem you're trying to solve? How are you doing things now and what about it isn't working? \$\endgroup\$
    – House
    Apr 29, 2014 at 13:32
  • \$\begingroup\$ @Byte56 What I'm asking is, what is the fastest way to store gameobjects in some sort of list and quickly call events, render, update. \$\endgroup\$
    – waco001
    Apr 30, 2014 at 11:50
  • \$\begingroup\$ A good place to learn the observer design patern you said was used in minecraft for event driven programming action is this. Just go down to the contents or keep flipping through until you reach observer. \$\endgroup\$ Aug 22, 2014 at 15:19
  • 1
    \$\begingroup\$ An array/arraylist is the fastest data structure there is for linear read access, or for random access where all keys fall into a contiguous range. A hashmap is generally good for random access but can also be accessed linearly and is more versatile (at a small cost). The event dispatcher / listener AKA publish-subscribe pattern is used so you don't have to explicitly loop through any collections later on - loops / conditionals like that have a cost so are best avoided. You say nothing about what events you are awaiting and who pub & sub are. No-one can answer 100% till you do. \$\endgroup\$
    – Engineer
    Oct 17, 2015 at 14:33

1 Answer 1

1
\$\begingroup\$

I think that a entity would register a listener with whatever was firing the events that entity wanted (much the way you register event listeners to swing components). The entity that was firing events would just make a ArrayList or similar of all the listeners registered to it and whenever it wanted to fire events, it would just iterate through the list calling the actionPerformed (or similar) functions on each one. This has the benefit that it is fairly straightforward to make run asynchronously if you wanted to, say, run a pathing algorithm as soon as a sort of event is received.

\$\endgroup\$

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .