By Yash Chamria
Kick Some Bass is an event-driven architecture based on the hybrid implementation of Publish/Subscribe and Observer patterns. The blog will go over how I integrated the Event system in Unity and how the team is using the events to their advantage.
Why an Event System?
Before that, it is important to understand why we decided to use the Event system in the first place. Kick Some Bass has three entirely distinct phases – Interactive Map, Fishing and Fighting. On top of that, each phase will have unique abilities, weapons, scoring system, achievement metrics, input, player moveset, camera system, AI with purpose, and the list goes on. All coupled systems will be hard to deal with and make the development process slow and potentially buggy.
To avoid spaghetti code and cyclic dependencies, the Event System comes to the rescue. The Event System allows all the systems to stay decoupled and only communicate using the events.
Integration with Unity
An Event System is a combination of events, event subscriber delegates and an event manager working together.
An event encapsulates the necessary data to execute the task.
An event subscriber delegate is used for the callback when an event of the mentioned type occurs.
The Event Manager processes events and matches them with the subscribed delegates, invoking the delegate function pointer.
C sharp(C#) makes the process of handling a function pointer simple and straightforward using the delegates. These function pointers are then associated with an event type. All the events must have an event type attached to them, where the event type is a huge event list enum. After that, any system can subscribe the function pointer for an event type to receive the notification.
Using the Events
The team is already integrating several systems into the event architecture. For instance, the Input System uses events to notify the input detection to the interested classes. This means systems interested in input don’t need to know about the Input System as long as they are subscribed for the events they are interested in, the function pointer will invoke automatically. Similarly, the ability system, fighter system, UI system and many more are event-driven structures.
This allows all the systems to be decoupled, flexible and easy to expand.