Skip to content

Event system

Ceanze edited this page Feb 15, 2019 · 2 revisions

The event system is a class called EventBus which is accessible everywhere in the project since it is a singleton. The system uses events to publish and subscribe in order to access callbacks.

How it works

For a synopsis on how it works, please see the Events.h or EventBus.h header files.

Creating an Event

In order to have certain functions and classes react to certain changes, an event for that change need to be created. All events are in the Events.h header file. An event is a struct which is inherited from an Event class, this is to make sure all events work in the EventBus.

To create an event make a struct with the event name you wish to have, which inherits from Event. The struct needs to have a constructor if it has variables. See example below.

struct ExampleEvent : public Event 
{
    ExampleEvent(int param1) : varible{ param1 } {};
    int varible;
}

Subscribe to an Event

To subscribe to an event, use the EventBus subscribe function. A function with the event you want to subscribe to need to exist in the class before this is being done. See example below.

ExampleClass::exampleFunction(ExampleEvent * e);

ExampleClass::ExampleClass()
{
    EventBus::get().subscribe(this, &ExampleClass::exampleFunction);
}

Publish to an Event

To publish to an event, use the EventBus publish function. The param requires an Event pointer to be sent, this can be created in the function parentheses.

EventBus::get().publish(&ExampleEvent(param1));

Alternative, the event can be created outside of the function.

ExampleEvent e(param1);
EventBus::get().publish(&e);

The event can be safely destroyed after the publish function.

Clone this wiki locally