# animation

Part of the [scripting reference](index.md), written by the engine so it matches your `frost`. Start with [your first game](../start/first-game.md) if you are new.

Use when animating sprites or models, playing clips and poses, tweening fields, emitting particles or handling animation events.

| In a script | Does |
| --- | --- |
| `bone(entity, "joint")` | World x, y, z, qx, qy, qz, qw of a unique model node, including this step's writes, without drawing; E0574 for a missing/ambiguous joint or invalid rig. See Bone |
| `emit(entity, n)` | Emits n specks with the entity's `Emitter` look; returns one `Burst`. `E0560`: over 4096 alive at once; `E0561`: no `Emitter`; `E0562`: n is not a whole number from 1 |
| `tween(entity, "Transform", "x", to, seconds, { ease = "out_quad" })` | Takes one number field of an entity from what it is to `to` over `seconds`, a little each step, and returns the tween (an entity: `despawn` it to stop). Options: `ease`, `from`, `loop` (`"restart"`, `"pingpong"`), `done` (an event sent when it ends). See Animation |

## Animation

Two components move things without a line of script per step. Both are stepped after
the scripts, so what a script decides in a step is on the frame drawn after it.

| Component | Field | Type | Default | Meaning |
| --- | --- | --- | --- | --- |
| `Clip` | `name` | text | `""` | The clip to play, by its name under `[clips]` in the project file. Empty plays nothing |
| `Clip` | `time` | number | 0 | Seconds into the clip. Set it to 0 to start again |
| `Clip` | `speed` | number | 1 | How fast time passes: 0 holds the frame, 2 plays twice as fast. Less than 0 is 0 |
| `Clip` | `frame` | whole number | 0 | The frame showing, counted from 0. Written by the engine: read it, do not write it |
| `Tween` | `slot` | whole number | 0 | With `generation`, the entity whose field changes. `tween()` fills both |
| `Tween` | `generation` | whole number | 0 | See `slot` |
| `Tween` | `component` | text | `""` | The component that holds the field |
| `Tween` | `field` | text | `""` | The field: a number or a whole number (which gets the nearest) |
| `Tween` | `from` | number | 0 | The value it starts from |
| `Tween` | `to` | number | 0 | The value it ends on, exactly |
| `Tween` | `time` | number | 0 | Seconds since it began |
| `Tween` | `seconds` | number | 1 | How long it takes. 0 or less is at once |
| `Tween` | `ease` | text | `"linear"` | How it gets there: see Easings |
| `Tween` | `loop` | text | `""` | `""` once, `"restart"` from the start again for ever, `"pingpong"` there and back for ever |
| `Tween` | `done` | text | `""` | The event sent, about the changed entity, when it ends. Empty sends none |

- **Clips.** A clip is a run of frames on one picture, equal cells from left to right,
  declared once in the project file:
  `run = { image = "art/hero.png", frame = [16, 16], at = [0, 16], count = 6, fps = 12 }`.
  An entity with a `Sprite` and `Clip = { name = "run" }` shows it: the engine writes the
  sprite's `src_x`, `src_y`, `src_w`, `src_h` (and its `image`, when the clip names one)
  each step. A clip loops unless it says `loop = false`; then it stays on its last
  frame. To change clip, set `name` and set `time` to 0; setting only `name` carries on
  from the same moment, which is how a run and a run-with-sword stay in step. A name
  that is no clip stops the step with `E1001`.

- **Clip events.** A clip may send events as frames come up:
  `events = [{ frame = 2, name = "footstep" }]` sends `footstep` about the entity, with
  the frame as its value, each time frame 2 comes up. `end = "landed"` on a clip that
  does not loop sends `landed` once, when its last frame has had its time. Scripts read
  them with `events("footstep")` in the next step, since animation runs after scripts.

- **Tweens.** A tween changes one number of one entity from a value to another over a
  time. Start one with `tween(entity, "Transform", "x", 200, 0.5, { ease = "out_quad" })`
  (see the script API). It is an entity of its own with a `Tween`, so an entity can have
  several at once, they are in the checksum, and `despawn` on what `tween` returned
  stops one. When it ends it writes `to`, sends its `done` event if it names one, and
  is gone. A tween whose entity is gone goes too. Two tweens on one field both write,
  and the later-made one wins.

- **Easings.** `linear`, `in_quad`, `out_quad`, `in_out_quad`, `in_cubic`, `out_cubic`,
  `in_out_cubic`, `out_back` (past the end, then back) and `out_bounce`. `in` starts
  slow, `out` ends slow. Another name stops the step with `E1002`.

### Scripted joints

A 3D `Bone` overrides a model node after clip blending, or its rest without `Pose`.
Ordinary `set` writes it in Luau and C#; removing it restores clip/rest. Overrides are
world components: checksum, snapshots and replay keep them. `bone(thing, "hand")`
reads the world pose; `world.bone` reads it over the protocol. `Parent.joint` attaches
a tool using its Transform as the socket offset. Unevenly scaled turns have Parent's
usual shear limitation. the engine's `pose` example has a script-only arm, held box and tip light.

Things with overrides or joint attachments advance their model clips before scripts;
their clip events are therefore readable then. They draw the exact pose before shadows,
without crowd rounding or rest-box culling. First adding one during a step holds its
clip clock until the next step. Other clips retain their normal stage. Reads see all
writes made so far; later scripts, tweens, Spin or Follow can still move things.

```toml
# Optional: animation clips, which an entity plays with `Clip = { name = "run" }`. A clip
# is `count` frames of `frame` image pixels each, left to right from `at` on `image`
# (left out: the sprite's own picture), shown at `fps` a second (left out: 10). It loops
# unless `loop = false`. `events` are sent as a frame comes up, and `end` when a clip
# that does not loop is over. See Animation.
[clips]
run = { image = "art/hero.png", frame = [16, 16], at = [0, 16], count = 6, fps = 12, events = [{ frame = 2, name = "footstep" }] }
land = { frame = [16, 16], at = [0, 32], count = 3, loop = false, end = "landed" }
```

```toml
# Optional: what a model's clip sends as a 3D thing's `Pose` plays it, by the clip's name:
# each event's name and the moment of the clip it is sent at (`at`, in seconds), about the
# thing, with the moment as its value; read by scripts in the next step with
# `events("step")`, in Luau and C# alike (a script plays a sound on it). `seconds` is the
# clip's length in the model (`assets.picture` lists it), and `loop` whether it goes round
# (left out: true; one that does not sends nothing once it holds its end), in every model
# the game loads with a clip of that name, whatever the cook found: a model's clip goes
# round when each joint ends within a millimetre, two degrees and a thousandth of its size
# of where it began, and is bent so it ends there exactly (no tick as it starts again). A game that loads a model whose clip of that name lasts another length
# is refused (E0486). `frost check` says (W0461) of a clip a `Pose` plays that holds its end
# though it nearly goes round (no joint off by more than ten times that), which joint and
# how far: a cycle exported a little off, which a line here has go round.
[poses]
walk = { seconds = 1.0, events = [{ at = 0.25, name = "step" }, { at = 0.75, name = "step" }] }
```

| Component | Field | Type | Default | Meaning |
| --- | --- | --- | --- | --- |
| `Parent` | `joint` | text | `""` | Local to this model node of `of`; empty keeps ordinary parenting. E0574 for a missing or ambiguous joint. Uses the current pose, including Bone overrides |
| `Bone` | `of` | text | `""` | Skinned thing's name. One override entity per joint; give it Parent.of too to despawn it with that thing |
| `Bone` | `joint` | text | `""` | Unique model node name, including unweighted sockets; duplicates or matrix-only nodes are refused (E0574) |
| `Bone` | `qx` | number | 0 | Absolute local quaternion x; finite components, nonzero quaternion, normalised when sampled |
| `Bone` | `qy` | number | 0 | Quaternion y |
| `Bone` | `qz` | number | 0 | Quaternion z |
| `Bone` | `qw` | number | 1 | Quaternion w |
| `Bone` | `translation` | true or false | false | Override local translation too; otherwise keep the clip/rest translation |
| `Bone` | `x` | number | 0 | Absolute local translation x, metres; finite |
| `Bone` | `y` | number | 0 | Translation y |
| `Bone` | `z` | number | 0 | Translation z |
| `Spin` | `rate` | number | 0 | Degrees a second, anticlockwise seen from the end of its axis. In 2D it turns `Rotation.angle`, kept from 0 to 360 |
| `Spin` | `axis_x` | number | 0 | In 3D, the axis it turns about, in its own frame: x |
| `Spin` | `axis_y` | number | 0 | Its y |
| `Spin` | `axis_z` | number | 1 | Its z: about its own z, the way a 2D thing turns |
| `Pose` | `clip` | text | `""` | The clip, by its name in the model (`assets.picture` lists them). Set it to play another: `set(thing, "Pose", "clip", "run")`, read it to know which plays. Empty, or a name the model has not, is the rest |
| `Pose` | `time` | number | 0 | Seconds into the clip. Set it to 0 to start again; left, a new clip goes on from the same moment. A clip that ends where it began goes round (each joint within a millimetre, two degrees and a thousandth of its size; `[poses]` `loop` says otherwise), any other holds its end, in the picture and in the joints `bone` reads and a `Parent.joint` hangs from alike |
| `Pose` | `speed` | number | 1 | How fast its time passes: 0 holds it. Less than 0 is 0 |
| `Pose` | `fade` | number | 0 | Seconds a change of clip mixes the pose it was in into the new one; 0 changes at once |
| `Pose` | `from` | text | `""` | Written by the engine: the clip a fade is from, empty when none runs |
| `Pose` | `from_time` | number | 0 | Written by the engine: that clip's time, going on under the fade |
| `Pose` | `blend` | number | 1 | Written by the engine: how far the fade has gone, 1 done |
| `Pose` | `was` | text | `""` | Written by the engine: the clip it played last step, which a change is seen against |
| `Pose` | `then` | number | 0 | Written by the engine: the time it ended the last step at, which a fade starts from |
| `Gait` | `idle` | text | `"idle"` | The clip below `walk_at` |
| `Gait` | `walk` | text | `"walk"` | The clip from `walk_at` |
| `Gait` | `run` | text | `"run"` | The clip from `run_at` |
| `Gait` | `walk_at` | number | 0.3 | Metres a second from which it walks |
| `Gait` | `run_at` | number | 3 | Metres a second from which it runs |
| `Gait` | `walk_speed` | number | 1.4 | Metres a second the walk clip was made to go at |
| `Gait` | `run_speed` | number | 4 | Metres a second the run clip was made to go at |
| `Gait` | `fade` | number | 0.2 | Seconds each change of clip fades over |
| `Gait` | `x` | number | 0 | Written by the engine: where it was last step, across |
| `Gait` | `z` | number | 0 | Written by the engine: and in depth |
| `Gait` | `seen` | true or false | false | Written by the engine: whether it has been seen a step yet |
| `Gait` | `speed` | number | 0 | Written by the engine: metres a second it went over the ground this step |
| `Emitter` | `fired` | whole number | 0 | Engine-written total specks emitted, used to number them. Game state, in the checksum |
| `Emitter` | `seed` | whole number | 0 | Picks speck directions and speeds: equal seeds emit equal patterns |
| `Emitter` | `life` | number | 1 | Seconds a speck lives |
| `Emitter` | `speed` | number | 3 | Initial speck speed, units a second: between half and all of this value |
| `Emitter` | `spread` | number | 30 | Maximum degrees from the thing's up at emission: either side in 2D, all around in 3D. 180: every way |
| `Emitter` | `gravity` | number | 0 | Units a second squared a speck falls by along y: below 0 falls |
| `Emitter` | `size` | number | 0.2 | Initial speck width and height, in units |
| `Emitter` | `size_end` | number | 0 | Its size at the end of its life, grown or shrunk evenly to it |
| `Emitter` | `add` | number | 1 | 2D light addition versus covering, 0 to 1; 3D specks always add |
| `Emitter` | `color` | whole number | `0xffd080` | Its colour as it leaves |
| `Emitter` | `color_end` | whole number | `0x000000` | Final colour: black fades an added speck out |
| `Emitter` | `layer` | whole number | 0 | 2D speck draw layer |
| `Burst` | `first` | whole number | 0 | First speck's number: emitter's `fired` at emission |
| `Burst` | `count` | whole number | 0 | Speck count |
| `Burst` | `seed` | whole number | 0 | The emitter's `seed` |
| `Burst` | `time` | number | 0 | Seconds since emission, engine-written |
| `Burst` | `life` | number | 1 | Emitter's `life` at emission |
| `Burst` | `speed` | number | 3 | Emitter's `speed` at emission |
| `Burst` | `spread` | number | 30 | Emitter's `spread` at emission |
| `Burst` | `gravity` | number | 0 | Emitter's `gravity` at emission |
| `Burst` | `size` | number | 0.2 | Emitter's `size` at emission |
| `Burst` | `size_end` | number | 0 | Emitter's `size_end` at emission |
| `Burst` | `add` | number | 1 | Emitter's `add` at emission |
| `Burst` | `color` | whole number | `0xffd080` | Emitter's `color` at emission |
| `Burst` | `color_end` | whole number | `0x000000` | Emitter's `color_end` at emission |
| `Burst` | `layer` | whole number | 0 | Emitter's `layer` at emission |
