Skip to main content

AddObserver

// Overload for method event handlers
template <typename TEvent, typename TObserver, typename TCallback>
requires TIsDerivedFrom<TObserver, UObject>::Value &&
TIsDerivedFrom<TEvent, ULES_Event>::Value &&
!TIsSame<TEvent, ULES_Event>::Value &&
LES::IsMethodEventHandler<TObserver, TCallback, TEvent>
FLES_ObserverHandle AddObserver(TObserver* Observer, TCallback Callback, const FName Channel = NAME_None);

// Overload for functor (callable object) event handlers
template <typename TEvent, typename TObserver, typename TCallback>
requires TIsDerivedFrom<TObserver, UObject>::Value &&
TIsDerivedFrom<TEvent, ULES_Event>::Value &&
!TIsSame<TEvent, ULES_Event>::Value &&
LES::IsFunctorEventHandler<TCallback, TEvent>
FLES_ObserverHandle AddObserver(TObserver* Observer, TCallback Callback, const FName Channel = NAME_None);
Type parameters
TEventThe type of the event you want the Observer to listen for. You should explicitly specify this type, like in the example further below.
TObserverThe type of the Observer object.
TCallbackThe type of the Callback method or functor.
Parameters
ObserverThe object that will be notified when events of TEvent type are sent on the Channel.
CallbackThe event handler that will be called when the event is received.
ChannelDetermines the channel the event will be sent on. Observers are notified only about the events sent on the channel they're listening on.
ReturnA handle to the newly created observer record in the Event System. You may use this handle later to remove this particular observer record from the Event System.

Adds the Observer to the Event System and marks it as listening for events of TEvent type, that are sent on the specified Channel. Example usage:

UObserver* Observer = NewObject<UObserver>();
EventSystem->AddObserver<UMyEvent>(Observer, &UObserver::OnMyEvent, "Some channel");

// UObserver should have a handler method defined like follows:
void UObserver::OnMyEvent(const UMyEvent* Event)
{
// Your code handling the event.
}
UObserver* Observer = NewObject<UObserver>();
EventSystem->AddObserver<UMyEvent>(Observer, [](UMyEvent* MyEvent)
{
// Your code handling the event.
}, "Some channel");

The Callback will not be called if an Event is sent and the Observer has already been garbage-collected.