Replication Server

The Replication Server replicates the state of the world to all connected Clients and Simulators.

To understand what is happening in the game world, and to be able to contribute your simulated values, you need to connect to a Replication Server. The Replication Server acts as a central place where data is received from and distributed to interested clients.

You can connect to a Replication Server in the cloud, but we recommend that you first start one locally on your computer. coherence is designed so you can easily develop everything locally first before deploying to the cloud.

Replication Servers replicate data defined in schema files. The schema's inspector provides all the tools needed to start a Replication Server.

  1. Run the Replication Server by clicking the run button.

  2. A terminal/command line will pop up running your server locally

  3. The port the Replication Server will use. Default: 32001.

  4. The Replication Server frequency. Default: 60.

You can also start the replication server from the coherence menu or by pressing ctrl+shift+alt+N.

If you're unsure where schema files are located, you can easily search through the project using Unity's project search window, witht:Coherence.SchemaAsset

To connect with multiple clients locally, publish a build for your platform (File > Build and Run, details in Unity docs). Run the Replication Server and launch the build any number of times. You can also enter Play Mode in the Unity Editor.

For Mac Users: You can open new instances of an application from the Terminal:

open -n <path to .app>

Connecting to a Replication Server

When the replication server is running, you connect to it using the Connect command from one of your component systems.

Connect to a Replication Server

using Unity.Entities; 
using Coherence.Replication.Client.Unity.Ecs;

class MyComponentSystem : SystemBase 
{ 
    protected override void OnCreate() 
    { 
        World.GetExistingSystem().Connect("127.0.0.1:32001"); 
    } 
}  

After trying to connect you might be interested in knowing whether the connection succeeded. The Connect call will run asynchronously and take around 100 ms to finish, or longer if you connect to a remote server. One way is to poll the NetworkState once each tick until we know that it has succeeded. It is also possible to listen for a [network event]().

Check if we are connected

protected override void OnUpdate() 
{ 
    // Method 1 
    if (World.GetExistingSystem().IsConnected) 
    { 
        // Yes, we are connected! This code will run every frame. 
    }

    // Method 2
    if (World.GetExistingSystem<NetworkSystem>().State == NetworkState.Connected)
    {
        // Yes, we are connected! This code will run every frame.
    }
    
    // Method 3
    Entities.ForEach((in ConnectedEvent connected) =>
    {
        // Yes, we are connected! This code will run only once.
    }).ScheduleParallel();
}

Check 'Run in Background' in the Unity settings under Project Settings -> Player so that the clients continue to run when not the active window.

Make sure that your system is actually run, for example by adding the [AlwaysUpdateSystem] attribute to your system class. By default, Unity ECS will not run your system if it doesn't process any Entities.

Last updated