To ensure the most decoupled system interaction possible I decided to implement an event system. The key functionalities I desired from the system was a 'Fire and Forget' modus oparandi for events (no returns needed), a way to filter events by rough categories (to only check for event responses on the most relevant objects), and a way to control how many events are triggered per frame (increase performance at cost of a few frames of delay in response).
Initially I hoped to be able to use Unity's built-in event system, or C#'s. However it quickly became clear that employing the built in systems offered little benefit when I also want to add things like categories and throttling event invocations. As such I made a custom event system. It relies on three components: event dispatchers, event receivers (subscribers), and an event hub. The Dispatchers and Receivers work just like C# events for most purposes, but also contain a field for their category, allowing the Event Hub to filter each of them into different lists. Though each dispatcher and receiver has the option to not utilize the Event Hub and instead manually link themselves for special case situations where the connection is static, always required, and possible at compile time.
The work horse of this system is the event hub. handles both event invocations and event subscriptions, while providing a decoupled manner for quests, the dialogue and the notification system to receive information without having to be aware of any other objects. It uses 6 lists of receivers and dispatchers, one for each category, as well as a queue of events for each category. On initial setup any and all dispatchers and receivers set to use the hub informs the Hub of its existence, which automatically sorts them into their relevant list. When an event is received by the Hub, it is added to the relevant queue, and on the next frame the Hub will process the events in each queue until it hits the max events per frame. Each event is given its own thread to process, i.e send a message to each receiver.
Comentarios