# Debugging and recovery

> Stop on a script line with breakpoints, record values with logpoints, read locals and the call stack, and recover from crashes and unsaved work.

Source: https://feather-engine.com/docs/logic/debugging/

The script debugger shows what your [scripts](/docs/logic/scripting/), [world scripts](/docs/logic/world-logic/)
and [Blueprints](/docs/logic/blueprints/) were doing on a given line: the values of their variables
and the chain of calls that led there. A breakpoint pauses the game; a logpoint writes a message and
lets the game run on. The same debugger works in the editor's Play mode, through `debug.get` and
`debug.set`, and in headless [simulations](/docs/agents/simulation/).

Scripts run inside a frame, so the game cannot stop halfway through one. When a breakpoint is hit,
the debugger records the hit and pauses the game at the end of that frame: world logic, object
scripts and game time stop until you continue.

## Set a breakpoint

1. Open the script: an object's script in **Tools → Script editor**, or a world script in
   **Window → Game logic → World scripts**.
2. Click the strip on the left edge of the line numbers. A red dot marks the breakpoint; click it
   again to remove it.
3. Press **Play**. When the line runs, the game pauses.
4. Open **Window → Game logic** and select the **Debugger** tab. While the game is paused the tab
   reads **Debugger (paused)**.
5. Expand the newest hit to see the call stack and the local variables.
6. Click **Continue** to carry on, or **Step frame** to run one frame and pause again.

Breakpoints belong to the editor session, not the project: they are not saved, and they stay set
between Play sessions. Set them before or during Play; when you are not playing, the Debugger tab
shows **Not playing — breakpoints apply at the next Play.**

## The Debugger tab

| Control | What it does |
| --- | --- |
| Status | **Running**, **Paused at a breakpoint**, or the not-playing message |
| **Pause** | Pauses the game now, at the end of the frame |
| **Continue** | Resumes a paused game |
| **Step frame** | Runs one frame, then pauses again |
| **Clear hits** | Empties the hit list |
| **+ Breakpoint** | Adds a breakpoint at line 1 of the open world script, or of the selected object's script; set the line in the row |
| **+ On object:ID** | Adds a breakpoint at line 1 of the selected object's script |

Each row under **BREAKPOINTS** has an on/off box, the script name, the line and a text field. Leave
the text empty for a breakpoint that pauses; type text to make it a logpoint. The bin removes the row.

**HITS** lists the newest 50 hits, newest first, with the script, line and game time. Hits on the
same line in the same frame are merged and show a count such as `×3`; one statement can count more
than once. **HOT RELOADS** lists the scripts reloaded during this Play session.

### Script names

| Script | Name |
| --- | --- |
| World script | Its name, such as `Game` |
| Object script | `object:ID`, such as `object:7` |
| Blueprint | `blueprint:ID`; lines are those of the Blueprint's compiled Rhai program, which `blueprint.get` returns as `source` |

## Logpoints

A logpoint records a message instead of pausing. Write the names of local variables in braces and
the debugger fills in their values when the line runs: `score now {total}` becomes `score now 10`.
Use logpoints on lines that run every frame, where a breakpoint would pause the game each time.

## What a hit records

| Field | Description |
| --- | --- |
| `script`, `line` | Where it stopped; lines start at 1 |
| `time` | Game seconds since Play started |
| `call_stack` | The calls that led there, innermost first, with their arguments, such as `add_coins(5)` |
| `locals` | Variables in scope, as text, including the script's global variables where the code can see them; a local hides a global of the same name |
| `message` | The logpoint text with its values filled in |
| `count` | How many hits were merged into this one |

In this world script, named `Game`, a logpoint on line 9 with the text `score now {total}` reports
`score now 5` and then `score now 10` as two `coin` events arrive.

```rust
let lives = 3;

fn start() {
    on("coin", |data| add_coins(data.value));
}

fn add_coins(value) {
    let total = get("score", 0) + value;
    set("score", total);
}
```

Breakpoints on lines inside a closure body, such as the `|data| …` above, are not hit. Put the code
you want to inspect in a named function, as `add_coins` is here.

## Hot reload during Play

You can change a script while the game is running. Applying a world script, an object script or a
Blueprint during Play reloads it: its functions are replaced and its variables keep their values.
The Console shows `Hot reloaded …: functions replaced, its variables kept.` If the new version
fails to compile, the running version stays. Adding or removing a script or a Blueprint on an
object during Play takes effect at the next Play.

## Limits

| Limit | Value |
| --- | --- |
| Breakpoints | 64 |
| Line | 1 to 100,000 |
| Script name | Up to 96 characters |
| Logpoint text | Up to 400 characters |
| Hits kept | The newest 200 |
| Locals per hit | 40; each value is cut to 200 characters |
| Step | 1 to 600 frames |

## When the engine fails

A failing script line normally stops only that script and reports the line in the **Console** (see
[Scripting → Limits](/docs/logic/scripting/#limits)). If an engine function fails inside a script
call — a world script hook or callback, an object script or a story snippet — the same applies: that
script stops with an `engine panic (script stopped)` error and the game keeps running.

Every such failure, and every crash, writes a crash report to the `crashes` folder in the Feather
data folder, with the app, version, operating system, time, location, message and a backtrace. A
failure that repeats every frame is written once per location. Only the newest 20 reports are kept.

| Command | What it does |
| --- | --- |
| `crash.list` | Lists the reports, newest first: `file`, `app` (`editor`, `player` or `cli`), `unix_time`, `recovered`, `location` and `message` |
| `crash.get` | Reads one report in full, backtrace included; `file` is a name from `crash.list` |
| `crash.clear` | Deletes every report and returns how many were `removed` |

`recovered: true` means a script stopped and the app kept running; `false` means the app crashed.

## Recover unsaved work

While you edit, the editor writes your unsaved work to the `recovery` folder in the Feather data
folder every 30 seconds. Saving, or closing the editor normally, removes it. If the editor crashes
or is closed without saving, the next start shows **Recover unsaved work?**:

- **Recover** opens the autosaved project. It is unsaved, so **File → Save project** writes it back
  to its original file.
- **Discard** deletes the autosave.

The autosave is kept until the recovered work is saved, so a second crash loses nothing.

| Command | What it does |
| --- | --- |
| `recovery.get` | Reports the autosave as `pending` (`source`, the project file it belongs to or `null`; `saved_unix`; `project_name`), or `null` |
| `recovery.restore` | Replaces the open project with the autosave. Refused when the open project has unsaved changes, unless `discard_changes` is `true` |
| `recovery.clear` | Deletes the autosave; `cleared` says whether there was one |

## Automate it

In a live editor, `debug.set` sets the breakpoint list (it replaces the whole list) and controls the
pause; `debug.get` returns the breakpoints, `paused`, `step_frames`, `total_hits`, the newest hits
(20 by default, `hits` up to 200) and the hot reloads:

```bash
feather-cli command debug.set \
  --json '{"breakpoints":[{"script":"Game","line":9,"log":"score now {total}"},{"script":"object:7","line":3}]}'

feather-cli command debug.set --json '{"step":10}'
feather-cli command debug.set --json '{"paused":false}'
feather-cli command debug.get --json '{"hits":5}'
```

`debug.set` also takes `paused: true` to pause now and `clear_hits: true`. Without a running editor,
pass the same `breakpoints` to `game.simulate`. A headless run never pauses: every breakpoint acts
as a logpoint, and the hits appear in the report's `debug` section.

```bash
feather-cli --workspace ./my-game --project project.feather.json command game.simulate \
  --json '{"frames":60,"events":[{"frame":10,"name":"coin","data":{"value":5}}],"breakpoints":[{"script":"Game","line":9,"log":"score now {total}"}]}'
```

After a crash, an agent can read what happened before changing anything:

```bash
feather-cli --workspace ./my-game command crash.list
feather-cli --workspace ./my-game command recovery.get
```
