I want to use the message system to send events from one class to many different classes.
I thought this is possible with the MessageBus. From my understanding I register all my objects that should receive a certain message as listeners and another object then publishes the message.
But somehow I am too stupid to work out how to implement it. I think with a small piece of example code, like one listener class and one who publishes something I should be fine to figure the rest out.
Search for FMessageEndpoint::Builder in the code base. You will find many examples. If you still run into problems, let us know what exactly the errors are, so we can figure out what you’re missing. Thanks!
From my understanding so far the class handles these 7 Messages which can be published be any other class with the same MessageEndpoint. Am I correct so far?
So now I try to create my own MessageEndpoint:
The Messaging system supports two paradigms: request-response and publish-subscribe. Typically, you use publish-subscribe to locate services initially, and after that you send messages directly between endpoints via request-response. In order for messages to be handled by an endpoint, a handler function must be registered for either paradigm.
The Handling<> methods only register the handlers, which means that if a message should be received by this endpoint then the registered function will handle it. If no handler is registered, the message will not be handled. A message will be received by an endpoint if either (a) it was sent directly to the endpoint (i.e. request-response), or (b) it was published and the endpoint had a subscription to the message type (i.e. publish-subscribe). However, subscription does not happen automatically as part of registering a handler, but has to be done explicitly via MessageEndpoint->Subscribe<FWolfCharacterHide>(). This means that for publish-subscribe you have to do both, register a handler and subscribe to the message type. Subscription is not needed for messages that are sent directly to one or more endpoints using their endpoint addresses.
Because the UObject sub-system is not quite thread-safe yet, a small hack is currently needed for all UStructs that serve as messages. You have to declare the type trait TStructOpsTypeTraits with WithMessageHandling=true. Take a look at EngineServiceMessages.h for examples. We are planning to fix this soon so that this hack will not be needed in the future, and any UStruct type can be used as messages.
thnaks, After including the headers and the correct function definition I am a little bit closer. But now struggeling with the next problem. When I publish a message it is not received.
I have another class from which I want to send the message. I created a messageEndpoint there as well and use the publish method.
It’s just part of the particular protocols being used. In general, we use publish-subscribe only for service discovery, and once a service endpoint address is known, we use request-response to talk to the service directly.
Publish-subscribe can be slightly more expensive if many endpoints are subscribed to the same message type. It can also add a little bit of extra processing to any computer on the network, because their message router needs to process published messages to check whether anyone is subscribed. We currently use UDP multicast for published message, UDP unicast for sent messages. If a message is sent to more than one recipient, UDP multicast will be used.
Just a reminder, if you want to send messages between processes or computers, make sure that the UdpMessaging transport plug-in is enabled. It does the actual work of translating messages between the in-process message bus and the network.