Dedicated Servers
A dedicated server is a build of your game that runs without a window, renderer or input device. It shares the scenes and scripts with the client, and its build profile sets the Target to Server. The Build Profiles page shows a client and a server profile side by side.
Create a server profile
Add a build profile in Project → Build Profiles… and set its Target to Server. A server profile:
- defines
TURIAN_SERVERfor its scripts, so code can branch on it; - leaves out the client-only bricks, such as the Vulkan renderer, and every brick that depends on one;
- runs the engine's headless player when the game starts.
Give the server its own Settings when it needs different Player Settings. Profiles are described in Build Profiles.
Scripts can check which build they run in. Application.IsHeadless is true on a server, and the #if TURIAN_SERVER branch is compiled only into the server:
namespace Usercode;
public class ServerClock : Component
{
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
}
}
Keep renderer, window and in-game UI code out of the server branch, or the server build fails.
Export a server
turian-cli export MyGame --profile Server
The server is written to Output/Server/MyGame/. On Windows the server is a console application, so its log and Ctrl+C work as they do in any console program. A desktop server is published as a self-contained executable that carries the .NET runtime, so the machine that runs it does not need .NET installed.
NativeAOT servers
A desktop server can be compiled ahead of time with NativeAOT, which produces a native executable with no runtime to start. Turn on Native AOT in the server profile and set the Runtime Identifier, such as linux-x64:
{
"Name": "NativeAOT",
"Platform": "desktop",
"Target": 1,
"Configuration": 1,
"Development": false,
"Settings": [],
"NativeAot": true,
"RuntimeIdentifier": "linux-x64"
}
turian-cli export MyGame --profile NativeAOT
What changes in a NativeAOT export:
- The game is published with
PublishAot, as a self-contained executable, and the build definesTURIAN_STATIC_RUNTIME. - Startup composition is generated at build time rather than discovered at run time. The export writes a
GeneratedExportRuntime.csfile into its generated project. It registers the project's types and their ids, the object factories, the JSON serializer context for value types, the asset reference converters, and the engine service modules, and it makes the headless player the game's player. - Editor-only paths, such as hot reload, stay dynamic and are not part of the export.
- NativeAOT is available only for a desktop server. Any other profile with Native AOT set fails the export with a message that says so.
The export logs the NativeAOT publish output at information level.
The NativeAOT server profile in example-14-dataasset-driven is the reference for this path. Its export has been verified on Linux x64 under NativeAOT. Other runtime identifiers are not covered by that check.
Run and stop a server
Start the server executable from a terminal, or from a service manager:
./Output/Server/MyGame/MyGame
The engine ticks the scenes at the project's fixed simulation rate and logs how fast it runs. Nothing else needs to be set up, and the server needs no display or GPU.
To stop it, send Ctrl+C or SIGTERM:
- The first interrupt asks the game to quit. The loop ends after the current frame, the runtime shuts down, and the process exits with the code the quit requested.
- A second interrupt ends the process at once. Use it only when a game has hung.
Game code can request the same shutdown with Application.Quit, and can pass an exit code:
Application.Quit(exitCode: 0);
Application.ExitCode holds the code the last quit requested. The engine handles the interrupt signals for the whole time the server loop runs.
Related pages
- Build Profiles for profile fields and the project asset.
- Android for exporting client builds to a device.
- CLI for every export option.