|
| template<typename Event > |
| SubscriptionId | subscribe (Handler< Event > handler) |
| | Registers a handler for events of type Event.
|
| |
| bool | unsubscribe (SubscriptionId id) |
| | Removes a handler.
|
| |
| template<typename Event > |
| void | publish (const Event &event) |
| | Calls every handler of type Event with event, before returning.
|
| |
| template<typename Event > |
| std::size_t | subscriber_count () const |
| | Number of handlers registered for events of type Event.
|
| |
| void | clear () |
| | Removes every handler of every event type.
|
| |
Lets systems react to events without knowing who sends them.
Any copyable or non-copyable type can be an event: struct EntityDestroyed { Entity entity; };. publish() calls the handlers of that exact type immediately, in subscription order.
Handlers may publish, subscribe and unsubscribe while an event is being dispatched: a handler added during a dispatch is first called by the next publish(), and a handler removed during a dispatch is not called anymore, even by the current one.
The bus is not thread-safe: use it from the game thread only.