Skip to main content

Networking and players

This area covers how a server starts, how a player joins, how the two sides send each other data, and the types that make up a player: the Player entity, its body, its states, how it moves and how it's saved.

Everything here is in Root.Township unless a page says otherwise. The transport sits in Alta.NetworkTransport.Runtime, and the bit streams every message is written with are in Alta.Serialization.Runtime. Add those two to your references if you send or read messages:

<Reference Include="Alta.NetworkTransport.Runtime" HintPath="$(Managed)Alta.NetworkTransport.Runtime.dll" Private="false" />
<Reference Include="Alta.Serialization.Runtime" HintPath="$(Managed)Alta.Serialization.Runtime.dll" Private="false" />

The shape of it​

Which side am I on?​

CheckTrue when
NetworkSceneManager.IsServerThis process is running the server (also in local test mode). Also true on a client in the main menu, whose LocalNetworkScene sets it.
NetworkSceneManager.IsLocalTestLocal test mode: server and player in one process.
NetworkSceneManager.IsRemoteServerA real server, not local test.
ServerHandler.Current != nullA server has started. Also true in the main menu's dummy server, so check IsDummyLocalServer.
CommandLineArguments.Contains("/start_server")The process was started as a server. Works before anything else has loaded.
ApplicationManager.IsHeadless-batchmode or -nographics was given. Rover's bots are headless clients, so this alone doesn't mean "server".

NetworkSceneManager.IsServer is set when the server socket opens (or the main menu loads), and set to false on a client as it loads into a server, so it isn't meaningful at mod load. Use the command line then. Client, server or both shows the pattern.

Two builds of Root.Township

Alta shipped a client build of Root.Township with most server code compiled out. In it, ServerHandler has only Current, CurrentServerId, Initialized, ServerConfig and the dummy-server members, and there's no ServerPlayerConnectionHandlerOld. The server build has all of it. The game installs we checked (TavernLib's server and client folders) and att-stubs all use the server build, so these pages describe that one. If your mod touches server-only members and a player runs the client build, expect MissingMethodException or TypeLoadException there.

Pages​

PageWhat's in it
Server lifecycleFrom /start_server to a running server, the bootup steps, events in order, and shutdown.
ServerHandlerThe running server: player limit, join and leave events, save utility, config.
Joining a serverThe join handshake on both sides, every check the server makes, and TavernLib's extra ones.
Sockets and connectionsNetworkManager, Socket, Connection, channels, the transport, threading.
Connection messagesMessageType, sending and handling, custom message types, size limits.
Entity syncEntity messages, MethodSync, who may send what, the per-frame serializers, spawning.
StreamAlta.Serialization.Stream: the bit reader and writer under every message and save.
PlayerThe Player entity, identity (UserInfo), player modes, finding players, events.
PlayerController and the bodyPlayerController, PlayerCharacter, local and remote bodies, which body each mode gets.
Player statesPlayerStateManager and the states: first join, playing, combat, downed, dead, spirit.
LocomotionLocomotionController, SmoothLocomotion, fly cam, and moving a player from the server.
SavingPlayer saves, the world save pass, folders on disk, save blockers.
Server settingsServerSettings, ServerConfiguration.json, the in-game settings board, server info types.
Command-line flagsEvery flag the game reads that matters for servers and clients, and the ones TavernLib adds.

Chunks, streaming and what's spawned where belong to World, entities and spawning. Items, docks and inventories belong to Items, interaction and damage. Console commands and ConsoleEvents belong to Console, utilities and Alta assemblies. TavernLib's own messages (32 and 33) and how it widens the message type are on TavernLib's network messages.

Most useful hook points​

Want toUse
Run code when a player joins or leaves (server)ServerHandler.Current.PlayerJoined / PlayerLeft. See ServerHandler.
Know when any Player entity appears (both sides)Player.OnPlayerAdded / OnPlayerRemoved. See Player.
Refuse or delay a joinA connection step, or a patch on the join handler. See Joining a server.
Send your own data between server and clientA custom MessageType. See Connection messages.
Move a player from the serverplayer.PlayerController.ForceTeleportTo(position). See Locomotion.
React to downed, dead, combatplayer.StateManager.StateActivated. See Player states.