# Scenes

A scene is one level, menu or room of your game, written as a TOML file of things: each
table is one thing, its key the thing's name, its entries its components. The project file says which scene a game starts in (`scene`),
which others scripts may switch to (`scenes`) and which scripts run (`scripts`).

The two-scenes sample starts in one level and has
a second for its script to switch to:

```toml
# example: two-scenes/game.toml
# Two levels that share one file of things, and a script that switches between them.
#
#   frost dev game.toml
#   frost test game.toml
scene = "level1.toml"
scenes = ["level2.toml"]
scripts = ["main.luau"]
seed = 1
replay = { steps = 260, checksum = "4c1890c067bda6c0", 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 = [
    [205, "f0012d92de8e4b5a"], [256, "6c0e19595318f5ea"],
]

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

[input]
actions = ["move_x"]
recording = [{ steps = 260, move_x = 1 }]

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

## Switching scenes

`scene.load("level2.toml")` changes the scene at the end of the step. Every thing of the
old scene goes, so every entity a script kept is dead: the engine calls the script's
`on_scene(name)` once the new things are in place and before anything is drawn, and
also for the scene the game starts in. Set a scene up there, finding things again with
`find`; do not carry a flag to the next step, which would draw one frame of the scene
bare.

```lua
-- example: two-scenes/main.luau
-- Walk right to the door to go to the second level. `state` outlives a scene; things
-- and the entity numbers a script kept do not, so on_scene finds them again.
state.level = state.level or 1

function on_scene(name)
    set(find("title"), "Text", "text", "LEVEL " .. state.level .. ": " .. name)
end

function step(dt)
    local hero = find("hero")
    local x = get(hero, "Transform", "x") + input.get(0, "move_x") * 80 * dt
    set(hero, "Transform", "x", x)
    local door = find("door")
    if door and x > get(door, "Transform", "x") - 8 then
        state.level = 2
        scene.load("level2.toml")
    end
end
```

`state` outlives the switch; things do not.

## Sharing what several scenes have

What several scenes share (the camera, the interface, the lights) goes in one file the
others include, never in a copy per level:

```toml
# example: two-scenes/common.toml
# What both levels have: the hero and the line at the top. Each level includes this.
[hero]
Transform = { x = 20, y = 60 }
Rect = { w = 10, h = 10, color = 0xf2364b }

[title]
Transform = { x = 8, y = 168 }
Text = { text = "", size = 1, color = 0xeceae6 }
```

```toml
# example: two-scenes/level2.toml
include = ["common.toml"]

[floor]
Transform = { x = 160, y = 40 }
Rect = { w = 320, h = 8, color = 0x3a4250 }
```

A name written in two of them is an error (`E0106`), and an included scene includes
nothing itself (`E0426`). [`frost reference project`](../reference/project.md) is the
whole project file.

## Big worlds: streamed cells

A world too big to keep whole is cut into cells: a `[stream]` table names a folder of
scenes on a grid, and the cells near the things it follows (a camera, the hero) enter
before physics each step and leave behind them, by the same rule on every machine, so a
recorded run streams the same way when it is played again. See
[streaming cells](../reference/project.md#streaming-cells) in the reference.

## Editing scenes from a program

A scene is text, so any program can write one. Over the [protocol](protocol.md),
`world.spawn`, `world.set` and `world.despawn` change the running game (`world.apply`
makes several as one, all or none) and `game.reset` puts back what the files say. To
change the files themselves, an agent works on a copy of the game (`copy.make`), tests
it, and lands it (`copy.land`), which merges a scene by thing, component and field with
every comment kept. `project.add` makes a new scene or script from the engine's stub, as
`frost add scene NAME` does on the command line.
