# Steps and determinism

Your game plays the same way every time, on every machine. That is what makes replays,
tests, time travel and multiplayer possible, and it comes from one rule: a game moves in
fixed steps, sixty a second, each exactly 1/60 s of game time, however fast the machine
draws. A step takes the step's input, runs the engine's stages in one
declared order and ends with a checksum of the whole world. The same files, the same
seed and the same input give the same world at every step, bit for bit, on every machine
that plays it. Today that is measured on the desktop, on Linux and in a browser; phone
exports are tested in the simulator and the emulator ([what works today](../status.md)). Everything else the engine does (replays, tests,
time travel, multiplayer, "who wrote this value") is built on that.

## One step

| Stage | Does |
| --- | --- |
| input | The step's actions: held keys, sticks, the pointer, fingers, or a recording's |
| interface | Menus and controls (`Node`, `Button`, ...) use the input; a use sends its event |
| physics | Box2D in 2D, Jolt in 3D: bodies and movers move, contacts and sensors send events |
| scripts | Each script's `step(dt)`, in the project's order, reading where physics left things |
| animation | Clips and tweens write their frames and fields |
| layout | The interface again, reading no input, so a menu a script showed is laid out this step |
| events | The step's events are kept for the next step's scripts |

Drawing is not a stage: a picture is drawn from the world after a step, as often as the
display asks, and drawing changes nothing. A window that is hidden gets no frames and the
game still steps.

## What keeps it the same everywhere

- **Fixed time.** `dt` is always 1/60. A slow frame is dropped, never queued, so lag
  never builds up.
- **Seeded random.** `math.random` starts from the project's `seed`. The same seed plays
  the same game.
- **The same arithmetic.** Machines' C libraries differ in the last digit of `sin` and
  its kin, so scripts' math functions are the engine's own and Luau is
  built so a multiply and an add are never fused. Use `math.pow` for a power that is not
  a written 2, 3 or 0.5.
- **A declared order.** Systems run in the order above; events come sorted by kind and
  by the things' slots; physics steps the same on one thread or eight.

## The checksum

`frost run --headless --steps 600 game.toml` prints two numbers:

- **world checksum**: the world after the last step, 16 hex digits. Equal checksums mean
  equal worlds, on any machine.
- **run checksum**: every world from the start to there, summed. A project's `replay`
  holds this one; [recorded runs](runs.md) explain how `frost test` uses it.

The world is every thing's components, every script's `state`, the physics' own state
and the random generator. It is not the picture, the sound, the time a step took or a
script's `local` variables.

## What to keep where

What the game is lives in the world or in `state`. A `local` at the top of a script is a
cache: the checksum does not cover it, a reload empties it, and a world put back by a
seek, a test or a rollback does not bring it back. Keep there only what can be built
again from the world and `state` (in `on_scene`, or when first needed), and empty it in
`on_restore()`, which the engine calls after a world is put back. `frost test --restore`
finds a script that breaks this rule.

`state` holds plain data only: numbers, text, true or false, and tables of those.

Sound is output, never game state: a game never reads what it played. Input is read by
action (`input.get(0, "move")`), never by device, so a key, a pad, a finger and a
recording drive the same game.

## Where it pays off

- [Recorded runs and checkpoints](runs.md): a test is a recording and one checksum.
- [Debugging](../guides/debugging.md): seek to any step, and ask who wrote a value.
- [Networking](../guides/networking.md): peers send inputs only, and roll back.
- [The protocol](protocol.md): an agent tries a change and compares runs, step for step.
