# C API: Animation layers

Section 3.8c of the header. The guide, with examples, the rules for root motion, threads and crowds, and the measured
cost, is [Animation layers and states](../../guides/layers.md). New structs: `sona_layer_options` (48 bytes, 44 with 4-byte
pointers), `sona_layer_info` (40), `sona_layer_event` (20), `sona_blend_sample` (24 / 16), `sona_blend_space_desc`
(48 / 36), `sona_state_desc` (32 / 20), `sona_condition` (16 / 12), `sona_transition_desc` (96 / 72),
`sona_state_machine_desc` (40 / 28), and the opaque `sona_blend_space` and `sona_state_machine`. Structs with a `size`
field are filled from their `_defaults` call (or `size` set to `sizeof`); a smaller `size` is `SONA_E_ARGUMENT`.
Every layer call returns `SONA_E_ARGUMENT` for a NULL avatar, a layer past `SONA_LAYER_MAX - 1` or an item preview,
and `SONA_E_DESTROYED` for a removed one.

## sona_layer_defaults

```c
void sona_layer_defaults(sona_layer_options *o);
```

| Parameter | Type | Meaning | Default |
|---|---|---|---|
| `o` | `sona_layer_options *` | The [`sona_layer_options`](../fields.md#sona_layer_options) to fill with the defaults | required |

**Returns** Nothing.

**Example** (also in [Animation layers, masks, blend spaces and state machines](../../guides/layers.md)):

```c
float upper[SONA_JOINT_COUNT];
sona_layer_options lo;

sona_avatar_play(a, "00400000-0064-0003-c1c8-f109a19cb2e0", NULL);   /* the stock walk */
sona_avatar_root_motion(a, SONA_ROOT_MOTION_EXTRACT);                /* walk in place, the game moves the avatar */

sona_mask_preset(SONA_MASK_UPPER_BODY, upper);
sona_layer_defaults(&lo);
lo.mask = upper;
sona_avatar_layer_play(a, 1, "GenericWave", &lo);                  /* layer 1: the wave, on Spine and above */
```

`size` set, `SONA_LAYER_OVERRIDE`, no flags, `loop` 1, `weight` 1, `fade_seconds` 0.2, `speed` 1, `from` 0,
`additive_ref` 0, `mask` NULL. NULL is ignored.

## sona_avatar_layer_play

```c
sona_result sona_avatar_layer_play(sona_avatar *a, uint32_t layer, const char *clip, const sona_layer_options *opts);
```

| Parameter | Type | Meaning | Default |
|---|---|---|---|
| `a` | `sona_avatar *` | The avatar | required |
| `layer` | `uint32_t` | A layer index, from 1 (0 is the base clip) | - |
| `clip` | `const char *` | A clip key or id | required |
| `opts` | `const sona_layer_options *` | The options, a [`sona_layer_options`](../fields.md#sona_layer_options) | `NULL`: the defaults |

**Returns** `SONA_OK`. **Errors** `SONA_E_ARGUMENT`, `SONA_E_DESTROYED`, `SONA_E_MEMORY` ([what each means](../../troubleshooting.md#error-codes)).

**Example** (also in [Animation layers, masks, blend spaces and state machines](../../guides/layers.md)):

```c
float upper[SONA_JOINT_COUNT];
sona_layer_options lo;

sona_avatar_play(a, "00400000-0064-0003-c1c8-f109a19cb2e0", NULL);   /* the stock walk */
sona_avatar_root_motion(a, SONA_ROOT_MOTION_EXTRACT);                /* walk in place, the game moves the avatar */

sona_mask_preset(SONA_MASK_UPPER_BODY, upper);
sona_layer_defaults(&lo);
lo.mask = upper;
sona_avatar_layer_play(a, 1, "GenericWave", &lo);                  /* layer 1: the wave, on Spine and above */
```

Plays a clip key or raw clip id on the layer, crossfading from what it showed over `fade_seconds`. NULL `opts`: the
defaults. `SONA_E_ARGUMENT` also for `"idle"`, `"still"`, `"prop"`, an unknown key and an option out of range
(`weight` outside 0..1, a negative or NaN time or speed, a mask weight outside 0..1). Changing between OVERRIDE and
ADDITIVE cuts instead of crossfading.

## sona_avatar_layer_blend

```c
sona_result sona_avatar_layer_blend(sona_avatar *a, uint32_t layer, sona_blend_space *b, const sona_layer_options *opts);
```

| Parameter | Type | Meaning | Default |
|---|---|---|---|
| `a` | `sona_avatar *` | The avatar | required |
| `layer` | `uint32_t` | A layer index, from 1 (0 is the base clip) | - |
| `b` | `sona_blend_space *` | The blend space | required |
| `opts` | `const sona_layer_options *` | The options, a [`sona_layer_options`](../fields.md#sona_layer_options) | required |

**Returns** `SONA_OK`. **Errors** `SONA_E_ARGUMENT`, `SONA_E_DESTROYED`, `SONA_E_MEMORY` ([what each means](../../troubleshooting.md#error-codes)).

**Example** (also in [Animation layers, masks, blend spaces and state machines](../../guides/layers.md)):

```c
sona_blend_sample s[3] = {
  { "Stand0", 0.0f, 0, 1 },                              /* clip, x, y, playback rate */
  { "00400000-0064-0003-c1c8-f109a19cb2e0", 1.2f, 0, 1 },  /* walk */
  { "00400000-0064-0003-c1c8-f109a19cb2e0", 2.4f, 0, 1.9f }, /* the walk played fast as a run */
};
sona_blend_space_desc d;
sona_blend_space *move;
sona_blend_space_defaults(&d);
d.param_x = "speed";
d.samples = s;
d.sample_count = 3;
sona_blend_space_create(ctx, &d, &move);
sona_avatar_layer_blend(a, 0, move, NULL);
sona_blend_space_destroy(move);                          /* the avatar keeps it while it plays */
```

Plays a blend space of the same ctx on the layer. `from` is the fraction 0..1 of its shared cycle (seconds with
`SONA_BLEND_UNSYNCED`).

## sona_avatar_layer_machine

```c
sona_result sona_avatar_layer_machine(sona_avatar *a, uint32_t layer, sona_state_machine *m, const sona_layer_options *opts);
```

| Parameter | Type | Meaning | Default |
|---|---|---|---|
| `a` | `sona_avatar *` | The avatar | required |
| `layer` | `uint32_t` | A layer index, from 1 (0 is the base clip) | - |
| `m` | `sona_state_machine *` | The state machine | required |
| `opts` | `const sona_layer_options *` | The options, a [`sona_layer_options`](../fields.md#sona_layer_options) | required |

**Returns** `SONA_OK`. **Errors** `SONA_E_ARGUMENT`, `SONA_E_DESTROYED`, `SONA_E_MEMORY` ([what each means](../../troubleshooting.md#error-codes)).

Runs a state machine of the same ctx on the layer from its initial state (a `STATE` event with `from_state` -1).
`speed` scales every state's speed; each state says whether it loops.

## sona_avatar_layer_goto

```c
sona_result sona_avatar_layer_goto(sona_avatar *a, uint32_t layer, uint32_t state, float fade_seconds);
```

| Parameter | Type | Meaning | Default |
|---|---|---|---|
| `a` | `sona_avatar *` | The avatar | required |
| `layer` | `uint32_t` | A layer index, from 1 (0 is the base clip) | - |
| `state` | `uint32_t` | A state's index | - |
| `fade_seconds` | `float` | Seconds to blend over | - |

**Returns** `SONA_OK`. **Errors** `SONA_E_ARGUMENT`, `SONA_E_DESTROYED` ([what each means](../../troubleshooting.md#error-codes)).

**Example**:

```c
sona_result result = sona_avatar_layer_goto(avatar, 0, 0, 0.5f);
if (result != SONA_OK) printf("sona_avatar_layer_goto: %s\n", sona_result_name(result));
```

Sends a machine layer to `state` now, crossfading over `fade_seconds`. `SONA_E_ARGUMENT` when the layer runs no
machine or the state is past its last.

## sona_avatar_layer_weight

```c
sona_result sona_avatar_layer_weight(sona_avatar *a, uint32_t layer, float weight, float over_seconds);
```

| Parameter | Type | Meaning | Default |
|---|---|---|---|
| `a` | `sona_avatar *` | The avatar | required |
| `layer` | `uint32_t` | A layer index, from 1 (0 is the base clip) | - |
| `weight` | `float` | A weight from 0 to 1 | - |
| `over_seconds` | `float` | Seconds to ease over; 0: at once | `0`: at once |

**Returns** `SONA_OK`. **Errors** `SONA_E_ARGUMENT`, `SONA_E_DESTROYED`, `SONA_E_MEMORY` ([what each means](../../troubleshooting.md#error-codes)).

**Example**:

```c
sona_result result = sona_avatar_layer_weight(avatar, 0, 0.5f, 0.5f);
if (result != SONA_OK) printf("sona_avatar_layer_weight: %s\n", sona_result_name(result));
```

Moves the layer's weight linearly to `weight` (0..1) over `over_seconds`; 0 or less sets it at once.

## sona_avatar_layer_mask

```c
sona_result sona_avatar_layer_mask(sona_avatar *a, uint32_t layer, const float *mask);
```

| Parameter | Type | Meaning | Default |
|---|---|---|---|
| `a` | `sona_avatar *` | The avatar | required |
| `layer` | `uint32_t` | A layer index, from 1 (0 is the base clip) | - |
| `mask` | `const float *` | A weight per joint, `SONA_JOINT_COUNT` of them; NULL: the whole body | `NULL`: the whole body |

**Returns** `SONA_OK`. **Errors** `SONA_E_ARGUMENT`, `SONA_E_DESTROYED`, `SONA_E_MEMORY` ([what each means](../../troubleshooting.md#error-codes)).

Copies `SONA_JOINT_COUNT` weights (each 0..1); NULL is every joint. Takes effect at the next pose.

## sona_avatar_layer_stop

```c
sona_result sona_avatar_layer_stop(sona_avatar *a, uint32_t layer, float fade_seconds);
```

| Parameter | Type | Meaning | Default |
|---|---|---|---|
| `a` | `sona_avatar *` | The avatar | required |
| `layer` | `uint32_t` | A layer index, from 1 (0 is the base clip) | - |
| `fade_seconds` | `float` | Seconds to blend over | - |

**Returns** `SONA_OK`. **Errors** `SONA_E_ARGUMENT`, `SONA_E_DESTROYED` ([what each means](../../troubleshooting.md#error-codes)).

**Example**:

```c
sona_result result = sona_avatar_layer_stop(avatar, 0, 0.5f);
if (result != SONA_OK) printf("sona_avatar_layer_stop: %s\n", sona_result_name(result));
```

Fades the layer out over `fade_seconds` and then empties it (a `STOPPED` event); 0 empties it now. An empty layer
is `SONA_OK`.

## sona_avatar_layer_info

```c
sona_result sona_avatar_layer_info(const sona_avatar *a, uint32_t layer, sona_layer_info *out);
```

| Parameter | Type | Meaning | Default |
|---|---|---|---|
| `a` | `const sona_avatar *` | The avatar | required |
| `layer` | `uint32_t` | A layer index, from 1 (0 is the base clip) | - |
| `out` | `sona_layer_info *` | Receives the [`sona_layer_info`](../fields.md#sona_layer_info) | required |

**Returns** `SONA_OK`. **Errors** `SONA_E_ARGUMENT`, `SONA_E_DESTROYED` ([what each means](../../troubleshooting.md#error-codes)).

**Example**:

```c
sona_layer_info x_layer_info;
memset(&x_layer_info, 0, sizeof x_layer_info);
x_layer_info.size = sizeof x_layer_info;
sona_result result = sona_avatar_layer_info(avatar, 0, &x_layer_info);
if (result != SONA_OK) printf("sona_avatar_layer_info: %s\n", sona_result_name(result));
```

The layer now: `kind`, `mode`, `state` (machines, else -1), `clip` (the `sona_clip_get` index, -1 for a raw id),
`weight` with fades, `time` and `normalized` through the clip or cycle, `ready` (its clips have loaded) and
`fading`. The caller sets `out->size`.

## sona_avatar_layer_events

```c
uint32_t sona_avatar_layer_events(sona_avatar *a, sona_layer_event *out, uint32_t max);
```

| Parameter | Type | Meaning | Default |
|---|---|---|---|
| `a` | `sona_avatar *` | The avatar | required |
| `out` | `sona_layer_event *` | Receives the [`sona_layer_event`](../fields.md#sona_layer_event) | required |
| `max` | `uint32_t` | How many entries `out` holds | - |

**Returns** `uint32_t`.

**Example**:

```c
sona_layer_event x_layer_event;
memset(&x_layer_event, 0, sizeof x_layer_event);
uint32_t x_got = sona_avatar_layer_events(avatar, &x_layer_event, 1);
printf("sona_avatar_layer_events: %lld\n", (long long)x_got);
```

Moves up to `max` events, oldest first, into `out` and returns how many: `STARTED`, `LOOPED`, `ENDED`, `STATE`,
`STOPPED`, each with its `layer`, `state` and `from_state` (machines; else -1) and `time` (seconds into the clip).
Up to `SONA_LAYER_EVENT_MAX` are held; past that the oldest go.

## sona_mask_preset

```c
sona_result sona_mask_preset(uint32_t preset, float mask[SONA_JOINT_COUNT]);
```

| Parameter | Type | Meaning | Default |
|---|---|---|---|
| `preset` | `uint32_t` | Which preset, one of the header's constants for it | - |
| `mask` | `float [SONA_JOINT_COUNT]` | Receives a weight per joint | required |

**Returns** `SONA_OK`. **Errors** `SONA_E_ARGUMENT` ([what each means](../../troubleshooting.md#error-codes)).

**Example** (also in [Animation layers, masks, blend spaces and state machines](../../guides/layers.md)):

```c
sona_mask_preset(SONA_MASK_UPPER_BODY, m);
sona_mask_branch(m, sona_joint_find("Spine"), 0.5f);    /* half the wave in the spine ... */
sona_mask_branch(m, sona_joint_find("Chest"), 1.0f);    /* ... all of it from the chest up */
```

Fills a mask: `SONA_MASK_NONE`, `_FULL`, `_UPPER_BODY` (Spine and below it), `_LOWER_BODY` (the hips and legs),
`_HEAD` (from the Neck), `_LEFT_ARM`, `_RIGHT_ARM`, `_ARMS` (from the shoulders). Joint 71 is always 0.

## sona_mask_branch

```c
sona_result sona_mask_branch(float mask[SONA_JOINT_COUNT], uint32_t joint, float weight);
```

| Parameter | Type | Meaning | Default |
|---|---|---|---|
| `mask` | `float [SONA_JOINT_COUNT]` | Receives a weight per joint | required |
| `joint` | `uint32_t` | A joint index, from `sona_joint_find` | - |
| `weight` | `float` | A weight from 0 to 1 | - |

**Returns** `SONA_OK`. **Errors** `SONA_E_ARGUMENT` ([what each means](../../troubleshooting.md#error-codes)).

**Example** (also in [Animation layers, masks, blend spaces and state machines](../../guides/layers.md)):

```c
sona_mask_preset(SONA_MASK_UPPER_BODY, m);
sona_mask_branch(m, sona_joint_find("Spine"), 0.5f);    /* half the wave in the spine ... */
sona_mask_branch(m, sona_joint_find("Chest"), 1.0f);    /* ... all of it from the chest up */
```

Sets `joint` and every joint below it in the rig to `weight` (0..1); the rest is kept.

## sona_param_id

```c
int32_t sona_param_id(sona_ctx *ctx, const char *name);
```

| Parameter | Type | Meaning | Default |
|---|---|---|---|
| `ctx` | `sona_ctx *` | The context | required |
| `name` | `const char *` | A parameter's name, 1 to 31 bytes | required |

**Returns** `int32_t`.

**Example** (also in [Animation layers, masks, blend spaces and state machines](../../guides/layers.md)):

```c
int32_t speed = sona_param_id(ctx, "speed");      /* 0 */
int32_t turn = sona_param_id(ctx, "turn");        /* 1 */
/* ... */
float p[2] = { player_speed, player_turn };
sona_avatar_set_params(a, 0, p, 2);               /* each frame, before sona_update */
```

The id of a parameter name (1 to 31 bytes), made on first use and numbered from 0 in that order; -1 for a bad name
or past `SONA_PARAM_MAX` (32) names.

## sona_avatar_set_param

```c
sona_result sona_avatar_set_param(sona_avatar *a, int32_t param, float value);
```

| Parameter | Type | Meaning | Default |
|---|---|---|---|
| `a` | `sona_avatar *` | The avatar | required |
| `param` | `int32_t` | A parameter id, from `sona_param_id` | - |
| `value` | `float` | The value to set | - |

**Returns** `SONA_OK`. **Errors** `SONA_E_ARGUMENT`, `SONA_E_DESTROYED`, `SONA_E_MEMORY` ([what each means](../../troubleshooting.md#error-codes)).

**Example**:

```c
sona_result result = sona_avatar_set_param(avatar, 0, 0.5f);
if (result != SONA_OK) printf("sona_avatar_set_param: %s\n", sona_result_name(result));
```

Sets one parameter on the avatar; read at the next `sona_update`. `SONA_E_ARGUMENT` for an id outside 0..31 or a
value that is not finite.

## sona_avatar_set_params

```c
sona_result sona_avatar_set_params(sona_avatar *a, uint32_t first, const float *values, uint32_t count);
```

| Parameter | Type | Meaning | Default |
|---|---|---|---|
| `a` | `sona_avatar *` | The avatar | required |
| `first` | `uint32_t` | The id of the first parameter to set | - |
| `values` | `const float *` | The values, `count` of them | required |
| `count` | `uint32_t` | How many entries the array holds | - |

**Returns** `SONA_OK`. **Errors** `SONA_E_ARGUMENT`, `SONA_E_DESTROYED`, `SONA_E_MEMORY` ([what each means](../../troubleshooting.md#error-codes)).

**Example** (also in [Animation layers, masks, blend spaces and state machines](../../guides/layers.md)):

```c
int32_t speed = sona_param_id(ctx, "speed");      /* 0 */
int32_t turn = sona_param_id(ctx, "turn");        /* 1 */
/* ... */
float p[2] = { player_speed, player_turn };
sona_avatar_set_params(a, 0, p, 2);               /* each frame, before sona_update */
```

Sets parameters `first .. first + count - 1`: a game's one call a frame. Nothing is set when any value is not
finite or the range passes `SONA_PARAM_MAX`.

## sona_avatar_param

```c
float sona_avatar_param(const sona_avatar *a, int32_t param);
```

| Parameter | Type | Meaning | Default |
|---|---|---|---|
| `a` | `const sona_avatar *` | The avatar | required |
| `param` | `int32_t` | A parameter id, from `sona_param_id` | - |

**Returns** `float`.

**Example**:

```c
float x_got = sona_avatar_param(avatar, 0);
printf("sona_avatar_param: %g\n", (double)x_got);
```

A parameter's value; 0 when unset or unknown. Triggers read 0 once their transition fired.

## sona_blend_space_defaults

```c
void sona_blend_space_defaults(sona_blend_space_desc *d);
```

| Parameter | Type | Meaning | Default |
|---|---|---|---|
| `d` | `sona_blend_space_desc *` | The [`sona_blend_space_desc`](../fields.md#sona_blend_space_desc) to fill with the defaults | required |

**Returns** Nothing.

**Example** (also in [Animation layers, masks, blend spaces and state machines](../../guides/layers.md)):

```c
sona_blend_sample s[3] = {
  { "Stand0", 0.0f, 0, 1 },                              /* clip, x, y, playback rate */
  { "00400000-0064-0003-c1c8-f109a19cb2e0", 1.2f, 0, 1 },  /* walk */
  { "00400000-0064-0003-c1c8-f109a19cb2e0", 2.4f, 0, 1.9f }, /* the walk played fast as a run */
};
sona_blend_space_desc d;
sona_blend_space *move;
sona_blend_space_defaults(&d);
d.param_x = "speed";
d.samples = s;
d.sample_count = 3;
sona_blend_space_create(ctx, &d, &move);
sona_avatar_layer_blend(a, 0, move, NULL);
sona_blend_space_destroy(move);                          /* the avatar keeps it while it plays */
```

`size` set, `dims` 1, no samples, `smooth_seconds` 0.1.

## sona_blend_space_create

```c
sona_result sona_blend_space_create(sona_ctx *ctx, const sona_blend_space_desc *d, sona_blend_space **out);
```

| Parameter | Type | Meaning | Default |
|---|---|---|---|
| `ctx` | `sona_ctx *` | The context | required |
| `d` | `const sona_blend_space_desc *` | The options, a [`sona_blend_space_desc`](../fields.md#sona_blend_space_desc) | required |
| `out` | `sona_blend_space **` | Receives the new blend space | required |

**Returns** `SONA_OK`. **Errors** `SONA_E_ARGUMENT`, `SONA_E_MEMORY` ([what each means](../../troubleshooting.md#error-codes)).

**Example** (also in [Animation layers, masks, blend spaces and state machines](../../guides/layers.md)):

```c
sona_blend_sample s[3] = {
  { "Stand0", 0.0f, 0, 1 },                              /* clip, x, y, playback rate */
  { "00400000-0064-0003-c1c8-f109a19cb2e0", 1.2f, 0, 1 },  /* walk */
  { "00400000-0064-0003-c1c8-f109a19cb2e0", 2.4f, 0, 1.9f }, /* the walk played fast as a run */
};
sona_blend_space_desc d;
sona_blend_space *move;
sona_blend_space_defaults(&d);
d.param_x = "speed";
d.samples = s;
d.sample_count = 3;
sona_blend_space_create(ctx, &d, &move);
sona_avatar_layer_blend(a, 0, move, NULL);
sona_blend_space_destroy(move);                          /* the avatar keeps it while it plays */
```

Checks and copies the desc and starts loading its clips. `SONA_E_ARGUMENT`, with the bad sample named in
`sona_last_error`: `dims` not 1 or 2, 0 or over 16 samples, a missing parameter name, a clip that is not a key or id,
two samples at one point, a negative or NaN value. 2D samples are triangulated here.

## sona_blend_space_destroy

```c
void sona_blend_space_destroy(sona_blend_space *b);
```

| Parameter | Type | Meaning | Default |
|---|---|---|---|
| `b` | `sona_blend_space *` | The blend space | required |

**Returns** Nothing.

**Example** (also in [Animation layers, masks, blend spaces and state machines](../../guides/layers.md)):

```c
sona_blend_sample s[3] = {
  { "Stand0", 0.0f, 0, 1 },                              /* clip, x, y, playback rate */
  { "00400000-0064-0003-c1c8-f109a19cb2e0", 1.2f, 0, 1 },  /* walk */
  { "00400000-0064-0003-c1c8-f109a19cb2e0", 2.4f, 0, 1.9f }, /* the walk played fast as a run */
};
sona_blend_space_desc d;
sona_blend_space *move;
sona_blend_space_defaults(&d);
d.param_x = "speed";
d.samples = s;
d.sample_count = 3;
sona_blend_space_create(ctx, &d, &move);
sona_avatar_layer_blend(a, 0, move, NULL);
sona_blend_space_destroy(move);                          /* the avatar keeps it while it plays */
```

Lets go of the host's handle. Avatars and states that play it keep it until they stop; `sona_close` frees the
rest.

## sona_blend_space_weights

```c
uint32_t sona_blend_space_weights(const sona_blend_space *b, float x, float y, float *weights);
```

| Parameter | Type | Meaning | Default |
|---|---|---|---|
| `b` | `const sona_blend_space *` | The blend space | required |
| `x` | `float` | The blend space's first input | - |
| `y` | `float` | The blend space's second input | - |
| `weights` | `float *` | Receives a weight per sample | required |

**Returns** `uint32_t`.

Writes each sample's weight at `(x, y)` (they sum to 1; `y` is ignored in 1D) and returns how many are above 0
(1 to 3). 0 for a NULL argument or a NaN point.

## sona_state_machine_defaults

```c
void sona_state_machine_defaults(sona_state_machine_desc *d);
```

| Parameter | Type | Meaning | Default |
|---|---|---|---|
| `d` | `sona_state_machine_desc *` | The [`sona_state_machine_desc`](../fields.md#sona_state_machine_desc) to fill with the defaults | required |

**Returns** Nothing.

**Example**:

```c
sona_state_machine_desc x_state_machine_desc;
memset(&x_state_machine_desc, 0, sizeof x_state_machine_desc);
x_state_machine_desc.size = sizeof x_state_machine_desc;
sona_state_machine_defaults(&x_state_machine_desc);
```

`size` set, everything else zero.

## sona_state_machine_create

```c
sona_result sona_state_machine_create(sona_ctx *ctx, const sona_state_machine_desc *d, sona_state_machine **out);
```

| Parameter | Type | Meaning | Default |
|---|---|---|---|
| `ctx` | `sona_ctx *` | The context | required |
| `d` | `const sona_state_machine_desc *` | The options, a [`sona_state_machine_desc`](../fields.md#sona_state_machine_desc) | required |
| `out` | `sona_state_machine **` | Receives the new state machine | required |

**Returns** `SONA_OK`. **Errors** `SONA_E_ARGUMENT`, `SONA_E_MEMORY` ([what each means](../../troubleshooting.md#error-codes)).

Resolves every name to an index once. `SONA_E_ARGUMENT`, naming the state, transition or condition in
`sona_last_error`: 0 or over 32 states, over 64 transitions, a state name that is empty, over 31 bytes or used
twice, a state with both or neither of a clip and a blend space, a transition naming an unknown state, a negative
fade, more than 4 conditions, an unknown op, a bad parameter name, a value that is not finite, an unknown initial
state.

## sona_state_machine_destroy

```c
void sona_state_machine_destroy(sona_state_machine *m);
```

| Parameter | Type | Meaning | Default |
|---|---|---|---|
| `m` | `sona_state_machine *` | The state machine | required |

**Returns** Nothing.

As `sona_blend_space_destroy`.

## sona_state_machine_find

```c
int32_t sona_state_machine_find(const sona_state_machine *m, const char *state);
```

| Parameter | Type | Meaning | Default |
|---|---|---|---|
| `m` | `const sona_state_machine *` | The state machine | required |
| `state` | `const char *` | A state's name | required |

**Returns** `int32_t`.

A state's index by name, -1 if unknown.

## sona_state_machine_state_name

```c
const char *sona_state_machine_state_name(const sona_state_machine *m, uint32_t state);
```

| Parameter | Type | Meaning | Default |
|---|---|---|---|
| `m` | `const sona_state_machine *` | The state machine | required |
| `state` | `uint32_t` | A state's index | - |

**Returns** `const char *`.

A state's name, NULL past the last. Valid while the machine lives.
