> ## Documentation Index
> Fetch the complete documentation index at: https://docs.lokutor.com/llms.txt
> Use this file to discover all available pages before exploring further.

# Sample game: Not Amused

> How a small voice game is put together with the Godot SDK: a king who is a LokutorAgent with lip sync and a react action, guards with written lines, a laughter meter, and facts of the moment

*Make the king laugh. With your voice.* You are a jester on a trapdoor, with one minute and a microphone. The king hears you, answers, and is not amused. Fill his laughter meter and you are pardoned; run out of time and the trapdoor opens.

It is a Godot game with one conversation in it, and shows a structure that works for many: **the model acts, the game keeps score**. This page is its architecture. The code samples are cut down to the idea.

> [!TIP]
>
> **Play it in your browser** at [lokutor.com/games/not-amused](https://lokutor.com/games/not-amused/), with no key and no download: a Godot web export, made as [Games in a web browser](https://docs.lokutor.com/guides/game-web.md) explains.

## The pieces

```
jester (the player)            king                                  guards, king's fixed lines
microphone ───────────────▶ LokutorAgent ──▶ voice, text          LokutorVoice ──▶ written lines
                               │   │   └────▶ LokutorLipSync ◀──────────┘
                               │   └─ states ─▶ body, camera, bubbles
                               └─ react(...) ─▶ meter, mood, guards, score
                                     ▲
game ── set_context: laughter, manners, hurry
```

| Piece | What it is | Why |
| :- | :- | :- |
| The king | `LokutorAgent` with a persona, `react` and `pull_lever` | The only character that improvises |
| His mouth | One `LokutorLipSync` on the face mesh | Shared by his conversation and his written lines |
| His fixed lines | `LokutorVoice` with the king's voice: *Next!* | The end of the round must come on time, word for word |
| The guards | A `LokutorVoice` each, with written lines | They whisper hints when the player freezes; they need no conversation |
| The jester's mouth | Another `LokutorLipSync`, moved by the microphone's level | The player's voice has no visemes |
| The game | A state machine: title, opening, round, won, lost | Owns the clock, the meter, the camera and the rules |

Only the king is a [session](https://docs.lokutor.com/pricing.md#limits), and only while a round is being played.

## The king

The persona says who he is, how short to be (one or two sentences, twenty words at most), and one rule: *call `react` first, then answer aloud*.

```gdscript theme={null}
agent = LokutorAgent.new()
agent.start_on_ready = false
agent.persona = PERSONA
agent.voice = "M2"
agent.language = "en"
agent.lip_sync = king_lips
agent.add_action("react", "How the jester's last line made you feel. Call it before every answer.", _on_react, {
    "reaction": {"type": "string", "enum": ["stone_face", "raised_eyebrow", "trembling_lips", "escaped_giggle", "open_laugh", "deep_offence", "unfinished_joke"], "description": "What your face does"},
    "amusement": {"type": "integer", "minimum": 0, "maximum": 10, "description": "How funny it was"}})
agent.add_action("pull_lever", "Open the trapdoor under the jester. The audience is over.", _on_lever)
add_child(agent)
```

`react` is the hinge of the whole game. The model is a good judge of jokes and a bad accountant, so it gives an opinion and the game turns it into points:

```gdscript theme={null}
func _on_react(args: Dictionary) -> String:
    var funny := clampi(int(args.get("amusement", 0)), 0, 10)
    amusement = clampf(amusement + roundf((funny - 2.5) * 5.2) if funny >= 3 else amusement, 0.0, 100.0)
    perform(str(args.get("reaction", "bored")))
    agent.set_context("How close you are to laughing", "%d out of 100" % int(amusement))
    if amusement >= 100.0:
        return "You cannot hold it any longer. Laugh out loud and pardon the jester."
    return "Your laughter is at %d of 100. Keep a straight face and answer." % int(amusement)
```

* The action and its rules are given to him after his greeting, with `add_action` and `set_persona`: before, his persona has only who he is and how he opens.
* The names of the reactions are not words he could take for a tag. With *bored* and *snort* among them he sometimes wrote `[bored]`, and a bracket that is not one of the [expression tags](https://docs.lokutor.com/api-reference/expression-tags.md) is read aloud.
* `unfinished_joke` is for a joke that is not over (*Three men walk into a castle...*). It gives no points, and the answer is a word or two: *And?*
* `speak_before_actions` is off: with an action that comes before any word, what the model writes first can be its plan (*so I call react first*).
* It is called **before** he speaks, so the snort, the guards trying not to laugh and the points appear while his answer is still being made.
* What it **returns** directs the next sentence. The king laughs because the game told him to, at the moment the meter is full.
* A value that makes no sense is clamped. An action that does not apply any more says so (*The lever is stuck.*).

## Facts of the moment

Two facts change during a round, and both go in [context](https://docs.lokutor.com/guides/game-characters.md#facts-of-the-moment): how close he is to laughing, and, when the clock is nearly out, that he should hurry the jester. He used to know the seconds left, and said them in every answer.

## The clock is the jester's

The minute runs only while it is the player's turn: not while the king speaks or thinks, and not while a guard whispers. Latency costs the player nothing, and a long answer from the king is not a punishment.

```gdscript theme={null}
if not agent.is_speaking and agent.state != LokutorAgent.State.THINKING and not guard_voice.speaking:
    seconds_left = maxf(0.0, seconds_left - delta)
```

When it runs out, the game waits until nobody is talking, and the king's *written* line closes the round.

## States move the body

`state_changed` drives the stage and nothing else does:

| State | King | Camera |
| :- | :- | :- |
| `PLAYER_TALKING` | Leans in, listens | Stays |
| `THINKING` | Keeps the last reaction | Stays |
| `SPEAKING` | A talking clip, picked at random; the head moves with `mouth_open` | Cuts to him, closer as the meter fills |
| `READY` | Back to bored, or to holding it | Cuts to both |

On top go the [layers](https://docs.lokutor.com/guides/game-characters.md#layers): a mood from `react`, eased by a spring, whose mouth fades to 30% while he speaks; blinks and glances; springs on belly and crown; visemes. The squash of each reaction is one line.

## The guards

They say written lines, fetched while the title screen is up, so a hint starts the moment it is needed:

```gdscript theme={null}
guard_voice.preload_lines(PackedStringArray(HINTS))

func hint(text: String) -> void:
    guard.play("whisper")
    await guard_voice.say(text)
    guard.play("stand")
```

A hint comes when the player has been silent for several seconds and the king is waiting. It is also how the game teaches itself, without a tutorial.

## The player on screen

The jester's mouth opens with the microphone's level, and so do a meter on screen and the energy of the body. Seeing yourself heard answers the first question of every voice game.

```gdscript theme={null}
var level: float = agent.microphone.level if agent.microphone else 0.0
jester_lips.advance(0.0, clampf(level * 3.0, 0.0, 1.0), level > 0.03, delta)
```

The bubble with what was understood comes from `player_said`, and the king's from `character_said`.

## Echo

With headphones the microphone stays open while the king talks; with speakers he would hear himself and stop. The game finds out which: while he greets the jester, `mute_mic_while_speaking` is on, and the loudness of the microphone is compared with `agent.voice_level()` at every delay up to half a second. If they rise and fall together, it is his own voice, and the microphone keeps closing while he talks. A key says it by hand.

## Interrupting a king

`character_interrupted` says that the player cut the character off, and what was heard of the reply. The king takes it badly: his face and body at once, and his words when he answers, because the game tells him what happened.

```gdscript theme={null}
agent.character_interrupted.connect(func(heard: String):
    cuts += 1
    king.play("angry")
    agent.set_context("The jester's manners",
        "The jester has just cut you off, after you had only said \"%s\". Complain about it first, with [angry]." % heard))
```

The fact is taken out with `null` once he has said it. A player who answers on the last word has not cut anybody off: compare what was heard with the whole reply (`transcript_truncated` of `agent.client` has both). Nor has one who goes on in his first second and a half: they had only paused in the middle of a joke, and it was the king who spoke too early. He is told that what comes is the rest of it.

## Nine languages

A prompt in one language and a conversation in another leak into each other. When the player chooses a language, everything that the king reads changes with it: the persona, what his actions say and answer, the facts of the moment (`set_context`) and the line above them (`context_heading`). Names of actions, values of enums and tags are code, and stay.

```gdscript theme={null}
agent.language = lang
agent.persona = Prompts.persona(lang)
agent.context_heading = Prompts.fact(lang, "now")
agent.add_action("react", Prompts.action(lang, "react"), _on_react, {...})   # the same name replaces it
```

## Assets

Every character is built by code and exported as GLB with the 15 visemes and the same set of expressions; contact sheets of every shape are drawn after each build. See [Asset pipeline](https://docs.lokutor.com/guides/game-asset-pipeline.md).

## Testing it

A round is played from a script in the game's own code path: lines for the jester, and what the king does with each (`react` with a number, then a sentence). Played against a [mock server](https://docs.lokutor.com/guides/game-dev-server.md) it is a test; played in front of the camera, it is how a trailer is recorded.

## Make your own

The same structure, with other words: a suspect who cracks (`react` is *suspicion*), a bouncer to talk past (`react` is *patience*), a date (`react` is *interest*). One agent, one action that gives an opinion, one number the game owns, and an ending the game decides.


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.