Sessions

Starting a session #

One call starts each kind of session. The calls return an empty string on success, otherwise the reason.

local net = NetcodeRequestBus.Broadcast
net.LoadSchema("netcode/game.netschema")

local why = net.StartHost(7777)              -- Listen Server
-- or net.StartDedicatedServer(7777)
-- or net.StartStandalone()

-- on another machine
local why = net.StartClient("192.168.1.20:7777")
if why ~= "" then Debug.Log("could not start: " .. why) end

Stop() ends a session. GetStateName(), IsConnected() and GetConnectionCount() support a HUD. The states are Idle, Connecting, Hosting, Serving, Standalone and Connected.

Starting from settings #

A launcher or headless run can start itself with registry settings.

--regset=/Heathen/Netcode/AutoStart/Mode=server|host|client|standalone
--regset=/Heathen/Netcode/AutoStart/Port=7777
--regset=/Heathen/Netcode/AutoStart/Address=192.168.1.20:7777

Playing from the Editor #

The Netcode toolbar has a Play button whose label shows the current setup, for example Host + 2. It reads Host + N, Dedicated + N or Join with an address. Settings holds the mode (Listen Server, Dedicated Server or Join a game), Transport, port, the number of game windows and the game window size. For a Dedicated Server it starts the project’s server launcher and then the clients one at a time, so the project must build that launcher.

The Netcode toolbar with its Play button reading Host + 2.
The Play button shows the current setup.
The Netcode Play settings window with Mode, Transport, Port, the number of game windows, window size and extra arguments.
The Play settings.

The Session page shows the state, connections, metrics and every NetRecord with its owner, observers and field values. You can pause it to read values. It is live for a Listen Server only. A Dedicated Server is a separate program, so the Editor shows nothing for it: the page says so and gives the path of the server’s log.

A Listen Server and two Clients running the Arena sample, with the Netcode Session page showing three connections and the list of NetRecords beside both client windows.
The Session page beside a host and two clients showing the same arena.

Joining #

A new connection is Connecting until an admission step answers Admitted or Rejected. Rejection reasons are Full, VersionMismatch, DefinitionsMismatch, AdmissionRejected, Timeout, Kicked, Replaced and TransportError. The Any Server can kick a connection.

The Any Server creates one player NetRecord for each admitted connection, using the type the Schema marks as the player. When a connection ends, the NetRecords it owned are removed.

Finding games on a LAN #

AdvertiseGame, SearchForGames and GetFoundGameAddress provide LAN discovery. Discovery uses IPv4 only.

Stopping #

Stop() raises Stopping, with everything still in memory, and then Stopped after the store is purged. The gem does not manage your game objects on stop, so the game decides what a stop means. State can be carried across a stop with TakeNetState, OverwriteNetState and AddToNetState.

Testing under bad conditions #

SetSimulatedConditions(latencyMs, jitterMs, lossPercent) on the access bus adds latency, jitter and loss so you can test without leaving your desk.

Rate This Article!