# Multiplayer

Let 2 to 8 players on different machines play one game. You write the game as you would
for players sharing a keyboard: read each player's input by number, and the engine keeps
every machine's game the same.

It works by rollback. Because a Coldwave game [plays the same on every
machine](../concepts/steps.md) for the same input, players send each other their inputs
and nothing else, and every machine plays the whole game. A player's input reaches the
others a few steps late; until it arrives, each machine predicts it, and when the real
input was not what was predicted, that machine puts its world back a few steps and
plays them again. You write no network code for any of it.

> **Note.** Multiplayer today is for games among friends on one network: players
> connect directly to the host's address over UDP. There is no relay, no lobby that
> finds games and no multiplayer in the web player yet, and a game in C# is refused.
> See [not yet](networking.md#not-yet).

## Make a game for two or more

```toml
[input]
actions = ["move"]
players = 2            # 2 to 8 over a network

[net]                  # optional; these are the defaults
delay = 2              # steps a player's input waits before its step, 0 to 8
window = 8             # steps that may be played on predicted input, 1 to 16
sync_every = 30        # steps between checksum comparisons, 1 to 600
wait_ms = 10000        # how long the game waits for a silent player before dropping it, 1000 to 60000
```

A script reads each player's input by number, as it does for a recording or a test:
`input.get(1, "move")`. Your script is never told which player is the local one,
because that would make the players' worlds differ.

**The one rule:** everything your game is must live in components or in `state`, never
in a script's `local`. A rollback puts the world back, and a `local` does not come back
with it. The engine checks this before a network game starts (`E0540`), and
`frost test --restore` tells you where the problem is.

The engine's own example, Duel, has two paddles, a ball and a crate it knocks about, all
of it 2D physics, with a version of the same court for four players.

## Play it

One player hosts, the others join the address the host prints:

```text
frost dev game.toml --host                 hosting at 127.0.0.1:52371: ...
frost dev game.toml --join 127.0.0.1:52371
```

Each window plays at 60 steps a second with its own player's keys. The title bar says
which player the window is and, when the game waits, why: `waiting for 1 more player to
join at ...`, `waiting for player 2: silent 1.4 s`, `player 2 is not answering`, `player
2 left`, `the worlds differ from step 120`.

- **A player who closes the window** says goodbye: that player's input is at rest from
  then on, on every machine, and the others play on.
- **A player who goes silent** for `wait_ms` (10 seconds when left out) is dropped, and
  the others play on.

With no window, `frost run --headless --host` or `--join ADDRESS`, with `--seconds S`,
does the same with each player's recording as its input.

## Host and join from your own menu

Your game can host and join from its own menu: a button's script calls `net.host()` or
`net.join(address)`. Until every player is in, the game is local and each machine runs
its menu alone. Once they are in, every machine loads the game again from its files, at
the scene `[net] start` names, at step 0, so every player starts from the same world
whatever each menu did.

```toml
[net]
start = "court.toml"        # the scene every player starts in
address = "127.0.0.1:7170"  # where net.host() and net.join() go when given no address
```

| Call | Does |
| --- | --- |
| `net.host(address?)` | Asks to host, on `address`, `[net] address` or a free port of this machine |
| `net.join(address?)` | Asks to join the host at `address` or `[net] address`; it asks again every 100 ms for up to 60 s |
| `net.cancel()` | Stops hosting or joining |
| `net.state()` | `{phase, player, players, joined, address, words, refused}`: `phase` is `off` (no `[net]` table, or a program with no network), `alone`, `hosting`, `joining` or `playing`; `words` is the sentence the window's title shows (`waiting for the host`); `refused` is the last request that could not be done, `{code, words}` |
| `net.player()` | The player this machine plays, from 0, once it is known |

A request that cannot be done is not an error in the step: `net.state().refused` tells
you, with its code. Once the game is playing with the others, these five calls stop the
step with `E0544`, because each machine would get a different answer. So ask
`net.player()` in the menu ("you will be player 2"), not in the game. A test or a replay
keeps the requests and does nothing with them, so a menu that hosts still replays.

The Duel example has such a menu, with HOST, JOIN and PLAY ALONE (`lobby.toml`,
`menu.toml` and `menu.luau`).

## Join a game already under way

A game can begin before every seat is taken, and a player can join later:

- Set `[net] begin_with = 1` and a game your menu hosts begins with the host alone, the
  other seats open. An open seat's input is at rest until someone takes it.
- A player who joins late is sent the game as it stands, catches up, and then plays
  like the others. The others play on all the while. One player joins at a time
  (`E0546` for a second).
- **Coming back.** A player who was dropped or left can take the same seat back. An
  exported game remembers its seat in `net-seat.toml`, beside the game's saves, so a
  player who starts the game again after a crash takes the seat back by pressing JOIN.
  Nobody else can take that seat (`E0569`). The seat of a host who left cannot be taken
  back yet.
- A game whose state is over 1,000,000 bytes cannot be joined late (`E0568` says its
  size). For scale, Duel for four is 16,042 bytes at step 300, and the Embers template in 3D
  46,562.

> **Warning.** A seat's token is sent unencrypted, like everything else, so anyone who
> can read your network traffic could take a seat. That is fine for friends on a home
> network; it is not protection for the open internet.

## Ship it

`frost export desktop` builds network play into your game's player when the game has a
`[net]` table, and only then: a game without one ships no network code. The exported
player takes the same flags:

```text
duel --host                       hosting at 127.0.0.1:52371
duel --join 127.0.0.1:52371       joined as player 2
```

Network play adds 100,752 bytes to an exported player (measured on Duel's, on macOS on
Apple silicon). Android exports of a game with `[net]` ask for the Internet permission.

## When something is refused

| Code | When |
| --- | --- |
| `E0540` | The game, put back at the middle of its recording, does not go on the same: a rollback would change the game. Keep what the game is in the world or in `state`, not in a script's `local`; `frost test --restore` finds where |
| `E0541` | A player's game files are not the host's, file for file |
| `E0542` | Two players' worlds differ. The report names the step, the value on each side and the script line that last wrote it |
| `E0543` | The game is in C#: not supported yet |
| `E0544` | A setting out of its range, a second session, or none |
| `E0545` | One player, or no actions |
| `E0546` | A join with no seat: the game has every player, or has begun with every seat taken, or another player is joining right now |
| `E0547` | An input that cannot be taken: a player the game does not have, values of another length, a value that is not a finite number, a step too far ahead, or one older than the window |
| `E0548` | An address that is none, or a socket that cannot be opened |
| `E0549` | A host or player that says nothing for the time allowed |
| `E0565` | A game's menu asked to host or join, and the game has no `[net]` table |
| `E0566` | A late join where the game sent does not fit this game: its components, players, scenes, tuning or physics are different |
| `E0567` | A late join where the game sent cannot be read: from another version of the engine, or cut or changed on the way |
| `E0568` | A late join to a game whose state is over 1,000,000 bytes: the refusal says its size |
| `E0569` | A join to a seat whose player left, without that seat's token: only its player can take it back |

When two worlds differ (`E0542`), you do not have to guess why: each machine reports the
step, the field, its value on both sides and who last wrote it, for example
`Match.travel` of `match`, last written by `duel.luau` line 54.

## How well it holds up

Measured with two players on one machine for 60 seconds, with 30 ms of latency (give or
take 10) and 10% of packets lost each way, on a busy machine: all 3,600 steps were
confirmed with the same checksum on both sides, the same as the recording played alone.
The host rolled back 8 times and the joiner 6.

Putting the world back and playing 8 steps again, the most the default settings ever
ask for in one frame, took a median of 0.15 ms for a small 2D game and 4.0 ms for a 3D
game with 160 physics bodies, out of a 16.7 ms frame (Apple M4 Max, on a busy machine).

Sound is never rolled back. A sound a corrected step asks for that the prediction did
not is played then, a little late; one only a wrong prediction asked for is left to
finish, because a sound cut short clicks.

## Driving it from a program

Everything above is also a [protocol](../concepts/protocol.md) command, for a test or
an AI agent: `net.host`, `net.join`, `net.run`, `net.status`, `net.leave`, and for tests
`net.condition` (make the link late and lossy) and `net.desync` (make the worlds
differ). [The protocol's net commands](../reference/protocol/net.md) have the
details.

## Not yet

- A relay, or a lobby that finds games: players connect directly to the host's address.
- Multiplayer in the web player: a browser has no UDP.
- Games in C# (`E0543`).
- Taking back the seat of a host who left.
- A network view in the editor.
