What is it?
Game events are commonly discussed as they can be an integral part of most games architecture.
A game event can be anything that other game objects could be interested in. The player dying, an explosion going off, and a coin being collected can all be classed as events. There is often a need to notify other entities when these actions occur without tightly coupling the entities.
When should you use it?
I use some form of an event system in most of my projects. Common uses include:
- Achievement system (various entities can raise events without needing a reference to the achievement manager, easily implement incremental achievements).
- Audio player. Any object can raise an audio event when required.
- AI systems
How do you use it?
Implementing Your Own Events
Once you have imported the event system (included in the GitHub project above and full code listing provided below) you need to write your own events. These events will be specific to your own game’s needs.
The base event class is straightforward (read: empty). All of your events should inherit from this class.
An example of an audio event is shown below (the audio queue included in the GitHub project responds to these events).
Once you have implemented your own events it is easy to “raise” them. This simply means “I don’t know who cares but this event has just happened, do what you will with that information”.
Listening to Events
No actions will be performed when an event is raised if they’re no listeners for that event. It is important to remember to stop listening to the event when the object is removed from the scene or you are no longer interested in the event.
Event Manager (Full Code)
The event manager class is included in full below.
Thank you for reading 🙂