1/11/10

The big event

I added another major component to my game engine this week, the event manager.  The event manager will, um..., manage events!  It will be responsible for the communication between all processes in the game.  The manager tracks two sets of data, event types and event listeners.

Each event that is created will have its own unique name or "event type".  In order for an event to be processed by the manager, its event type must first be registered by the manager.  When an event type is registered, the event manager initializes a container that will store all the listeners for that event.  If a system attempts to trigger an event that is not registered with the manager, the manager will reject the event.

Event listeners are the objects that handle the events processed by the event manager.  When a system wants to listen for events, it will register a listener with the event manger for the type of event it wants to listen to.  Each time an event is processed, all the listeners that are listening for that particular event type will have thier HandleEvent method called.

The event manager allows for two ways of processing events, triggering an event immediately, and queueing the event to be run the next time the event queue is processed.



The architecture of this event manager is once again based heavily on the engine described in Game Coding Complete.  I didn't do any copy paste on this one, but I did consult the book and the source code quite a bit to make sure that  what I was doing was consistent with the book.  Coding the event manager myself definitely gave me a better understanding how it worked than when I just read through the book the first time.

Another thing I did in the development was write unit tests for the event manager.  I didn't use any unit testing frameworks, I just created methods that wrote to the console the name of the test and whether or not it passed or failed.  In the case of a failure, I would simply debug the test that failed.  I've become a big fan of unit testing as a means of testing code, especially in this instance, where as of right now, I don't have a game in place yet to test the event system. 


I'm going to stick with the manager theme for my next project,  I'm going to start work on the process manager. Processes that need to be included in the current state of the game will be registered with the process manager, and the process manager will loop through it's registered processes each frame update.


No comments:

Post a Comment