---
name: ferret-motion
description: Use for a scene's logic and mechanics: Ferret's game language (.fgl streams, kinds, mail, TS externs, scene forms, live change, game.* commands) and the motion language inside it (waits, par, race, states, defmotion, seeded, snapshots).
---

# The game language, and the motion language in it

## When to use
**A scene's logic is written here**, not in the scene's TypeScript: rules, state, who reacts to what, entities with their own state. Motion programs say what happens over time. TypeScript is for externs, what the language cannot say.

## Steps
1. The game installs it: `installGame(world, { sensors, effects, motion })` from `@ferret/dataflow`; its files are `logic/*.fgl`, named in the code so they are packed.
   TS/JS: `(extern wave-height :from "logic/sea.ts" :kind pure ...)`; the build connects the file by itself, it exports `waveHeight`; callable in streams and motion programs.
2. Forms: `def`, `pre`/`fby`, `defflow`, `defn`; clocks `when merge every every-s event`; `defkind :fields` with `set-field send collect query nearest spawn despawn`; `extern`; `defmotion`, `(motion name)`; scene `game level entity link`.
3. Sensors `time dt step pressed axis position exposed`, effectors `emit command`; others the game gives.
4. Commands: `game.check` before loading, `game.load`, `game.run`; change the running game with `edit.game.set` (no build, state kept); look with `game.eval`, `game.trace`, `game.entities`, `game.graph`.
5. A missing mechanism: a module in `logic/<mechanism>/` with its parameters, externs and own test (`reference/game-language.md`).

## Motion programs
`seq par race for while guard repeat loop on states`, `wait wait-until wait-event`, units `30rpm 90deg 0.4s`. Alone: `new MotionRuntime({ seed, budget })`, `rt.load`, `rt.run`, `rt.tick(dt)` each fixed step; `p.state()`, `p.snapshot()`.

## Check afterwards
`game.check` is `ok`; the mechanism's test passes (headless, or a Ferretic test with `WAIT EVENT` and `game.eval`); `game:error` never came.

## What usually goes wrong
- A statement with only constants runs every step: put a value on a clock.
- A value that depends on itself without `pre` is refused; streams of two clocks need `when` or `merge`.
- A field written this step reads as before until the next; mail arrives the next step.
- A `.fgl` under `src/`, or one the code does not name, is not in the package.
- `:from` is a path from the project's root; a file not found there must come from `installGame(world, { externs })`.
- A motion loop without `wait` hits the budget; a `defn` cannot wait: use `defmotion`.
- A long list: ask with `{offset, count}`.

## Files

- `reference/game-language.md` — The language a scene's logic is written in: streams stepped with the world, kinds of entities with fields and mail, motion programs, TypeScript externs, scenes as forms, the game.* commands and changing a running game without a build.
- `reference/motion.md` — physics/motion.md
- `reference/api-dataflow.md` — every export of `@ferret/dataflow` with signature and doc comment
- `reference/api-motion.md` — every export of `@ferret/motion` with signature and doc comment
- `reference/commands.md` — 17 commands of these packages with their arguments
