MessageEndpoint, subscribed events being received multiple times

I’m using publish-subscribe FMessageEndpoints to send/receive messages, however message callbacks (registered with subscribe) are being triggered an arbitrary number of times, even though the message is only published once.
I’ve also noticed that the instance of the object receiving the event is only valid during one of these callbacks, the rest have empty variables.

An example to illustrate:
In my custom Game Mode I have a pointer to my custom Game State and a message callback registered with subscribe. If I log “Received” in this callback, it’ll appear an arbitrary number of times, however, if I check that the pointer to the custom Game State is valid before logging, it’s only shown once.

I’ve tried setting recipient thread and sending thread, but still no luck. It’s not necessarily a problem, since I can check if the instance is valid, but I’d like to understand why this is happening, and if I’ve actually set something up incorrectly. Thanks!

Relevant code

MessageEndpoint = FMessageEndpoint::Builder("GameMode").Handling<FEventPlayerDeath>(this, &ADDGameMode::HandlePlayerDeath)

MessageEndpoint->Subscribe<FEventPlayerDeath>();


void ADDGameMode::HandlePlayerDeath(const FEventPlayerDeath& Message, const TSharedRef<IMessageContext, ESPMode::ThreadSafe>& Context) {  
    // if (DDGameState)  // Pointer to custom Game State instance
    UE_LOG(LogTemp, Log, TEXT("Received")); // Logged an arbitrary number of times, unless the above line is un-commented  
}

// This only runs once (in a different class)
MessageEndpoint->Publish(new FEventPlayerDeath(Player));

Do you have multiple instances of your GameMode by any chance?

Also note that, if you have multiple instances of the Engine running on the network, they will receive each other’s published messages.

FMessageEndpoint.Publish() has an overload that takes an EMessageScope. By default it is Network, but you can also use Thread or Process or to limit the reach of published messages.

In the near future I will also implement a Host scope to limit message reach to the local computer.

Thanks for your reply and apologies for taking so long to reply! I’m not explicitly creating any GameMode instances, just setting the custom GameMode to be used in the map settings, so that should mean only one instance is active, is there a way to check? And I’m not using any networking, and have tried changing the message scope but that produced the same results. Thanks!

You can open the Messaging Debugger (must be enabled in Plugins) and check how many endpoints with the name “GameMode” there are. You can also trace all messages and see how many messages of type FEventPlayerDeath are being sent.

Okay, it turns out that, upon restarting the editor, there are between 6 and 10 GameMode endpoints! After leaving it for a little while (not in play mode), 8 more appeared with “Time Registered” from 304 to 335. Any ideas why this is happening, or do I need to do some digging to try and determine what I’ve done to cause more instances to be created? Thank you so much for your help!

In what function are you creating the endpoint?

Yep that was the problem, thanks a bunch! I was registering the endpoint in the constructor, but moving it to StartPlay solved the issue and now only one endpoint is showing. Is there any documentation on using the messaging system? It’s such a major component but I couldn’t find any examples and have been trying to figure it out by looking at the engine code and the API reference, which could really do with some usage examples (unless I’ve missed them) IMessageBus | Unreal Engine Documentation

Thanks again!

Cool, glad you figured it out. The code documentation is the only one available right now. I will write some tutorials at some point. The usage examples are best found in the Engine itself - simply search for FMessageEndpointBuilder in the code base.