← Tutorials

Dedicated Server

Turian builds a client and a dedicated server from one project. The client is the game people play in a window. The server is the same scenes and scripts built for the Server target, which runs without a window, renderer or input device. This tutorial sets up both and runs them.

This tutorial takes about twenty minutes. You need the getting-started tutorial done. It follows the layout of the example-08-client-server example.

This tutorial does not add a network connection. The engine has no networking layer, so it covers building and running the two targets. Connecting them is up to your game and the transport you choose.

1. Create a project

turian-cli new HelloServer
turian-studio --project HelloServer

A new project starts with a Client build profile that is already active. You will add the server profile next.

2. Write a script for both builds

Create Assets/ServerClock.cs:

namespace Usercode;

/// <summary>Logs the simulation once per second on client and server alike.</summary>
public class ServerClock : Component
{
    /// <summary>Seconds after which the game quits; zero runs until stopped.</summary>
    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();
    }
}

Save it, then check that it compiles:

turian-cli compile HelloServer

The command ends by printing Output: followed by the compiled library's path.

The #if TURIAN_SERVER branch is compiled only into the server build. Turian defines that symbol for every profile whose target is Server. Application.IsHeadless tells you at run time which kind of player is running.

3. Attach it

Select Box in the Scene Tree, click Add Component in the Inspector and choose ServerClock. The Inspector now shows a QuitAfterSeconds field. Save the scene with Ctrl+S.

4. Create the server profile

Open Project → Build Profiles… in Studio. Click Add Build Profile and choose Desktop Server. The new profile is named Server. You can rename it in its fields, but the name must stay Server for the commands below to work.

The window now lists two profiles: Client, marked Active, and Server. Leave Client as the active one.

5. Give the server its own Player Settings

A server often needs a different product name, and Turian can keep that as a variant of the project's Player Settings, so the original stays unchanged. Create the variant with the command line:

turian-cli variant Assets/Settings/PlayerSettings.dataasset "Server PlayerSettings" --folder Assets/Settings/Build --project HelloServer

The command creates Assets/Settings/Build/Server PlayerSettings.dataasset. Open it and put the value you want to change under the __Variant block, in Overrides:

{
  "__Variant": {
    "Overrides": {
      "ProductName": "HelloServer Dedicated"
    }
  }
}

Back in Build Profiles…, add Server PlayerSettings to the Settings list of the Server profile. Now the server exports with that product name, and the client keeps the project's own.

6. Export both builds

turian-cli export HelloServer --profile Client --profile Server

Each --profile exports one profile, and both happen in one run. The client goes to Output/Client/<game>/ and the server to Output/Server/<game>/, each with an executable named after the project.

Turian checks the server build for client-only files. If a script uses the renderer, the window or the in-game UI outside an #if !TURIAN_SERVER branch, the server build fails with a message that lists the client-only files it would ship and asks you to guard that code. Guard the code and export again.

7. Run the server

On Linux or macOS, run the server executable from its folder:

Output/Server/HelloServer/HelloServer

On Windows, run HelloServer.exe instead. The server prints Dedicated server started; headless: True, then one Simulated N s line per second. It runs with no window, and it needs no display or GPU. Press Ctrl+C to stop it.

To check that it exits by itself, set QuitAfterSeconds on Box to 5 in Studio, export the server again and run it. It stops after about five simulated seconds, which makes it usable in a script.

8. Run the client

Output/Client/HelloServer/HelloServer

A window opens with the Box in the scene. Its log says Client started; headless: False. The same script runs in both builds, and the only difference is which branch of the #if was compiled.

Next steps

  • Export for Android — the same profile idea, with an Android profile.
  • Async tasks — keep slow work off the game loop, which a server needs most.
  • Build profiles — every field a profile holds, and how the server target leaves out client-only bricks.
  • CLI reference — the commands used on this page and the rest.

Edit this page