Coldwave Read the docs

Recipe: stream a world#

Goal: a world bigger than what is kept alive at once, cut into cells that enter near the player and leave behind, with the run still replaying to one checksum.

Steps#

  1. Keep in the base scene what is always there: the player (a follower must be there, E0577 otherwise), the camera, the interface, and anything connected by Parent, Joint or Node.
  2. Write each cell as an ordinary scene in world coordinates, in one folder, named by its grid place: X_Y.toml in 2D, X_Z.toml in 3D, where X is floor(x / size). Thing names are unique across every cell.
  3. Declare the grid with a [stream] table that lists every cell.
  4. React to cell.enter and cell.leave events (value "X,Y"); find things of a cell again after it comes back.

The stream-cells sample is all four steps:

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" }
Luau
-- 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

Fetch the details with frost reference project (its streaming section) and frost reference stream.loaded.

Checks#

Shell
frost check game.toml                 # every cell is read: E0576 names a bad one
frost test --write game.toml          # first time: replay and checkpoints
frost test game.toml                  # ok: 600 steps to run checksum ...
frost test --restore game.toml        # put back at each checkpoint, it streams on the same
frost run --headless --steps 150 --state game.toml   # entered and left, from state

Over the protocol, stream.read at a step says which cells are in, what is saved of the others and whether physics may step: seek to a step where the player is on a boundary and read it. frost bench shows the stream span and the stream.entered count: the cost of the steps where cells enter. A cell enters whole, so a slow entry is a cell with too much in it: split it.

Pitfalls#

  • A cell entering is whole or not at all; a cell with more things than budget never enters. Split it or raise the budget.
  • Physics waits for a cell it needs: a teleport shows queued cells for several steps.
  • Cell text and assets are all read at load, on the web too: streaming bounds live things and bodies, not asset memory.
  • Ground must cover its cell; a body must stay inside the cells kept in.