A simple event system where classes can subscribe to events and respond to them.
This solution includes the Event System, and some example usage files. If you want to use the Event System, you only need the following files...
- EventSystem.h
- EventSystem.cpp
- Listener.h
- Event.h
The event system itself does not need any special initialization. However, the objects that LISTEN for events must subscribe to those events. Example...
// Subscribe batman to the Bat Signal event
Batman batman;
EventSystem::Instance()->RegisterClient("BAT_SIGNAL", &batman);
Anyone with access to the EventSystem can send events by using the SendEvent() method. The first parameter is what event you are sending, and the second parameter is a void* that you can use to send parameters with.
// Send an event
EventSystem::Instance()->SendEvent("BAT_SIGNAL", 1);
Every frame of your program, you should call the Event Systems ProcessEvents() method. This method goes through all events currently in queue and has their listeners respond to those events. So once this method has finished, there will be 0 events left over in the queue. Example...
// Process events that have occurred this frame
EventSystem::Instance()->ProcessEvents();
You can unsubscribe a listener from all events by calling UnregisterAll(Listener*) method. Example...
// Unsubscribe Batman from any future events. He quits.
EventSystem::Instance()->UnregisterAll(&batman);
You can unsubscribe a listener from specific events by calling UnregisterClient(EVENTID, Listener*) method. Example...
// Batman no longer cares about Bank Robberies. Stop listening for them..
EventSystem::Instance()->UnregisterClient("BANK_ROBBERY", &batman);
If you need to get rid of all events currently in queue (for some reason), you can do so with the ClearEvents() method. Example...
// Clear Events
EventSystem::Instance()->ClearEvents();
When you are finished with the Event System, you can shut it down by calling Shutdown() method. Example...
// Shutting down the Event System
EventSystem::Instance()->Shutdown();