This is a sample application to illustrate the use of the Unreal Message Bus system from a native code project.
There are two UProject files included here:
MessagingServer
is the server application listening forFJumpNowMessage
(which will cause the 3rd person character to jump).MessagingClient
is the client application sendingFJumpNowMessage
after a simple UMG Button is clicked.
- Compile and package both projects for your platform (only tested on Windows, and the following steps will include Windows-specific instructions. If you use any other platform, you might have to adjust accordingly.)
- Run the server application with the command line switch
-Messaging
to enable the Message Bus:MessagingServer.exe -Messaging
- Run the client application with the command line switch
-Messaging
to enable the Message Bus:MessagingClient.exe -Messaging
- With both applications running, click the "Jump" button in the client to see the Character in the server jump.
The interresting part of the server is RemoteJumpComponent.cpp.
It implements an Actor Component which is intended to be placed on a Character, which listens to its OnJumpNow
Event to act accordingly (that is, to jump).
When BeginPlay
is called, create an instance of FMessageEndpoint
by using FMessageEndpoint::Builder()
with a given Endpoint name (which is only used for debugging purposes, and does not influence how message are delivered).
To indicate which messages the Endpoint handles, call the FMessageEndpointBuilder::Handling()
method and pass the message type as template parameter, along with the method to be called when such a message arrives.
This does not mean message will be received, it only indicates what to do when a particular message arrives.
To actually receive them, call FMessageEndpoint::Subscribe<T>()
for every message type that is interresting at this point.
This also means that you can dynamically decide whether you want to receive particular message types or not, and toggle them on or off using FMessageEndpoint.Unsubscribe<T>()
.
During initialization, a handler method has been registered with FMessageEndpointBuilder::Handling()
and enabled using FMessageEndpoint::Subscribe<T>()
.
As soon as a message arrives, this method will be called with 2 parameters:
- The actual message.
- A Message Context that stores additional data about and around the actual message.
You could use the Message Context to reply to the sender directly, but this is outside the scope of this sample.
Once the message arrives, do what you must to satisfy the request.
The interresting part of the client is RemoteJumpTriggerComponent.cpp. Just like the server, it implements an Actor Component and is used on the HUD when the Button is clicked.
This is pretty much the same as for the Server. The only difference: We don't need to handle any messages, nor subscribe to them (unless we want to listen for them too).
Using the stored pointer to FMessageEndpoint
, we can call its FMessageEndpoint::Publish<T>(T Message)
method.
This is a broadcast and will go out to all listening entities on the Message Bus...or none at all, if noone is listening.
A valid message must be a UStruct that contain any UPROPERTY
type except for UObjects
. This means integral types (int32
, float
, etc.), selected container types (TArray
, TMap
, etc.), and other UStructs
.
It also needs a default constructor so it can be reconstructed on the other side.