Listening to Events
On this page
Listening Methods
Currently, the following options are available for event listeners:
Type | Description |
---|---|
DidaEventListenerComponent | Can be added to any object that allows components, listens for events at BeginPlay based on configuration. |
DidaEventListenerWidget | Can be added to any widget-supporting object, listens for events at Construct based on configuration. |
DidaEventListenerVariable | A versatile listener that can be placed anywhere and requires manual function calls to bind listeners. |
DidaEventMultiListenerComponent | Similar to DidaEventListenerComponent, but allows for multiple event listeners. |
DidaEventMultiListenerWidget | Similar to DidaEventListenerWidget, but allows for multiple event listeners. |
DidaEventMultiListenerVariable | Similar to DidaEventListenerVariable, but allows for multiple event listeners. |
For example, you can create a listener using CreateListenerVariable
.
If you create a ListenerVariable
, make sure to hold its reference to prevent it from being garbage collected, which would stop it from receiving events.
Below is an explanation of the relevant parameters:
Parameter | Description |
---|---|
Outer | Used as the Outer for instantiating the variable, and to access the EventSubsystem . |
Event | The event to be triggered. |
OnEventInvoked | Callback function invoked after the event is triggered. |
Channel | The channel being listened to; only events with either no channel or the same channel will trigger. |
PriorityLayer | The priority layer, which determines priority together with priority size. |
ListenerPriority | The priority size, which determines priority together with priority layer. |
bShouldRegisterOnStart | Whether to bind the listener upon registration; otherwise, you can manually call RegisterChannelListener , etc. |
Priority determines the order in which multiple listeners are triggered for an event; higher-priority listeners are triggered first.
For clarity, use layers to define priority size whenever possible.
Priority will first check the priority enumeration; the higher the enum value, the higher the priority.
Important Notes
A single DidaEventListenerInterface
cannot listen to the same event multiple times. Re-registering the listener has no effect, even with different channels. If necessary, unregister the previous listener or create multiple instances.
A DidaEventListenerInterface
can listen to multiple events, which triggers OnEventCalled
for each event. This can complicate logic maintenance, so it is recommended that each DidaEventListenerInterface
corresponds to one event. For multiple events, consider using the DidaEventMultiListenerXXX
interface.
Advanced Usage
To support advanced listening needs or avoid creating additional objects, an existing object can be set as a listener.
Simply add the DidaEventListenerInterface
to any UObject
-based class and implement the necessary logic:
- Add the
DidaEventListenerInterface
interface to the class. - Implement
GetListenerName
to specify the listener’s name, which can simply returnGetName()
. - Implement
OnEventCalled
to handle event callbacks. - Call
UDidaEventLibrary::RegisterListener
to register the listener at the appropriate time. - Call
UDidaEventLibrary::UnregisterListener
to unregister the listener when necessary.