Listening to Events

Listening Methods

Currently, the following options are available for event listeners:

TypeDescription
DidaEventListenerComponentCan be added to any object that allows components, listens for events at BeginPlay based on configuration.
DidaEventListenerWidgetCan be added to any widget-supporting object, listens for events at Construct based on configuration.
DidaEventListenerVariableA versatile listener that can be placed anywhere and requires manual function calls to bind listeners.
DidaEventMultiListenerComponentSimilar to DidaEventListenerComponent, but allows for multiple event listeners.
DidaEventMultiListenerWidgetSimilar to DidaEventListenerWidget, but allows for multiple event listeners.
DidaEventMultiListenerVariableSimilar to DidaEventListenerVariable, but allows for multiple event listeners.

For example, you can create a listener using CreateListenerVariable.

concept-listener-variable

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:

ParameterDescription
OuterUsed as the Outer for instantiating the variable, and to access the EventSubsystem.
EventThe event to be triggered.
OnEventInvokedCallback function invoked after the event is triggered.
ChannelThe channel being listened to; only events with either no channel or the same channel will trigger.
PriorityLayerThe priority layer, which determines priority together with priority size.
ListenerPriorityThe priority size, which determines priority together with priority layer.
bShouldRegisterOnStartWhether 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:

  1. Add the DidaEventListenerInterface interface to the class.
  2. Implement GetListenerName to specify the listener’s name, which can simply return GetName().
  3. Implement OnEventCalled to handle event callbacks.
  4. Call UDidaEventLibrary::RegisterListener to register the listener at the appropriate time.
  5. Call UDidaEventLibrary::UnregisterListener to unregister the listener when necessary.