Coder Social home page Coder Social logo

unrealmessagebusdemo's Introduction

UnrealMessageBusDemo

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 for FJumpNowMessage (which will cause the 3rd person character to jump).
  • MessagingClient is the client application sending FJumpNowMessage after a simple UMG Button is clicked.

How to run the Demo

  1. 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.)
  2. Run the server application with the command line switch -Messaging to enable the Message Bus: MessagingServer.exe -Messaging
  3. Run the client application with the command line switch -Messaging to enable the Message Bus: MessagingClient.exe -Messaging
  4. With both applications running, click the "Jump" button in the client to see the Character in the server jump.

About the Server

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).

Initialization

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>().

Waiting for the Message

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:

  1. The actual message.
  2. 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.

About the Client

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.

Initialization

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).

Sending a Message

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.

About the Messages

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.

unrealmessagebusdemo's People

Contributors

bhaalsen avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.