# C API: NPC kit module (sona_npc.h)

An optional library, `sona_npc`: an avatar that says its lines with lip sync, emotes and looks at the player. Include `sona_npc.h` and link `sona_npc`. The guide: [The NPC kit (sona_npc)](../../guides/npc.md).

## sona_npc_defaults

```c
void sona_npc_defaults(sona_npc_options *o);
```

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

**Returns** Nothing.

**Example**:

```c
sona_npc_options x_npc_options;
memset(&x_npc_options, 0, sizeof x_npc_options);
x_npc_options.size = sizeof x_npc_options;
sona_npc_defaults(&x_npc_options);
```

`size` set; `look_seconds` 2, `idle_emote_seconds` 8 (0: never).

## sona_npc_create

```c
sona_result sona_npc_create(sona_ctx *ctx, sona_avatar *a, const sona_npc_options *o, sona_npc **out);
```

| Parameter | Type | Meaning | Default |
|---|---|---|---|
| `ctx` | `sona_ctx *` | The context | required |
| `a` | `sona_avatar *` | The avatar | required |
| `o` | `const sona_npc_options *` | The options, a [`sona_npc_options`](../fields.md#sona_npc_options) | `NULL`: the defaults |
| `out` | `sona_npc **` | Receives the new NPC | required |

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

Drives a, which must stay in ctx while the NPC lives. NULL options: the defaults.

## sona_npc_destroy

```c
void sona_npc_destroy(sona_npc *npc);
```

| Parameter | Type | Meaning | Default |
|---|---|---|---|
| `npc` | `sona_npc *` | The NPC | required |

**Returns** Nothing.

Stops the NPC; the avatar stays in the ctx.

## sona_npc_say

```c
sona_result sona_npc_say(sona_npc *npc, const char *text, uint32_t *line);
```

| Parameter | Type | Meaning | Default |
|---|---|---|---|
| `npc` | `sona_npc *` | The NPC | required |
| `text` | `const char *` | The line to say, UTF-8 | required |
| `line` | `uint32_t *` | Receives the line's number; may be NULL | `NULL`: not written |

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

**Example** (also in [The NPC kit (sona_npc)](../../guides/npc.md)):

```c
uint32_t id;
sona_npc_say(npc, "Hello there! Welcome to my shop.", &id);   /* queued; said in order */
sona_npc_emote(npc, "laugh");                                  /* at once, without a word */
float head[3] = { px, py, pz };                                /* the player's head, world space */
sona_npc_look_at(npc, head);                                   /* NULL: no one to look at */
```

Queues a line (UTF-8). *line (may be NULL) is its number, for the line callback.

## sona_npc_emote

```c
sona_result sona_npc_emote(sona_npc *npc, const char *text);
```

| Parameter | Type | Meaning | Default |
|---|---|---|---|
| `npc` | `sona_npc *` | The NPC | required |
| `text` | `const char *` | What to do, such as `"wave"` | required |

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

**Example** (also in [The NPC kit (sona_npc)](../../guides/npc.md)):

```c
uint32_t id;
sona_npc_say(npc, "Hello there! Welcome to my shop.", &id);   /* queued; said in order */
sona_npc_emote(npc, "laugh");                                  /* at once, without a word */
float head[3] = { px, py, pz };                                /* the player's head, world space */
sona_npc_look_at(npc, head);                                   /* NULL: no one to look at */
```

Emotes from text at once, without speaking ("wave", "laugh").

## sona_npc_look_at

```c
sona_result sona_npc_look_at(sona_npc *npc, const float target[3]);
```

| Parameter | Type | Meaning | Default |
|---|---|---|---|
| `npc` | `sona_npc *` | The NPC | required |
| `target` | `const float [3]` | The point to look at, world space; NULL: stop looking | `NULL`: stop looking |

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

**Example** (also in [The NPC kit (sona_npc)](../../guides/npc.md)):

```c
uint32_t id;
sona_npc_say(npc, "Hello there! Welcome to my shop.", &id);   /* queued; said in order */
sona_npc_emote(npc, "laugh");                                  /* at once, without a word */
float head[3] = { px, py, pz };                                /* the player's head, world space */
sona_npc_look_at(npc, head);                                   /* NULL: no one to look at */
```

The player's head, world space; NULL: no player to look at.

## sona_npc_update

```c
sona_result sona_npc_update(sona_npc *npc, double dt);
```

| Parameter | Type | Meaning | Default |
|---|---|---|---|
| `npc` | `sona_npc *` | The NPC | required |
| `dt` | `double` | Seconds since the last call | - |

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

**Example** (also in [The NPC kit (sona_npc)](../../guides/npc.md)):

```c
sona_npc_update(npc, dt);
sona_update(ctx, dt, NULL, NULL);

sona_npc_state st;
st.size = sizeof st;
sona_npc_state_get(npc, &st);   /* SONA_NPC_IDLE, _SPEAKING (st.line), _EMOTING; st.queued */
```

Advances the NPC by `dt` seconds: the line being said and its lip sync, emotes, the look at the player and the idle. Call it each frame before `sona_update`.

## sona_npc_state_get

```c
sona_result sona_npc_state_get(const sona_npc *npc, sona_npc_state *out);
```

| Parameter | Type | Meaning | Default |
|---|---|---|---|
| `npc` | `const sona_npc *` | The NPC | required |
| `out` | `sona_npc_state *` | Receives the [`sona_npc_state`](../fields.md#sona_npc_state) | required |

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

**Example** (also in [The NPC kit (sona_npc)](../../guides/npc.md)):

```c
sona_npc_update(npc, dt);
sona_update(ctx, dt, NULL, NULL);

sona_npc_state st;
st.size = sizeof st;
sona_npc_state_get(npc, &st);   /* SONA_NPC_IDLE, _SPEAKING (st.line), _EMOTING; st.queued */
```

What the NPC is doing: idle, speaking or emoting, the line being said (0 when none) and how many lines wait.

## sona_npc_struct_size

```c
uint32_t sona_npc_struct_size(const char *name);
```

| Parameter | Type | Meaning | Default |
|---|---|---|---|
| `name` | `const char *` | A public struct's name | required |

**Returns** `uint32_t`.

**Example**:

```c
uint32_t x_got = sona_npc_struct_size("sona_config");
printf("sona_npc_struct_size: %lld\n", (long long)x_got);
```

sizeof a public struct of this header by name, for the bindings' layout checks; 0 when unknown.

## sona_npc_struct_offset

```c
uint32_t sona_npc_struct_offset(const char *struct_dot_field);
```

| Parameter | Type | Meaning | Default |
|---|---|---|---|
| `struct_dot_field` | `const char *` | `"struct.field"` | required |

**Returns** `uint32_t`.

**Example**:

```c
uint32_t x_got = sona_npc_struct_offset("example");
printf("sona_npc_struct_offset: %lld\n", (long long)x_got);
```

offsetof a field by "struct.field"; `UINT32_MAX` when unknown.
