Multiplayer

Turian does not yet ship a networking library. What it has today is a foundation for multiplayer games: a way to build a client and a dedicated server from the same project, a simulation that ticks at a fixed rate with or without a window, and a contract for applying player commands in a fixed order so that an authority can decide the outcome. Sending those commands over a network, synchronising objects between machines, and matchmaking are the game's own code for now. This page says what exists, what each part is for, and what is not there.

What exists today

A dedicated server build

A project can define several build profiles. A profile whose target is Server builds the game without a window, renderer or input device. Bricks that depend on the renderer are left out, the scripts compile with the symbol TURIAN_SERVER, and the game runs the engine's headless player. The headless player ticks the scenes at the fixed simulation rate until Application.Quit is called, or until the process gets Ctrl+C or SIGTERM.

Application.IsHeadless tells a script which kind of build it is running in. The client and server example project (example-08-client-server) uses both: the same scene and scripts build as a windowed Client and as a headless Server. Its ServerClock component logs the simulation once per second and quits after a set time, which makes the server usable as a smoke test:

namespace Usercode;

public class ServerClock : Component
{
    public float QuitAfterSeconds { get; set; }

    float elapsed;
    float nextReport = 1f;

    public override void OnStart()
    {
#if TURIAN_SERVER
        Log.Logger.LogInformation("Dedicated server started; headless: {Headless}", Application.IsHeadless);
#else
        Log.Logger.LogInformation("Client started; headless: {Headless}", Application.IsHeadless);
#endif
    }

    public override void OnFixedUpdate(float fixedDeltaTime)
    {
        elapsed += fixedDeltaTime;
        if (elapsed >= nextReport)
        {
            Log.Logger.LogInformation("Simulated {Seconds:F0} s", elapsed);
            nextReport += 1f;
        }

        if (QuitAfterSeconds > 0 && elapsed >= QuitAfterSeconds)
        {
            Application.Quit();
        }
    }
}

Code that uses the renderer, a window or the in-game UI belongs in an #if !TURIAN_SERVER branch, or the server build fails to compile. The example's README gives the export command, run from the Turian checkout, which writes both builds to Output/Client and Output/Server:

dotnet run --project Turian/Editor/CLI -- export ../Turian-examples/example-08-client-server --profile Client --profile Server

A fixed-rate simulation

OnFixedUpdate runs at a fixed 60 steps per second. The headless player uses the same runtime as the windowed game, with a different application kind, so a server and a client step their scenes at the same rate. Gameplay that should advance identically on every machine belongs in OnFixedUpdate rather than OnUpdate, whose step depends on the frame rate. See Components for the order of the callbacks.

An authoritative simulation contract

For state that must match on every machine, the engine provides a small set of types. They are useful once you decide which machine is the authority.

Type Role
SimulationSession Owns the commands, the random streams and your world, and advances them on one clock.
ISimulationWorld The game's own authoritative state. It validates and applies a command at its tick, runs the game's systems in a fixed order, and captures and restores its state as JSON.
SimulationCommand A command stamped by the authority with the tick it applies at, an admission sequence, the producer's identity, a request sequence, a kind and a versioned JSON payload.
SimulationCommandLimits Bounds on pending commands, producers, payload size and how far ahead a command may be scheduled.
SimulationCheckpoint A completed tick, holding the clock, the random streams, the pending commands and the game's state.
SimulationStateHash A SHA-256 of a JSON state, with object keys sorted, for comparing two copies of a simulation.
SimulationRandomStreams The seed and named random streams. Presentation code uses its own random source.

A producer, such as a player's client, requests an action by calling Schedule with its identity, a sequence number that counts up from one, a command kind and a payload. The session rejects a sequence number it has already accepted, and it applies accepted commands at tick boundaries in target-tick and admission order. The results of the last tick are available in LastResults, and a TickCompleted event fires after each one. Trusted admission records can be fed back with ScheduleRecorded to replay a session.

The session is not created for you. A game constructs it with its world, a SimulationClock that has not ticked yet, a seed and a compatibility string that identifies the build, content and command schemas. Registering it as a service is enough: the runtime adds AddSimulationLoop by default, and that loop drives any SimulationSession it finds. SimulationSession.CaptureSave creates a save snapshot that carries a checkpoint under the extension name org.mass4.turian.simulation.

What is not there

  • No transport. The engine has no sockets, connections, reliable or unreliable channels, or serialisation of messages between machines. A game chooses its own networking library and connects it to Schedule on the authority and to a client's request path.
  • No authentication. Schedule trusts the producer identity it is given. The docs of the session say that establishing who a producer is, and what it may do, belongs to the authority code that calls Schedule.
  • No replication. Nodes and components are not synchronised between machines. A server sends the state it needs in whatever form the game chooses, and clients apply it themselves.
  • No prediction, interpolation or lag compensation. A client that wants to hide latency must write that itself.
  • No lobby, matchmaking, rooms or host migration.

If you need those now, build them on top of the session and the dedicated server build, and keep the authoritative state inside ISimulationWorld so it can be checkpointed and hashed.

A minimal setup

  1. Create a second build profile with the target Server, next to your client profile. The build profiles page describes each setting.
  2. Put client-only code (windows, rendering, UI) in #if !TURIAN_SERVER, and gameplay code in the shared path.
  3. Run the server export, then start Output/Server/<your game> on a machine without a display. It logs the fixed-rate simulation and keeps running until Ctrl+C.
  4. Only after this works, add your transport and connect its messages to SimulationSession.Schedule.

Next steps


← All docs Edit this page