Adding a new listener

Listener listens to incoming broadcasts.

A listener is an actor/object that listens to any incoming broadcasts that happen in the future. Using the Agora Global Events World Subsystem, you can easily set a listener through Blueprints or C++. At any time, you can remove the listener or remove all listeners from anywhere. It doesn't have to be from a specific actor or object. For example: You can add/remove your character as a listener from another actor placed in the world or spawned at runtime.

To register a new listener, first add Agora Global Events World Subsystem node to your graph. Then drag a pin from its output and search for Register Listener and add it. In the register listener node, you should see a pin called Callback. This is the pin that will run when a message is broadcasted, so drag a wire and search for Add Custom Event... and select it (Feel free to rename the newly added custom event). Lastly, you have to specify what is listening. You can connect any actor or object here but for now let's connect the current Blueprint. Right click on an empty graph area and search for self. Select the node that says Get a reference to self. Connect this to the New Listener pin. You should now have a graph similar to the below screenshot.

Typically, you would connect this to Begin Play event so listener is registered when the map loads. The channel tag specifies which channel this listener is listening to. Imagine you have a button in your level, and you want all the lights to toggle when you press it. On a scenario like that, you can set the channel tag to something like "Event.Button.ToggleLight" and broadcast on that channel so only listeners with that channel tag is executed.

If you have played Fortnite Creative mode, you'll be very familiar with channel system 😉.

In C++, you can access the Agora Global Events World Subsystem via the static function Get and then call the Register Listener. You should also supply a separate function for Callback event.

Make sure you add this plugin as dependency otherwise you'll get LNK errors.

In your header file, add the below code (Feel free to rename the function/arguments as you like).

virtual void BeginPlay() override;

// Our custom event that will run.
void OnGlobalEventCallbackReceived(UObject* Payload);

Then in you source file, add this:

void AYourActor::BeginPlay()
{
    Super::BeginPlay();
    
    // Get a pointer to events subsystem;
    const auto AgoraEventsSubsystem = UAgoraGlobalEventsWorldSubsystem::Get(*GetWorld());
    
    // Create and bind a new Agora Global Message Received Delegate
    FAgoraGlobalMessageReceivedDelegate MessageDelegate;
    MessageDelegate.BindDynamic(this, &ThisClass::OnGlobalEventCallbackReceived);
    
    // Finally register this as a listener. We set our target gameplay tag as empty (using {}) so OnGlobalEventCallbackReceived will be called everytime we broadcast.
    AgoraEventsSubsystem->RegisterListener(this, {}, MessageDelegate);
}

void AYourActor::OnGlobalEventCallbackReceived(UObject* Payload)
{
    // Do your stuff.
}

That's all for C++ part. Now whenever you broadcast, the OnGlobalEventCallbackReceived will be triggered.

Last updated