# Gameplay in Luau

You write your game's rules as Luau scripts: a small, fast language, sandboxed by the
engine, with no JIT. The project file lists them under `scripts`, and they run in that order every step.
[The scripts reference](../reference/scripts.md) is the whole API; this page is how to
write a game with it.

## The shape of a script

A script's top level runs once, when it is loaded. The engine then calls the functions
it defines:

| Function | Called |
| --- | --- |
| `step(dt)` | Once a fixed step, sixty a second; `dt` is the step's length in seconds |
| `on_scene(name)` | When a scene has started, its things in place and nothing yet drawn; also for the scene the game starts in |
| `on_restore()` | Before the script next runs, after the game was put back to another moment (a seek, a test, a rollback) |

Every engine call but `print` needs the world, so `find`, `each` and `math.random` work
in those functions, not at the top level. The two-scenes sample:

```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`, and what a `local` is for

`state` is the script's own table that outlives a step: counters, timers, the level
number. The engine copies it into the world at the end of every step, so it is in every
snapshot and checksum, a client can read it (`script.state`), and a world put back hands
the script its old `state` again. It holds plain data: numbers, text, true or false,
and tables of those (a function or a metatable in it is `E0307`). Fill it at the top of
the script with `state.score = state.score or 0`, which keeps the value through a
reload.

A `local` at the top of a script is a cache. It is not saved, not in the checksum, and
empty after a reload, and a world put back does not bring it back. Keep there only what
can be built again from the world and `state` (a lookup table, a list of things found
with `each`), build it in `on_scene` or when first needed, and empty it in
`on_restore`. `frost test --restore` finds a script that breaks this.

## Libraries, data and tuning

| In the project file | In a script | Is |
| --- | --- | --- |
| `libraries = ["lib/kit.luau"]` | `local kit = library("lib/kit.luau")` | Shared code: the library's text runs and its return value (a table of functions) is the result. Each call is a copy of its own |
| `data = ["levels/one.toml"]` | `data("levels/one.toml")` | A TOML file as a plain table: a level, a list of waves. Read when the game loads, never while it runs |
| `[tuning] jump = 280` | `tuning.jump` | Numbers to try: `frost run --set tuning.jump=300` and the protocol's `tuning.set` change one without editing a file |

`library` and `data` take paths from the script's own folder too (`./`, `../`). A changed
data file starts the game again, and a replay recorded with other data gives another
checksum.

## Numbers and the same bits everywhere

- Luau's numbers are 64-bit. A component field declared `0.0` stores 32 bits, so `0.1`
  reads back as `0.10000000149011612`; `0` is a whole number; `{ f64 = 0.0 }` keeps every bit.
- All of `math` is the engine's own, the same on every machine and in a browser: turn and
  aim with `math.sin` freely. The one exception is `^` with a power other than a written
  `2`, `3` or `0.5`: use `math.pow`.
- `math.random` is the game's seeded source. There is no clock, no file, no network and
  no `require`.
- A run of a script is stopped after a million loop turns and calls (`E0305`), and all
  scripts may hold 64 MiB (`E0306`): counted, not timed, so they stop at the same point
  everywhere.

## Changing a script while it runs

Under `frost dev` a saved script is swapped in at once, keeping the world and the script's
`state`; the time from save to screen is printed. A script that does not compile is
refused and the old code keeps running; one that fails while running pauses the game until
a save fixes it. In a browser, `frost dev --web` sends each saved script to the open tabs.

## Types in your editor

```sh
frost reference --types game.toml > frost.d.luau
```

writes the script API as a Luau definitions file, with `get`, `set`, `each` and `spawn`
typed from the game's own components and each library typed as the table it returns.
Give it to luau-lsp as a definitions file, and make it again when the components or the
libraries change. Hovering a call shows its reference comment.

## Fast loops over many things

`each("Transform", {"Transform.x"})` returns the things and a buffer of the field's
values, eight bytes a thing; `set(ids, "Transform", "x", buffer)` writes them back in one
call. It is for a script that moves thousands of things a step; see the
[scripts reference](../reference/scripts.md).
