LogoLogo
⚠️ Outdated documentationGo to LatestHomeAPI
SDK 0.9
SDK 0.9
  • Welcome
  • Overview
    • What is coherence?
    • How does coherence work?
    • Features and Roadmap
    • Rooms and Worlds
    • Requirements
    • Release Notes
  • Get Started
    • Install coherence
    • Scene setup
    • Prefab setup
    • Baking and code generation
    • Build and run
    • Create a free account
    • Deploy Replication Server
    • Share builds
    • Troubleshooting
  • Authority and communication
    • How authority works
    • Authority transfer
    • Commands
    • Simulation frame
    • Client connections
    • Server-authoritative setup
    • GGPO
    • Animations
    • Value sync callbacks
  • Persistence
    • Overview
    • Configuring persistence
    • Storage
    • Example – a global counter
  • Optimization
    • Overview
    • Simulation frequency
    • Areas of interest
    • Level of detail
    • Interpolation
  • Connected entities
    • Overview
    • Entity references
    • Parent-child relationships
    • CoherenceNode
  • Simulators
    • Overview
    • Client vs Simulator logic
    • Build and deploy
    • Simulator load balancing
    • Room Simulators
    • Multi-Room Simulators (advanced)
    • World Simulators
    • Simulator slugs
    • Testing Simulators locally
  • Tutorial project
    • Get the Tutorial Project
    • Start Tutorial
      • 1. Transforms
      • 2. Physics
      • 3. Persistence
      • 4. Animation and variables
      • 5. AI navigation
      • 6. Network commands
      • 7. Team-based
      • 8. Connected Entities
  • Game Services
    • Game account
    • Key-value store
    • Matchmaking
  • Developer Portal
    • Overview
    • Dashboard
    • Enabling game services
    • Configure Rooms
    • Manage Worlds
  • API reference
    • coherence SDK
      • CoherenceSync
      • MonoBridge
      • LiveQuery
      • Level of detail
      • Sample UI
      • Settings window
      • Custom bindings (advanced)
      • PlayResolver
      • Rooms
      • Worlds
    • Cloud API
      • API tokens and keys
      • Game account
      • Key-value store
      • Matchmaking
    • Replication Server
    • Simulation Server
  • Schema reference
    • Overview
    • Specification
    • Field settings
    • Archetypes in schemas
  • Resources
    • Downloads
    • SDK update guide
    • Video tutorials
    • Order of execution
    • Glossary
    • CLI utilities
    • Simulator CLI arguments
    • Helper scripts
    • Troubleshooting
    • Continuous Integration setup
  • Community
    • Community
  • Additional information
    • Pricing
    • SLA
    • Unreal Engine support
    • WebGL
    • Peer-to-Peer (P2P)
Powered by GitBook
On this page
  • Creating a Simulation Server
  • Command line parameters in the cloud
  • SimulatorUtility
  • Builds
  • Build and deploy

Was this helpful?

Export as PDF
  1. API reference

Simulation Server

coherence uses the concept of ownership to determine who is responsible for simulating each Entity in the Game World. By default, each Client that connects to the Server owns and simulates the Entities they create. There are a lot of situations where this setup is not adequate. For example:

  • The number of Entities in the Game World could be too large to be simulated by the players on their own, especially if there are few players and the World is very large.

  • The game might have an advanced AI that requires a lot of coordination, which makes it hard to split up the work between Clients.

  • It is often desirable to have an authoritative object that ensures a single source of truth for certain data. State replication and "eventual correctness" doesn't give us these guarantees.

  • Perhaps the game should run a persistent simulation, even while no one is playing.

With coherence, all of these situations are can be solved using dedicated Simulation Servers. They behave very much like normal Game Clients, except they run on their own with no player involved. Usually they also have special code that only they run (and not the clients). It is up to the game developer to create and run these programs somewhere in the cloud, based on the demands of their particular game.

Creating a Simulation Server

If you have determined that you need one or more Simulation Servers for your game, there are multiple ways you can go about implementing these. You could create a separate Unity project and write the specific code for the Simulation Server there (while making sure you use the same schema as your original project).

An easier way is to use your existing Unity project and modify it in a way so that it can be started either as a normal Game Client, or as a Simulation Server. This will ensure that you maximize code sharing between Clients and Servers - they both do simulation of Entities in the same Game World after all.

Important: to build a Simulation Server, you have to build for the Linux platform.

To determine whether to start a build as a Client or as a Simulation Server, you can use command line arguments:

Reading command line arguments

using System;
using UnityEngine;

public class Boot : MonoBehaviour
{
    void Start()
    {
        var args = Environment.GetCommandLineArgs();
        var isSimulationServer = false;

        foreach (var arg in args)
        {
            if (arg == "--coherence-simulation-server")
            {
                isSimulationServer = true;
            }
        }

        /* do things based on 'isSimulationServer' here... */
    }
}

To pass the command line argument, start the application like this:

$ ./Game --coherence-simulation-server  

Command line parameters in the cloud

The Simulation Server is started with the following parameters in the cloud:

--coherence-region             // region where the simulator is started
--coherence-world-id           // unique world id (in world mode)
--coherence-http-server-port   // REST access for starting / stopping rooms
--coherence-auth-token         // token to authorize access
--coherence-simulation-server  // identify as simulation server
--coherence-simulator-type     // Room or World

// the following should be used also when starting a local simulation server to 
// successfully connect to the local replication server

--coherence-ip                 // address of the replication server (eg 127.0.0.1)
--coherence-port               // port of the replication server
--coherence-room-id            // room id when connecting to a room
--coherence-unique-room-id     // unique room id to connect to in room mode
--coherence-room-tags          // tags used when creating the room
--coherence-room-kv            // key values supplied when creating the room

SimulatorUtility

The SDK provides a static helper class to access all the above parameters in the C# code called SimulatorUtility.

static class SimulatorUtility
{
    public enum Type
    {
        Undefined = 0,
        World = 1,
        Rooms = 2
    }
    
    public static Type SimulatorType;         // Type of simulator: Room / World.
    public string Region;                     // Region where simulator was spawned.
    public string Ip;                         // Ip of the replication server.
    public int Port;                          // UDP port of the replication server.
    public int RoomId;                        // RoomId for current session. 
    public ulong UniqueRoomId;                // Unique RoomId.
    public ulong WorldId;                     // World Id (in worlds mode).
    public int HttpServerPort;                // Port used for REST commands.
    public string AuthToken;                  // Auth token used for authentication.
    public List<string> RoomTags;             // Tags for the room. 
    public Dictionary<string,string> RoomKV;  // Key Values for the room.
    public bool IsInvokedAsSimulator;         // Whether the instance was invoked as a simulator.
    public bool IsInvokedInCommandLine;       // Whether the instance was invoked on the commandline.
    public bool IsSimulator;                  // Identify whether simulator.  
}

Builds

Change your build target to be Linux and tick Headless Mode. Rooms with just Simulators always shut down automatically.

Build and deploy

Last updated 2 years ago

Was this helpful?

When building stand-alone builds, Unity also has an option for . This is great for Simulation Servers since we're not interested in rendering any graphics on these anyway. By using headless mode we get a leaner executable that is easier to deploy in the cloud.

Refer to the .

headless mode
Simulator: Build and deploy section