Skip to content

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).

sona_npc_defaults

void sona_npc_defaults(sona_npc_options *o);
Parameter and type Meaning
o
sona_npc_options *
The sona_npc_options to fill with the defaults (required)

Returns Nothing.

Example:

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

sona_result sona_npc_create(sona_ctx *ctx, sona_avatar *a,
                            const sona_npc_options *o, sona_npc **out);
Parameter and type Meaning
ctx
sona_ctx *
The context (required)
a
sona_avatar *
The avatar (required)
o
const sona_npc_options *
The options, a 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).

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

sona_npc_destroy

void sona_npc_destroy(sona_npc *npc);
Parameter and type Meaning
npc
sona_npc *
The NPC (required)

Returns Nothing.

Stops the NPC; the avatar stays in the ctx.

sona_npc_say

sona_result sona_npc_say(sona_npc *npc, const char *text, uint32_t *line);
Parameter and type Meaning
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).

Example (also in The NPC kit (sona_npc)):

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

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

sona_npc_emote

sona_result sona_npc_emote(sona_npc *npc, const char *text);
Parameter and type Meaning
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).

Example (also in The NPC kit (sona_npc)):

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

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

sona_npc_look_at

sona_result sona_npc_look_at(sona_npc *npc, const float target[3]);
Parameter and type Meaning
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).

Example (also in The NPC kit (sona_npc)):

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

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

sona_npc_update

sona_result sona_npc_update(sona_npc *npc, double dt);
Parameter and type Meaning
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).

Example (also in The NPC kit (sona_npc)):

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

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

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

sona_result sona_npc_state_get(const sona_npc *npc, sona_npc_state *out);
Parameter and type Meaning
npc
const sona_npc *
The NPC (required)
out
sona_npc_state *
Receives the sona_npc_state (required)

Returns SONA_OK. Errors SONA_E_ARGUMENT (what each means).

Example (also in The NPC kit (sona_npc)):

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

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

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

uint32_t sona_npc_struct_size(const char *name);
Parameter and type Meaning
name
const char *
A public struct's name (required)

Returns uint32_t.

Example:

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

uint32_t sona_npc_struct_offset(const char *struct_dot_field);
Parameter and type Meaning
struct_dot_field
const char *
"struct.field" (required)

Returns uint32_t.

Example:

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.

This page as Markdown · All the docs as one file (llms-full.txt)