Skip to main content
Version: v1.18

Network

To make an extension compatible with Skyreal VR collaboration mode, it's necessary to understand how network replication system work in Unreal. To start, the oficial documentation can bring basic information: Unreal replication

Unreal datamodel replication

By default, some Unreal datamodel objects are replicated and some are not. The synthesys bellow can help to understand whitch object is avalable given the context.

Unreal datamodel replication


ClassOne per applicationOne per mapOne per userReplicated on serverReplicated on clients
Game instanceX----
Game mode-X---
Game state-X-XX
Player controller--XX-
Pawn--XXX


For example:

  • Every pawn are available on every collaboration instance of the application, this is why user can see avatar of other players.
  • Every PlayerControllers are available on the server but only local player controllers are available on a single collaboration instance of the application. The reason why is because the playercontroller represent the behaviour or the user and it should not be visible or modified by other users.

Replication basics

To simplify, the unreal replication system is divided into two part:

  • The information replicated from the server to every clients
  • The requests send from one client to the server

What is impossible to do with the replication system

  • Replicated data from the client to the server (for security reasons, clients can only request things)
  • Spawn a new replicated actor on a client (same reason as above)
  • Having return values on request send from the client to the server

Replication (Server -> client)

To replicate an actor from the server to the clients, make sure te "Replicates" property is checked in the "Class Defaults".

Replicate Actor

Once this operation is done, the actor should be swpaed automatically on every clients, but it doesn't mean that the actor will be fully replicated. To do so, every properties should be tagged as replicated. One of the most important is "Replicated Movement" in the "Class Defaults" that allow object position and rotation to be automatically replicated during session (warning, the object has to be moved on the server side to be replicated, no movements comming from the client will be taken into account).

To replicate a custom variable of the blueprint, create a custom variable and in the detail panel, select either "Replicated" or "RepNotify" in the Replication field.

Replicate Actor

"Replicated" is the easiest way of replicate a variable without adding any behaviour, but most of the time, it's important to be notified on the client side that something has changed. Then "RepNotify" should be used to execute some code after the property has been replicated on client.

Replicate Actor

In the example above, the property "TestVar" representing the visibility of the actor has been replicated using RepNotify. Because ActorHiddenInGame can't be replicated by design in Unreal, it's necessary to create some code to sync the visibility between collaborative instance of the aplication. In this case, when the server will edit the calue of the variable "TestVar", it will be replicated on the client. When the data will be received on the client side, the function "OnRep_TestVar" will be called and the visibility of the object will be changed localy given the variable content. Then, the local visibility is not natively replicated, but replicated thanks to an temporary replicated variable.

RPC request (Client -> Server)

To comunicate from the client to the server, the work is harder. First of all, only object where you have local ownership can have an up connection with the server (for example the player controller or the pawn, by not a random SkrPart that nobody owns). This limitation make things harder to send request, so Skyreal VR has developed a way to simplify this flow called "RPCInterfaceComponent". Once declared, this object is automatically instanciated with the local ownership to allow developer to create easily a comunication with server. To create one

  • first go on the "RegistrableAssets" directory of the extension to create a DataAsset that inherits from "SkrRPCInterfaceComponentDescription".


Replicate Actor

  • Then, click on the "+" at the right of the "ComponentClassToSpawn" to create a new SkrRpcInterfaceComponent, and save it into another directory (only data assets should be saved in the "RegistrableAssets" directory.


Replicate Actor

  • Once created, open it an create a custom event:


Replicate Actor

  • Then, in the details panel, select "Run on Server" in the replicates field and fill it with your code. This code will be executed on the server even if it is called from the client side. With the same example as above, the function will edit the "TestVar" created previously.


Replicate Actor

Replicate Actor

  • Once this is done the last things to do is to call this method from anywere in the code. On the example, in the replicated actor, a method called "ChangeVisibilityFromClientSide" that anybody can call on any instance of the aplication, will call the fonction created in the RPC component.


Replicate Actor

At this point, the visibility of the object can be set on the client and the server and will be automatically replicated unsing Unreal replication system. The only thing to do to change the visibility is to call "ChangeVisibilityFromClientSide" anywhere in the code without taking into account the owner of the object.

Spawning an actor as a client

One of the most comon issue with network is when the developer needs to create an object from the client side. The solution provided by Unreal is to call an RPC function with any params needed, but because there are no return value in the RPC call, the client will never ave a direct access to this object. To solve this issue, we have created an RPC component interface called "SkrActorSpawnerRpcDispatcher". This dispatcher allow to create an actor from everywhere with a latent function (meaning that there are several frames between the input of the function and the output without blocking the game thread). Bellow a sample with the spawning of a simple actor: