# Debugging

A bug you saw once, you can see again: every run of a game
[replays to the bit](../concepts/steps.md), so you can stand at the same step, with the
same values, as often as you need. This page goes
from the cheapest question to the deepest: what is wrong with the files, who wrote this
value, why is this pixel this colour, and what is this script doing line by line.

The examples use the basics sample, a square that
walks right and picks up a coin worth 5 at step 131.

## What is wrong with the files

```sh
frost check game.toml
```

lists every problem one a line, as `FILE:LINE: CODE: what is wrong and what to do`, and
runs what loading runs (each script's top level and `on_scene`) without stepping. Every
code is stable; the [error index](../reference/errors.md) links each to where it is
explained. Under `frost dev`, a script that does not compile is refused and the old code
keeps running, and a script that fails while running pauses time until a save fixes it.

`print` writes to standard error with its file and line; over the protocol `log.since`
reads the same lines, with the warnings `game.info` lists.

## Who wrote this value

`frost blame` plays the game on its recording with the world keeping who writes, then says
who wrote a field last and what it was before:

```text
$ frost blame game.toml score.Text.text
score.Text.text is "SCORE 5" at step 180
  script main.luau:24 in step: step 131, 1 write, from "SCORE 0"

$ frost blame game.toml hero.Transform.x --step 100 --history 2
hero.Transform.x is 145.0 at step 100
  script main.luau:9 in step: steps 1 to 100, 100 writes, from 40.0
  scene scene.toml:3: step 0, 1 write, from 0.0
```

A writer is a line of a scene, a line of a script (and the two functions that called it,
so a write through a helper names who used the helper), one of the engine's systems
(physics, the interface, animation), a protocol client, or the default. Keeping writers
changes nothing in the game and costs about a tenth of a step. Over the protocol it is
`blame.on`, then `blame.value`, `blame.entity` and `blame.gone` (who despawned a thing).

## Why is this pixel this colour

```text
$ frost blame game.toml --pixel 200,90 --step 10
pixel 200,90 at step 10: 1 drawn there
  coin (e0.0): Rect, layer 0, covers 255 of 255
    made by scene scene.toml:7 in step 0
    Transform.x is 200.0: scene scene.toml:8: step 0, 1 write, from 0.0
```

Every thing drawn at the pixel, the one in front first, who made it and who placed it.
In 3D, the protocol's `frame.at` also says which triangle of which mesh is there and how
it is lit, one line a light with its share and the reason for it ("in shadow", "out of
its reach: 7.811 away, it reaches 4.5"): [3D rendering](3d-rendering.md#explain-a-pixel)
shows one. Neither needs a graphics device or takes a picture.

## Time travel

The protocol puts a game at any step of its recording and back:

| Command | Does |
| --- | --- |
| `game.seek {step}` | Plays the recording to that step, from a kept moment when it can (one every 300 steps) |
| `game.time {rate}` | Under `frost dev`: 1 is normal, 0.5 half speed, 0 paused; `game.step` then steps it |
| `game.snapshot`, `game.restore` | Keep a moment and go back to it |
| `game.try {from, inputs}` | Play a branch of input from a snapshot and read what came of it, in one call |
| `events.since {step}` | Every event of the steps after one, in order |
| `world.series` | A field's values over a stretch of steps |

Seeking twice to one step gives one world, checksum for checksum. To compare a change
before and after on the same input, seek to the step, change the script
(`script.reload`), and seek again.

## Breakpoints

`frost dap` is a Debug Adapter Protocol server for any editor with a DAP client: it starts
the game (with or without a window) or joins one `frost dev` is playing, and stops the
scripts at breakpoints in the game's `.luau` files. A stop shows the stack, each
function's locals and upvalues, and tables to open; the debug console runs Luau where
the script stands; one filter, `errors` (on by default), stops where a script raises an
error. `frost dap --help` prints the launch and attach settings and an nvim-dap
configuration. A game in C# is debugged the same way through your own netcoredbg.

The same is the protocol's `debug.` commands. Stopped at line 23 of the sample as the
coin is picked up:

```json
{"method":"debug.attach","params":{}}
{"method":"debug.breakpoints","params":{"file":"main.luau","lines":[23]}}
{"method":"input.set","params":{"player":0,"action":"move_x","value":1}}
{"method":"game.step","params":{"steps":300}}
  -> {"step":101,"stopped":{"file":"main.luau","line":23,"reason":"breakpoint"}}
{"method":"debug.evaluate","params":{"text":"picked.value"}}   -> {"type":"number","value":"5"}
{"method":"debug.step","params":{"kind":"over"}}                -> {"line":24,"state":"paused"}
{"method":"debug.evaluate","params":{"text":"state.score"}}    -> {"type":"number","value":"5"}
```

A game runs the same, checksum for checksum, with a debugger attached that stops
nothing. An exported game has no debugger in it.

## When a test fails

`frost test` names the first step, the first value and the line that wrote it, against the
game as last accepted (and git HEAD when there are uncommitted changes): see
[recorded runs](../concepts/runs.md). A bug that appears only after a seek, a reload or a
rollback is almost always a `local` that should be in `state`: `frost test --restore`
puts the game back at each checkpoint and names the first step that goes another way.
