Matches

Every match runs on a server, and this machine's player connects to it. When the server is on this machine, that's over a loopback transport, so single-player and multiplayer are the same game with the same code. The engine assumes nothing about what a game does with a match, like pausing: games build that from inputs and state.

Starting a match from tide run

A game whose selected entry is a prefab or match scene starts playing at once. Startup uses --scene first, then settings.startScene, then Main. tide run can open it to others, or join one instead:

sh
tide run --host --stats                 # others can join: in a room, and on UDP port 7777
tide run --host 8000 --stats            # on another port
tide run --connect 192.168.1.5 --stats  # join another machine's match, by its address
tide run --join K7QF2M --stats          # join the room with this code
tide run --web --host --stats           # on the web, in a room only

To check a match with several players without opening a window for each, tide run --players 2 --script play.txt --stats plays them all at once with no window, and takes a picture from each machine (see Several players).

Testing a game online

A match on one machine, or between two at one desk, has next to no lag, so it can feel better than it will between players far apart: there, each machine sees what another player does later, and the prediction it showed meanwhile is corrected. To feel it, and tune the game for it, without a second machine on the internet, make the way to the match's server worse:

sh
tide run --connect 192.168.1.5 --latency 80 --jitter 10 --loss 2 --stats  # as if this machine were 80 ms away
tide run --players 2 --latency 80 --script duel.txt                        # player 2 is 80 ms from player 1's server
tide run --players 3 --latency 3:150 --script duel.txt                     # only player 3 is far away

--latency is milliseconds each way, --jitter up to that many more at random, and --loss the percent of datagrams lost. They apply to a match this machine joins; a machine that runs the server adds nothing. With --players, the run still repeats exactly, so a script can capture the joining player's view at the moment that matters, like a block that comes too late, and the same run shows it again after a change. On the web, a page's address takes ?latency=80, ?jitter=10 and ?loss=5. See A slow network.

Starting a match from code

A game that starts in a menu selects a local scene as its entry, such as a local Main, and its local code decides which match this machine is in:

Call What it does
Session.Start(scene) Starts a match on this machine, which runs its server
Session.Start() The same, in the selected startup prefab or match scene: for code that can't name the game's scenes, like a package's
Session.Open() Lets others join the match this machine runs: in a room, and on port 7777 too (not on the web)
Session.Open(port) The same, on another port
Session.Close() Lets no one else join; the players in the match stay
Session.Kick(player, message) Sends a player out of the match this machine runs, telling them why (the message is optional)
Session.KickAll(message) The same for every player on another machine
Session.Ban(player, message) Kicks a player and keeps them out for the rest of the match (the message is optional)
Session.Unban(player) Lets a banned player join again
Session.Join(code) Joins the match in the room with this code, like "K7QF2M", or on a network of the game's own, by the text that network takes
Session.Connect(address) Joins another machine's match by its address: "192.168.1.5", "192.168.1.5:7777" or a name like "localhost"
Session.Connect(address, port) The same, on another port than 7777
Session.Leave() Leaves the match. With host migration, a match this machine runs goes on without it
Session.End() Ends the match this machine runs, for everyone
Session.Watch(center, size) In a sharded world, sees that area wherever this machine's player is
Session.Unwatch() Stops watching it

Start names the scene the match starts in, with its values as for Scene.Load: Session.Start(Arena { size = 30 }). Without an argument, it uses the selected entry's authored prefab tree or scene defaults. If the entry is local, name a match scene explicitly. Join and Connect get whatever the server runs. Starting or joining a match leaves the one this machine is in first.

There's one kind of match. It starts closed, so single-player is just a match nobody else was let into. Open and Close change that at any time: a game can start alone and open its match to friends later, then close it once the party's full. Closing turns away anyone who isn't in the match, and opening it again uses the same room and port.

Kick and KickAll send players out, and they get the message with their Disconnected (see below). A kicked player always hears it: one whose network lost the goodbye is told the next time they're in touch with the server. It's a kick, not a ban: once they know, a kicked player can join again, as the same player, while the match is open. To end a party and play on alone, close the match and kick everyone:

tide
Session.Close();
Session.KickAll("Thanks for playing!");

Bans

Session.Ban(player, message) kicks a player, who goes offline with Banned and the message, and keeps them out for the rest of the match: each time they try to join again, open or closed, they're refused with the same Banned and message. Session.Unban(player) lets them in again, as the same player, while the match is open. Like kicks, bans only change a match this machine runs, and its own player can't be banned.

A ban is of the player as the match knows them, the PlayerID they joined as. Their PlayerID stays theirs while they're banned, so nobody new is given it, and a ban goes with the match when host migration hands it to another machine, message and all. A returning player is known by the secret their machine was given when it first joined, so a ban can't stop someone who comes back without it: from another device, after clearing what the game kept, or by joining a migrated match at its new host's address. A dedicated server whose host checks accounts binds the ban to the account as well, which a fresh start doesn't escape.

tide
view Players(Session session, mut Moderation moderation)
{
    if (!session.server) return;
    if (GUILayout.Button("Ban the cheater")) Session.Ban(moderation.reported, "Cheating");
}

```csharp
local scene Main { }

scene Arena
{
    int size = 20;
}

view Menu(Session session)
{
    if (session.state != SessionState.Offline) return;
    GUILayout.Area(Anchor.MiddleCenter)
    {
        if (GUILayout.Button("Play")) Session.Start(Arena);
        if (GUILayout.Button("Host"))
        {
            Session.Start(Arena { size = 40 });
            Session.Open();
        }
        if (GUILayout.Button("Join")) Session.Join("K7QF2M");
        if (GUILayout.Button("Connect")) Session.Connect("192.168.1.5");
    }
}

Session calls are statements, in views and local event handlers, and in the functions they call:

tide
void Host(mut Menu menu)
{
    Session.Open();
    menu.page = Page.Room;
}

Match code can't make them, as it runs the same on every machine: calling such a function from a system is an error at that call.

A menu that comes with tide

The bundled Rooms package adds Play, Host and Join menus, invite links and room codes to share, and volume settings. List rooms in the game's tide.packages to use it. For pre-match groups, ready flags, lobby browsing and quick match, use the Lobby package. Both menus are optional; sessions and room codes are built into the engine.

Rooms

Players find each other's matches by a room's code, like K7QF2M: 6 letters and digits, without look-alikes like 0 and O. Opening a match opens a room, and Session.room is its code at once, for the game to show; other players join it with Session.Join(code), from a browser or a desktop game alike.

tide
view RoomCode(Session session)
{
    if (session.room == "") return;
    GUILayout.Area(Anchor.UpperCenter)
    {
        GUILayout.Label($"Room {session.room}");
    }
}

Here are two machines, side by side. Each frame below is the game on its own, starting in its menu, in no match:

  1. In one, press Start a match: it plays alone, with its match closed. Move the pointer over the frame to steer its player.
  2. Press Open. Its room's code shows, and Copy code puts it on the clipboard.
  3. In the other frame, type the code into the field, or click the field and paste, and press Enter or Join. Both players are now in one match, each frame showing it from its own machine.
  4. Try Close and then Leave and Join again: a closed match turns the player away, and its menu says why. Kick all sends a message along, and End ends the match for both.
Open a room, and join itLive Tide example, two players, tap it to play
Open separately

Player 1

Player 2

tide
settings { title = "Rooms"; tickRate = 30; maxPlayers = 4; }

local scene Main { } // The program starts in its menu, in no match
scene Arena { }

component Body
{
    [Position] float2 position; // Drawn gliding to where a correction put it
    Color color;
}
singleton Roster { Map<PlayerID, Entity> bodies; int arrivals; }

// Where this machine's pointer is in the arena, which its input sends.
local singleton Aim { float2 world; bool moved; }
local singleton Menu
{
    string code;
    string message = "Not in a match";
}

input Steer
{
    // A place, not something a player does: what another machine guesses of it stays
    [Absolute, Clamp(-3, 3)] float2 target;
    [Absolute] bool steering;

    Sample(Devices devices, Aim aim)
    {
        target = aim.world;
        steering = aim.moved; // Not before the pointer was ever on this window
    }
}

// The match, the same on every machine in it: a body for each player.
event(PlayerJoined joined) Arrive(mut Roster roster)
{
    var seat = roster.arrivals % 4;
    roster.arrivals += 1;
    var color = seat == 0 ? Color(0.98, 0.75, 0.14) : seat == 1 ? Color(0.18, 0.83, 0.75) : seat == 2 ? Color(0.98, 0.44, 0.52) : Color(0.47, 0.72, 1);
    roster.bodies[joined.player] = Spawn(Body { position = float2(seat - 1.5, 0), color = color }, Owner { player = joined.player });
}

event(PlayerLeft left) Depart(mut Roster roster)
{
    roster.bodies[left.player].Destroy();
    roster.bodies.Remove(left.player);
}

system Follow(Steer steer, mut Body body, Time time)
{
    if (steer.steering) body.position += (steer.target - body.position) * Math.Min(1, 6 * time.dt);
}

// This machine's side of it: which match it's in is local code's to say.
view Lobby(Session session, mut Menu menu)
{
    Draw.Clear(Color(0.086, 0.086, 0.094));
    GUI.SetTheme(GUITheme { radius = 6 });
    GUILayout.Area(Rect(8, 8, Screen.width - 16, session.state == SessionState.Offline ? 118 : 86))
    {
        if (session.state == SessionState.Offline)
        {
            GUILayout.Label(menu.message);
            if (GUILayout.Button("Start a match")) Session.Start(Arena);
            GUILayout.Horizontal()
            {
                GUILayout.TextField("Code", menu.code, out var entered);
                if ((GUILayout.Button("Join") || entered) && menu.code != "") Session.Join(menu.code);
            }
        }
        else if (session.state == SessionState.Connecting)
        {
            GUILayout.Label($"Joining room {menu.code}...");
            if (GUILayout.Button("Cancel")) Session.Leave();
        }
        else if (session.server)
        {
            GUILayout.Label(session.open ? $"Open, in room {session.room}" : "Closed: nobody else can join");
            GUILayout.Horizontal()
            {
                if (session.open)
                {
                    if (GUILayout.Button("Close")) Session.Close();
                    if (GUILayout.Button("Copy code")) Clipboard.Copy(session.room);
                }
                else if (GUILayout.Button("Open")) Session.Open();
                if (GUILayout.Button("Kick all")) Session.KickAll("The host cleared the room");
                if (GUILayout.Button("End")) Session.End();
            }
        }
        else
        {
            GUILayout.Label($"In room {session.room}, {session.ping} ms from its host");
            if (GUILayout.Button("Leave")) Session.Leave();
        }
    }
}

local event(Disconnected gone) Say(mut Menu menu)
{
    menu.message = gone.reason == DisconnectReason.Kicked ? $"Kicked: {gone.message}" : $"Out of the match: {gone.reason}";
}

view Point(Devices devices, mut Aim aim)
{
    Draw.SetCamera(float2(0, 0.6), 2.4);
    aim.world = Draw.ScreenToWorld(Screen.Flip(devices.pointer.position));
    if (Math.LengthSq(devices.pointer.delta) > 0) aim.moved = true;
}

view Others(Body body, without ownership)
{
    Draw.Circle(body.position, 0.22, body.color);
}

view Mine(Body body, with ownership)
{
    Draw.Circle(body.position, 0.22, body.color);
    Draw.WireCircle(body.position, 0.32, 0.04, Color.white);
    Draw.Text("you", body.position + float2(0, 0.4), 0.22, Color.white, Anchor.LowerCenter);
}

The relay, a server of ours, only introduces players to each other. Their packets then go straight between them, over WebRTC, which finds a way through their routers. When their networks can't connect directly, a TURN server carries the packets instead. The host's machine picks the code itself, so there's nothing to wait for. If another room had it first, the host picks another before anyone could read it out, and Session.room changes. It's "" while the relay can't be reached, and the room opens again once it can: players already in keep playing, and only joining needs the relay.

Session.invite is what a player sends a friend to join their room. On the web it's a link: the game's page, wherever it's served, with ?join= and the room's code, like https://example.com/game/?join=K7QF2M. Opening it starts the game in that room, as ?join= in a page's address always does. On desktop and Android there's no page to link to, so it's the room's code itself, which the friend types in. It's "" whenever Session.room is, and on the web until the relay confirms the host's room is registered.

tide
view Invite(Session session)
{
    if (session.invite == "") return;
    GUILayout.Area(Anchor.UpperCenter)
    {
        // A link on the web, the code elsewhere
        var link = session.invite != session.room;
        if (GUILayout.Button(link ? "Copy the invite link" : "Copy the code")) Clipboard.Copy(session.invite);
    }
}

Where this machine stands

Session is a built-in local singleton, taken as a parameter like any other, and read-only:

Field What it is
state SessionState.Offline, Connecting or Connected
player This machine's PlayerID, once connected
ping The network's round trip to the server, in milliseconds: what the machines wait for their next frame to read a packet isn't in it
server Whether this machine runs the server
open Whether others can join it (only the server's machine knows)
room The code of the room the match is in, or "" if it's in none, as when it's closed. On a network of the game's own, what that network says the match is joined by
invite What others open to join the match: on the web a link to the game's page with ?join= and the room's code, elsewhere the code itself (see Inviting with a link). "" when room is, and while a web host waits for the relay to register its room

A single-player match is a match too, so state is Connected there as well: this machine runs the server, and its player connects to it without going through the network. That's server true and open false. $"{session}" shows all of it at once.

The built-in local events Connected and Disconnected say when that changes. Connected is sent once the match's world has arrived and this machine plays in it. Disconnected has a reason:

Reason Why
Left This machine called Leave, or started another match
TimedOut The server stopped answering, or never did
Refused The server runs another build of the game, has no room left, or its match is closed
ServerLeft The server's machine left, which ended the match
Failed It couldn't start: no network, a port in use, an address that isn't one, or a room nobody has
Ended The match's last scene unloaded (see Scenes), or the machine running it called Session.End()
Kicked The server's machine sent it away, saying why in message
Banned The server's machine banned it, or turned it away as banned, saying why in message
tide
local event(Disconnected gone) BackToMenu(mut Menu menu)
{
    menu.message = gone.reason == DisconnectReason.Kicked ? $"Kicked: {gone.message}" : $"Disconnected: {gone.reason}";
}

Dedicated servers

tide run --server --anonymous starts a native server with no window, graphics context, audio device, views or player of its own. One process can run many independent matches of the same game. Matches own separate worlds, players, inputs, commands and clocks; they share game code, immutable assets and a worker pool. Matches currently update in order, with parallel systems within each match. Game C must keep match-specific state in its world or an explicit context; process-wide C globals are shared. A native C crash still affects the process.

For quick startup, clients request a match with Session.Connect("localhost:7777/0"), or Session.Join("K7QF2M/0") through the server's WebRTC room. Replace 0 with an existing match ID to join that match. The command line accepts the same address through --connect or --join. Session.room reports the endpoint with its assigned match ID once admitted. A match ID identifies a match; it grants no permission by itself. A sharded game's server runs one world split across its matches instead, and match 0 joins it.

Anonymous creation and joining are explicit testing policies. Without --anonymous, configure authentication and admission policy through the server entry point. Creation and joining are authorized separately, so a backend can create reserved matches while players only receive permission to join them. Verification can complete asynchronously without blocking simulation. Accepted identities bind player reconnections; credentials and account metadata stay outside replicated worlds.

The built-in client address form is anonymous. Authenticated clients and trusted services use tide/hosting.h with a game's verifier and credentials. Credentials require WebRTC/DTLS or a custom transport that provides equivalent protection; the hosting layer refuses credentials over its plain UDP path. Tide supplies the admission interface, not an account database or a particular identity provider.

The server starts matches in the selected entry when it is a prefab or match scene, using --scene, then settings.startScene, then Main. If the selected entry is a local menu, use --scene to select a match prefab or scene for the server, or provide a process entry point that selects the match scene and its typed startup parameters. See server options.

Server builds can omit assets reached only from client code. Assets used by simulation, including helper calls, defaults and dynamic match asset references, remain available. Server and client retain the same match compatibility identity; different pack bytes use the existing self-contained snapshot transfer path.

Host migration

When the machine running a match leaves, the match ends for everyone, unless the game turns on host migration in its settings:

tide
settings
{
    hostMigration = true;
}

Then, when the machine running a room's match leaves or stops answering, another player's machine takes the match over, and the others join it again as the same players:

  • A game that hides state from some players (visibility) can't turn it on: no player's machine has the whole match to go on from, and the compiler says so. Such a match runs on a dedicated or hosted server when it has to outlast its players.
  • The match goes on from the last tick the new host had verified. Players' predictions after it are rolled back, as when any guess is wrong.
  • The last host's player leaves the match (PlayerLeft), and from then on entities without an owner read the new host's input.
  • Local code sees no Connected or Disconnected while the match changes hands: Session.state is Connecting meanwhile, and views keep showing the last world they had. On the new host, Session.server becomes true.
  • A player who doesn't come back to the new host within 20 seconds leaves the match.
  • A host that leaves says so, and the match changes hands at once. One that stops answering is noticed after a few seconds.
  • Session.End() ends the match for everyone instead: every player gets Disconnected with Ended, even one who missed the goodbye, as the relay keeps the room as ended for a while. So does a match whose last scene unloads.
  • Only matches in a room change hands, as the room is where the players meet again, or on a network of the game's own that says where its players meet. A closed match stays closed: its players come back, but no one new joins.

This migration path requires the full authoritative world. Visibility-enabled clients receive partial worlds and cannot currently promote one into a host: Tide refuses that takeover until a trusted complete-state handover exists. Players can't pass for each other: they only ever get each other's cookies hashed (see Coming back).

A machine that sleeps

A machine that sleeps, such as a Steam Deck, a laptop or a phone, is gone from its match all the while. It leaves a match other machines can be in, as Session.Leave() does: local code gets Disconnected with Left, and goes on from there as after any leave. That's a match it joined, or one it runs that's open or has other players in it. The others' match goes on without it, through host migration when the game has it, and the player can join again as the same player.

On Linux, a Steam Deck's SteamOS among them, the machine leaves as it goes to sleep, with a goodbye, so the others' match changes hands at once: the system waits a moment for the game to leave first. Elsewhere it leaves when it wakes, and the others notice it went a few seconds after it stopped answering.

A game that's only in the background, minimized or behind another app, isn't asleep: its match goes on at its full tick rate, drawing nothing, until the player comes back. A phone's system may stop an app in the background after a while, which is as if it slept.

A match a machine runs alone and closed, as single-player is, goes on where it stopped: nobody waited for it.

On the web, the browser freezes a page it hides, and the page's match is noticed the way any silent machine is.

Players in the match

In the match, players come and go through the built-in events PlayerJoined and PlayerLeft, with the player's PlayerID. The server's own player joins before the match's first tick.

tide
event(PlayerJoined joined) GiveBody()
{
    Spawn(Body, Owner { player = joined.player });
}

What each player controls is up to the Owner component (see Input).

Coming back: joining a server gives this machine a cookie, and joining the same server again presents it, so the player gets their PlayerID back, and with it whatever the game kept for them. If the server still thought they were connected, the new connection takes over with no events at all; if they'd left, PlayerJoined comes again with the same PlayerID. The cookie lasts as long as the program, for now.

Input delay

By default there is none: a player's own input applies at once. Clients run ahead of the server, by about half the round trip, so their input arrives in time. Only other players' inputs are ever guessed, and when a guess is wrong, the client rolls back and runs the ticks again. See Networking for how.

Running ticks again costs time: a wrong guess runs about as many ticks again as the client is ahead, and on a busy match that's most ticks. A client measures what its rollbacks cost and keeps its delay at the least where they fit: one rollback may take up to about a frame, and never less than a 60th of a second, and all of them together up to a budget an update (4 ms, unless the host says otherwise), on average. Past either it delays its input a tick at a time, and takes the delay back within a second or two of there being room again, or of there being no rollback at all. A client whose guesses are right pays no delay for ticks it never runs again, and the player on the server's own machine never has any. tide run --stats shows the delay when there is one.

A game can ask for delay in place of prediction altogether:

tide
settings
{
    prediction = Prediction.Delay;
}

Then clients never predict. Their inputs are delayed so that every player's reach the server before its tick, and ticks run once each, as the server confirms them: nothing is ever rolled back, at the cost of input delay of about a round trip and a half. A game whose ticks are expensive, or whose views can't show a rollback well, picks it; the server runs the match the same either way.

During a state repair the client may also simulate authenticated ticks privately to compare against the server's repair payload and reduce its download. That comparison consumes CPU in Delay and partial-prediction modes too; it publishes no speculative state or events. Adopting a repair can skip simulation across the replaced interval, so compare CPU alongside confirmed progress and repair traffic when measuring prediction modes.

Predicting what's near

To predict only this player's entities and their descendants, use settings { prediction = Prediction.Owned; }. Ownership follows the nearest ancestor with Owner, including the entity itself. Other entities stay at their confirmed state, including nearby entities and entities without positions. Every confirmed tick still simulates the whole permitted world. During Owned prediction, the compiler runs singleton systems only when they contribute to owned simulation, following component/singleton dependencies through helpers, queries and event handlers. An ordering attribute alone does not include a system. Their queries can read other entities as they stand, or explicitly use without Far to exclude them. Far names entities excluded from prediction in this mode too. Ownership and parent changes are checked before each predicted tick. With nothing owned, entity systems predict nothing. This mode does not take predictionRange.

Selection is conservative: writes through mut count as reads too, possible owned spawners are retained while anything is owned, and dynamic structural changes and shared match tasks can retain additional work. Declared C access is trusted as usual. Excluded singleton values remain at the prediction baseline; take a verified parameter in a view for the current confirmed value. Unrelated singleton differences do not invalidate retained predictions, but input changes, structural state and whole-heap differences still can. This setting does not promise that every owned controller has a small dependency set.

A big world can predict only what's near each player:

tide
settings
{
    predictionRange = 40;
}

A client then runs its predicted ticks only for entities within 40 world units of its player's own entities (those whose Owner is the player), along each axis. What's farther stays as the server last confirmed it. It runs once a tick, as each tick is confirmed, as with Prediction.Delay, and is drawn that far behind. In predicted ticks, what's near meets what's far as it stands: a far entity can still be read or queried, but systems and handlers that run for each entity don't run for it until the tick is confirmed. Rollbacks then run again only what's near, so a busy world costs a client less input delay. In exchange, the client runs every confirmed tick for the whole world once. While what's near goes as it predicted, its predicted ticks then stand, with what's far as of an older confirmed tick, up to a quarter of a second older; past that, or when what's near went otherwise, it runs them again.

An entity's position is its first [Position] field, or its Transform's, as with shards; an entity in a hierarchy goes by its root's. One with neither is always predicted, as is anything spawned in a predicted tick. A player who owns nothing with a position predicts everything. Views draw far entities as confirmed, blended between confirmed ticks as verified parameters are, without correction smoothing; components holding text, lists or grids move a tick at a time. An entity crossing the range jumps between where it was predicted and where it was confirmed. A system written in C is handed the runs of near entities. The setting needs Prediction.Rollback.

Code that goes through every entity at once, like a singleton system's query, still runs them all. It can leave far entities out itself with the filter Far: without Far takes what's near, and with Far what's far, in a system's or a query's parameters. Only a client's predicted ticks have anything far, so on the server and in confirmed ticks without Far takes everything:

tide
system Separate(mut Crowd crowd, query (Body body, without Far) near)
{
    // ...
}

The physics packages do this for their step: in a predicted tick, far bodies are held where they were confirmed, and near bodies meet them as walls that don't move. A far body that touches a near one still moves in the physics world, though its component stays as confirmed.

How many players

A match takes 16 players unless the game says otherwise:

tide
settings
{
    maxPlayers = 64;
}

maxPlayers goes from 1 to 65,536, the same supported ceiling as a dedicated server's total connections. The settings are separate: one limits a match, the other limits connections across all of the server's matches. One player more is refused, as from a full match (DisconnectReason.Refused). The world keeps an input for every player it can take, so the number is part of the game's data: builds with different maxPlayers can't share a match, and changing it under tide run starts the match over.

Pick what the game is built for. In a full-world match every player receives every other's input and simulates the whole world, so traffic can grow with the square of its players. Visibility restricts state, simulation and owner-input delivery, while the server still simulates the complete match.

Limits for now

  • Under tide run --web, a reload plays on alone: the room closes, and the other players drop out.
  • A match can't start in a scene that holds text or lists yet.