Unreal Real-Time Guide and Replication Example

GameSparks' real-time capabilities are easy to learn and use and offer game developers working in Unreal an easy multiplayer option without having to rent servers and host their game instances online:

To run a real-time session you'll need:

Game Seems Slow? Your game will seem slow when testing in multiple editors if you don't go to Editor Preferences -> General -> Miscellaneous and untick the Use Less CPU when in background checkbox.

This tutorial:

Gamemode! The following will take place in your gamemode.

Authenticating and Calling a Matchmaking Request for Testing

This is only intended for testing. Your authentication and function-calling solutions will have to fit your application's needs.

GSMessageListeners Component and Session Initialization

RT Session Events

Sending Packets

This is the core replication aspect in GameSparks' real-time SDK. Unlike Unreal's inbuilt networking layer, you'll have to manually spawn, remove, and set the location of your actors. Our Tetris example comes fully commented in Blueprint so you can see how gameplay sequences end with functions made to replicate changes. Replication functions send packets to other clients who receive these and update actors representing other clients.

Receiving Packets

If you've bound the OnData Event to a Custom Event in your gamemode, then clients will be able to receive packets:

Once that information is delivered you can use it to make changes to the actor representing the opponent's client in the local player's game/world. Each client has their own world, map, or level. Other clients are represented through actors present in those levels, whether they are a character or a Tetris piece. Packets coming in are used to update these actors to represent change made by that client.

The process is as follows:

Optimizing Packet Size! The trick becomes - how do you deliver the information in the smallest size possible to represent the change made by a client?

Example of Function Execution and Replication

The rest of this guide demonstrates how an input from a client turns into packet information that is delivered to another client and processed. The process is as follows:

1. This is a simple function triggered via input. The function checks if the local player's Tetris piece (Active Shape) is allowed to rotate. All of this is done on the client side so hacking is possible. A possible solution is saving values and invoking functions through the GameSparks real-time server first but we'll cover that in another tutorial.

2. Once the function gives the player the ability to rotate, it will also start the process of sending that information to other clients in the real-time session by calling the Event from the gamemode responsible for sending rotation packets.

3. The rotation Event is fed to the local player's controlled shape's rotation. The rotation information is then saved in a vector in the data object, placed in index 1 in the packet's data object, and sent to all active peers in the session with OpCode 2.

4. When that packet is received by the opponent player, it will be known that this packet is a rotation packet because of its OpCode. We switch the sequence based on the OpCode and feed the data into the appropriate function - in this instance we're calling the rotation function that will replicate the sender's controlled shape's rotation.

5. The function will check if the data is present (Safety check). If that data exists, break down the vector and extract the XYZ variables, and set the replicated shape's rotation.

6. To spawn the shape we assigned an Int for every shape type:

Download Sample Project? You can download the Sample project with the Unreal SDK and have a look at the functions to see how we've attempted to replicate the information. Each function is commented for your convenience.

Did this page help you? Please enter your feedback below. For questions about using this part of the platform, please contact support here