# Streaming worlds

Build a world bigger than you could keep loaded at once. You cut it into cells: scenes
on a grid that enter near the things your game follows and leave behind them. Which cells are in at a step is decided by
one rule, before physics, from what the world holds, so a recorded run streams the same
way on every machine and every time it is played again. The whole of it is
[streaming cells](../reference/project.md#streaming-cells) in the reference.

## A road of four cells

The stream-cells sample is a hero walking right
along a road of four cells and back:

```toml
# example: stream-cells/game.toml
# A road of four cells that enter ahead of the hero and leave behind it, by one rule on
# every machine. The hero walks right across them, then back.
#
#   frost dev game.toml
#   frost test game.toml
scene = "base.toml"
scripts = ["main.luau"]
seed = 1
replay = { steps = 600, checksum = "c6b840469fb21a48", engine = "0.0.1 r280" }
# The run's checksum on the way there, written by `frost test --write`: a test that
# fails says between which two of these steps the game first went another way.
checkpoints = [
    [256, "d58d9b4530b1d282"], [512, "023608768c5b3d94"],
]

[window]
size = [320, 180]
background = 0x15171c

# Cells are scenes in `folder`, named by their grid place (X_Y in 2D), each `size`
# units on a side. Those within `load` cells of a follower enter; those beyond `drop`
# leave. At most `budget` things enter in a step.
[stream]
folder = "cells"
cells = ["0_0.toml", "1_0.toml", "2_0.toml", "3_0.toml"]
size = 160
load = 1
drop = 1
followers = ["hero"]
budget = 64

[input]
actions = ["move_x"]
recording = [{ steps = 290, move_x = 1 }, { steps = 310, move_x = -1 }]

[input.keys]
move_x = { negative = "A", positive = "D" }
```

A cell is an ordinary scene, written in world coordinates, named by its grid place
(`X_Y.toml` in 2D, `X_Z.toml` in 3D); `floor(position / size)` is the cell a point is in,
negative places too. Only the cells listed exist; a gap is empty world.

```toml
# example: stream-cells/cells/1_0.toml
# Cell 1 of the road: world coordinates, names unique across every cell.
[road1]
Transform = { x = 240, y = 40 }
Rect = { w = 160, h = 8, color = 0x466450 }

[post1]
Transform = { x = 280, y = 64 }
Rect = { w = 6, h = 40, color = 0xc9bfae }
```

What is always there (the hero, the camera, the interface) is the base scene. A follower
must be in it: a follower that is missing or outside the grid is `E0577`.

```toml
# example: stream-cells/base.toml
# The base scene: what is always there. A follower stays in it, never in a cell.
[hero]
Transform = { x = 40, y = 60 }
Rect = { w = 10, h = 10, color = 0xf2364b }

[camera]
Transform = { x = 160, y = 90 }
Camera = {}

[cells]
Transform = { x = 160, y = 168 }
Text = { text = "", size = 1, color = 0xeceae6 }
Screen = {}
```

## The table

| Key | Is |
| --- | --- |
| `folder` | Where the cells are, from the project file |
| `cells` | The cells, listed: a file missing is an error |
| `size` | A cell's side, in world units |
| `load` | How many cells round a follower are brought in (1 when left out) |
| `drop` | How far a cell may be before it leaves, at least `load` (2 when left out): the gap stops a cell entering and leaving on a boundary |
| `followers` | The things cells gather round; left out, the lowest placed `Camera` |
| `budget` | How many things may enter in one step (256 when left out): a cell enters whole, or waits for the next step |

## In a script

`cell.enter` and `cell.leave` are events like any other, with the cell's `"X,Y"` as their
value, seen by scripts in the same step. Entering means every thing of the cell is there;
leaving means its things were saved and removed.

```lua
-- example: stream-cells/main.luau
-- Walk the hero along the road; the camera follows. A cell's entry and leaving are
-- events like any other, with the cell's "X,Y" as their value.
state.entered = state.entered or 0
state.left = state.left or 0

function step(dt)
    local hero = find("hero")
    local x = get(hero, "Transform", "x") + input.get(0, "move_x") * 120 * dt
    set(hero, "Transform", "x", x)
    set(find("camera"), "Transform", "x", math.max(160, x))

    for _, cell in ipairs(events("cell.enter")) do
        state.entered += 1
        state.last = "LAST IN " .. cell.value
    end
    for _, cell in ipairs(events("cell.leave")) do
        state.left += 1
        state.last = "LAST OUT " .. cell.value
    end
    set(find("cells"), "Text", "text", "IN " .. state.entered .. " OUT " .. state.left .. " " .. (state.last or ""))
end
```

| Call | Does |
| --- | --- |
| `stream.loaded(x, y)` | Whether the cell at this world position is wholly in. A position, not a cell's number: the cell two along with `size = 160` is `stream.loaded(2 * 160, 0)` |
| `stream.hold(x, y, on?)` | Keeps the cell at this world position in (or, with `false`, lets it go) whatever the followers do |

An entity number kept from a cell is dead once the cell leaves: `find` it again after it
comes back. Physics waits while a cell it needs is still on its way in; scripts still
run, and can hold back travel on `stream.loaded`.

## What a cell remembers

A thing a cell owns keeps its changes when the cell leaves: a moved crate is where it was
left when the cell comes back, a body keeps its velocity, a despawned thing stays gone. A
thing that crosses out of its cell becomes the world's for good and never doubles on the
way back. Things scripts spawn are always the world's. All of it is world state, so a
replay, a snapshot and a seek keep it; `scene.load` starts it afresh.

## Checks and costs

- `frost check` reads every cell; a wrong grid or cell is `E0576`, a name two cells share
  at entry `E0578`, holding a cell that is not listed `E0579`.
- `frost test` holds the run's checksum, streaming included, and `frost test --restore`
  shows that a game put back at a checkpoint streams on the same.
- The protocol's `stream.read` says which cells are in, what is saved of those that are
  out, and whether physics may step. The `stream` span in `frost bench` times the boundary;
  `stream.cells` and `stream.entered` count its work.

## Limits

Cell text and assets are read when the game loads, on the web too: this version bounds the
things and bodies alive, not the memory of assets. Connected things (a `Parent`, a
`Joint`, an interface `Node`) belong in the base scene, and a cell cannot include
scenes. Ground must cover its cell, and bodies must stay within the cells kept in.
