# C API: Avatars

An avatar moves through `SONA_AVATAR_QUEUED`, `LOADING`, `READY`, and ends in `FAILED` or `REMOVED`.

`sona_placement` holds `position[3]` (metres, default 0, 0, 0), `turn_deg` (degrees about Y; 0 faces -Z) and `scale` (default 1).

`sona_add_options`:

| Field | Meaning |
|---|---|
| `place` | Where it stands |
| `clip` | A clip key, a raw clip id, `"prop"`, `"idle"` or `"still"`; NULL: `"idle"` |
| `loop` | Default 1 |
| `speed` | Default 1 |
| `start_at` | Seconds; negative means random, to desynchronise a crowd |
| `visible` | Default 1 |
| `priority` | Higher builds sooner; default 0 |

`sona_play_options` holds `loop` (default 1), `speed` (default 1) and `from` (seconds into the clip, default 0).

## sona_add_defaults

```c
void sona_add_defaults(sona_add_options *o);
```

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

**Returns** Nothing.

**Example** (also in [Crowds: level of detail and sprites](../../guides/crowd.md)):

```c
/* 10,000 avatars in rows round a pitch, facing in */
sona_lod_set(ctx, NULL);
for (i = 0; i < 10000; i++) {
  sona_add_options o;
  sona_add_defaults(&o);
  seat(i, o.place.position, &o.place.turn_deg);   /* your layout */
  o.start_at = -1;                                 /* random start: a crowd out of step */
  sona_avatar_add_base64(ctx, looks[i % 12], &o, &av[i]);
}
```

Fills `*o` with the defaults above.

## sona_avatar_add

```c
sona_result sona_avatar_add(sona_ctx *ctx, const uint8_t manifest[SONA_MANIFEST_BYTES],
                            const sona_add_options *opts, sona_avatar **out);
```

| Parameter | Type | Meaning | Default |
|---|---|---|---|
| `ctx` | `sona_ctx *` | The context | required |
| `manifest` | `const uint8_t [SONA_MANIFEST_BYTES]` | The manifest's 1,000 bytes | required |
| `opts` | `const sona_add_options *` | The options, a [`sona_add_options`](../fields.md#sona_add_options) | required |
| `out` | `sona_avatar **` | Receives the new avatar | required |

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

**Example** (also in [Untrusted files, long sessions and speed](../../guides/hardening.md)):

```c
sona_manifest m;
if (sona_manifest_parse(manifest, &m) != SONA_OK)   /* any 1,000 bytes from a player */
  puts("not an avatar");                            /* SONA_E_MANIFEST */
else
  sona_avatar_add(ctx, manifest, NULL, &avatar);    /* its items load through your sources */
```

Queues an avatar and returns at once. A bad manifest does not fail here: the avatar moves to `FAILED` with `SONA_E_MANIFEST`, as the web's `add()` never throws for one. It fails at once only with `SONA_E_LIMIT` (over `max_avatars`) or `SONA_E_ARGUMENT`.

## sona_avatar_add_base64

```c
sona_result sona_avatar_add_base64(sona_ctx *ctx, const char *b64,
                                   const sona_add_options *opts, sona_avatar **out);
```

| Parameter | Type | Meaning | Default |
|---|---|---|---|
| `ctx` | `sona_ctx *` | The context | required |
| `b64` | `const char *` | The manifest as base64 text, at most 4096 characters | required |
| `opts` | `const sona_add_options *` | The options, a [`sona_add_options`](../fields.md#sona_add_options) | required |
| `out` | `sona_avatar **` | Receives the new avatar | required |

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

**Example** (also in [Getting started](../../getting-started.md)):

```c
sona_avatar *avatar = NULL;
sona_avatar_add_base64(ctx, b64, NULL, &avatar);   /* b64: the manifest; returns at once */
sona_avatar_play(avatar, "GenericWave", NULL);     /* remembered until it is ready */
```

The same, from base64 text of at most 4096 characters, as the web.

## sona_avatar_state_of

```c
sona_avatar_state sona_avatar_state_of(const sona_avatar *a);
```

| Parameter | Type | Meaning | Default |
|---|---|---|---|
| `a` | `const sona_avatar *` | The avatar | required |

**Returns** `sona_avatar_state`.

**Example** (also in [VR and XR bodies (sona_xr)](../../guides/xr.md)):

```c
sona_xr_input in;
sona_xr_input_defaults(&in);
in.time = now;                                  /* seconds, rising */
in.flags = SONA_XR_HAS_HEAD;                    /* and _HAS_LEFT, _HAS_RIGHT for the grips the runtime tracked */
in.head.position[0] = head_pos[0];
in.head.position[1] = head_pos[1];
in.head.position[2] = head_pos[2];
sona_xr_feed(xr, &in);
if (sona_avatar_state_of(avatar) == SONA_AVATAR_READY) sona_xr_apply(xr, avatar);
sona_update(ctx, dt, &view, NULL);
```

The avatar's state.

## sona_avatar_id

```c
uint32_t sona_avatar_id(const sona_avatar *a);
```

| Parameter | Type | Meaning | Default |
|---|---|---|---|
| `a` | `const sona_avatar *` | The avatar | required |

**Returns** `uint32_t`.

**Example** (also in [Lip sync: a microphone or any audio drives the mouth](../../guides/lipsync.md)):

```c
sona_voice_mouth v[64];
for (i = 0; i < n; i++) sona_voice_mouth_init(&v[i], sona_avatar_id(avatars[i]));
/* each frame: */
uint32_t changed = sona_voice_mouths(avatars, v, levels, n, dt);
```

A number unique within the ctx.

## sona_avatar_play

```c
sona_result sona_avatar_play(sona_avatar *a, const char *clip, const sona_play_options *opts);
```

| Parameter | Type | Meaning | Default |
|---|---|---|---|
| `a` | `sona_avatar *` | The avatar | required |
| `clip` | `const char *` | A clip key or id | required |
| `opts` | `const sona_play_options *` | The options, a [`sona_play_options`](../fields.md#sona_play_options) | `NULL`: the defaults |

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

**Example** (also in [Getting started](../../getting-started.md)):

```c
sona_avatar *avatar = NULL;
sona_avatar_add_base64(ctx, b64, NULL, &avatar);   /* b64: the manifest; returns at once */
sona_avatar_play(avatar, "GenericWave", NULL);     /* remembered until it is ready */
```

Plays a clip. Called before `READY`, it is remembered and applied when the avatar appears. NULL `opts` takes the defaults. Since 1.1 it also clears the clip queue, and returns `SONA_E_ARGUMENT` for an item preview.

## sona_avatar_stop

```c
void sona_avatar_stop(sona_avatar *a);
```

| Parameter | Type | Meaning | Default |
|---|---|---|---|
| `a` | `sona_avatar *` | The avatar | required |

**Returns** Nothing.

**Example** (also in [3D print export](../../guides/print3d.md)):

```c
sona_play_options pl = { 0, 1.0f, 1.25f };          /* no loop, speed 1, from 1.25 s */
sona_avatar_play(a, "GenericWave", &pl);
sona_update(ctx, 0, NULL, NULL);
sona_avatar_stop(a);
sona_update(ctx, 0, NULL, NULL);
sona_print_avatars(&a, 1, &opts, &print);
```

Holds the current pose.

## sona_avatar_move

```c
void sona_avatar_move(sona_avatar *a, const sona_placement *to, float over_seconds);
```

| Parameter | Type | Meaning | Default |
|---|---|---|---|
| `a` | `sona_avatar *` | The avatar | required |
| `to` | `const sona_placement *` | Where to stand, a [`sona_placement`](../fields.md#sona_placement) | required |
| `over_seconds` | `float` | Seconds to ease over; 0: at once | `0`: at once |

**Returns** Nothing.

**Example** (also in [Getting started](../../getting-started.md)):

```c
sona_placement to = { { 1.0f, 0.0f, -2.0f }, 90.0f, 1.0f };
sona_avatar_move(avatar, &to, 0.5f);                  /* over half a second */
sona_avatar_crossfade(avatar, "GenericWave", NULL, 0.2f);
```

Moves the avatar to a new placement over `over_seconds`.

## sona_avatar_set_visible

```c
void sona_avatar_set_visible(sona_avatar *a, int visible);
```

| Parameter | Type | Meaning | Default |
|---|---|---|---|
| `a` | `sona_avatar *` | The avatar | required |
| `visible` | `int` | 1 drawn, 0 hidden | - |

**Returns** Nothing.

**Example**:

```c
sona_avatar_set_visible(avatar, 1);
```

Shows or hides it. A hidden avatar is not drawn. It does nothing to an item preview, which `sona_draw` never draws.

## sona_avatar_bounds

```c
void sona_avatar_bounds(const sona_avatar *a, float center[3], float *radius);
```

| Parameter | Type | Meaning | Default |
|---|---|---|---|
| `a` | `const sona_avatar *` | The avatar | required |
| `center` | `float [3]` | Receives the centre of the bounding sphere, world space | required |
| `radius` | `float *` | Receives the sphere's radius | required |

**Returns** Nothing.

A bounding sphere, in metres.

## sona_avatar_remove

```c
void sona_avatar_remove(sona_avatar *a);
```

| Parameter | Type | Meaning | Default |
|---|---|---|---|
| `a` | `sona_avatar *` | The avatar | required |

**Returns** Nothing.

**Example** (also in [Cookbook: recipes for game moments](../../cookbook.md)):

```c
sona_avatar_remove(avatar);
```

Removes the avatar and frees what only it used. The handle must not be used afterwards.
