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

# Godot: introduction

> Install the Lokutor addon for Godot 4.3+, add a LokutorAgent node to a character, and talk to it in five minutes: microphone, 3D voice, lip sync and game actions in pure GDScript

> [!NOTE]
>
> Preview, version 0.2.0. Tell us what your game needs at [contact@lokutor.com](mailto:contact@lokutor.com).

The Lokutor addon makes a character of your game a voice agent: it listens through the player's microphone, answers in its own voice from where it stands, moves its mouth, and calls your game's functions. It is plain GDScript, so there is nothing to compile.

## What you need

* **Godot 4.3** or newer.
* A **Lokutor API key**, from [app.lokutor.com](https://app.lokutor.com).
* A microphone. Headphones while you try it, so the character does not hear itself.

It works on Windows, macOS, Linux, Android, iOS and in web browsers ([Games in a web browser](https://docs.lokutor.com/guides/game-web.md)). For a web page that is not a Godot game, use the [JavaScript SDK](https://docs.lokutor.com/guides/browser-setup.md).

## Install

**1. Copy the addon**

Copy [`godot/addons/lokutor`](https://github.com/lokutor-ai/lokutor-sdk/tree/main/godot/addons/lokutor) into your project's `addons/` folder.

**2. Enable it**

**Project > Project Settings > Plugins > Lokutor Voice Agents > Enable**. This turns on **Audio > Driver > Enable Input**, which Godot leaves off and the microphone needs.

**3. Ask for the microphone on export**

Android: tick the **Internet** and **Record Audio** permissions in the export preset. macOS and iOS: fill in the preset's microphone usage description.

## Your first character

**1. Add the node**

Select your character and add a child node, **LokutorAgent**. It is a `Node3D` and the voice comes from where it is, so the head is a good place.

**2. Paste your API key**

In **Account > Api Key**. Leave it empty to read the `LOKUTOR_API_KEY` environment variable, which keeps the key out of your scene files. Never ship a game with a key inside: see [Shipping](#shipping).

**3. Say who it is**

Write **Character > Persona**, and pick a **Voice** (`F1`-`F5`, `M1`-`M5`, or a voice from [your dashboard](https://docs.lokutor.com/voices-languages-models.md)) and a **Language**. `auto` follows the language the player speaks.

**4. Press Play and talk**

The character greets the player and listens.

Two demos come with the [`godot/`](https://github.com/lokutor-ai/lokutor-sdk/tree/main/godot) folder, which is a project of its own: **Brom's Forge** (`demo/forge/`), a blacksmith you can talk to and buy from, and `demo/brom_demo.tscn`, the smallest complete example.

## Events

```gdscript theme={null}
extends Node3D

@onready var mira: LokutorAgent = $Mira/Head/LokutorAgent

func _ready() -> void:
    mira.player_said.connect(func(text): print("You: ", text))
    mira.character_said.connect(func(text): $Subtitles.text = Lokutor.strip_tags(text))
    mira.state_changed.connect(_on_state)
    mira.error_occurred.connect(func(code, message): push_warning("%s: %s" % [code, message]))

func _on_state(state: LokutorAgent.State) -> void:
    $ThinkingBubble.visible = state == LokutorAgent.State.THINKING
```

`character_said` carries what the character is about to say, with the [expression tags](https://docs.lokutor.com/api-reference/expression-tags.md) the voice performs (`[laugh]`); `Lokutor.strip_tags()` takes them out.

| State | Meaning |
| :- | :- |
| `DISCONNECTED` | No conversation. |
| `CONNECTING` | Opening it, or opening it again after the connection dropped. |
| `READY` | Listening. |
| `PLAYER_TALKING` | The player is talking. |
| `THINKING` | The player finished; the reply is being made. |
| `SPEAKING` | The character is being heard. |

## Start and stop

A conversation is billed while it is open ([Pricing](https://docs.lokutor.com/pricing.md)), so open it when the player engages. Turn off **Start On Ready** and:

```gdscript theme={null}
func _on_player_entered(_body: Node3D) -> void:
    mira.start_conversation()

func _on_player_left(_body: Node3D) -> void:
    mira.stop_conversation()
```

While it is open you can change who the character is and what it knows:

```gdscript theme={null}
mira.set_persona("You are Mira. The player just insulted you and you want an apology.")
mira.set_context("Gold the player has", 12)     # facts of the moment; null takes one out
mira.set_voice("F3")
mira.set_language("es")
mira.reset_conversation()                        # it forgets what was said
```

When the connection drops it is opened again by itself (`reconnecting`, then `reconnected`). The new session does not remember the old one: `set_context` is sent again, what was said is not. If it cannot be opened, or the server ends it, `conversation_ended` says why.

## Microphone

| `mic_mode` | |
| :- | :- |
| `ALWAYS` (default) | The microphone is open; the server notices when the player stops talking. |
| `PUSH_TO_TALK` | Heard only while the Input Map action in **Push To Talk Action** is held, or between `set_talking(true)` and `set_talking(false)`. |
| `OFF` | No microphone. The character hears what you give `feed_microphone(samples, rate)`: a recording, or another character's `voice_played`. Send silence too when there is nothing to hear. |

**Mute Mic While Speaking** (on) sends silence while the character's voice can still reach the microphone, so laptop speakers do not make it interrupt itself, and while another character or a `LokutorVoice` is heard, so that it does not answer them. Turn it off when players wear headphones and you want them to be able to interrupt. If they do not, and the character hears its own words come back from the speakers, they are not taken for the player's: `guard_against_echo` drops them, emits `echo_heard` and turns mute while speaking back on. `muted = true` is the player's own mute button. Godot picks the microphone with `AudioServer.input_device`; `LokutorMicrophone.devices()` lists them.

## The voice

`spatial_voice` places it in 3D at the node. `voice_bus` sends it to an audio bus (put a radio, a cave or a giant's chest there), and `voice_output` plays it through your own player, which is how a 2D game uses an `AudioStreamPlayer2D`. `pitch` shifts it up or down.

## A dashboard agent

To use an agent built in the [dashboard](https://docs.lokutor.com/guides/my-first-agent.md) (prompt, voice, knowledge, tools), set **Agent Id** and empty **Persona**, **Voice** and **Language**: whatever you leave in them overrides the dashboard. `set_context` needs a persona in the game, because a dashboard prompt cannot be extended from it.

## Shipping

A key inside a game build can be extracted. Have your server trade it for a ticket that lasts ten minutes ([`POST /ws-ticket`](https://docs.lokutor.com/guides/secure-browser-setup)) and give the agent that. `ticket_provider` is called before every connection, reconnections included:

```gdscript theme={null}
extends Node3D

@onready var mira: LokutorAgent = $Mira/Head/LokutorAgent

func _enter_tree() -> void:
    $Mira/Head/LokutorAgent.ticket_provider = _get_ticket

func _get_ticket() -> String:
    var reply: Dictionary = await Lokutor.request_json("https://your-server.example/lokutor-ticket", HTTPClient.METHOD_POST)
    return reply.body.get("ticket", "") if reply.ok else ""
```

Return an empty string when there is no ticket: the conversation ends with `auth.missing_key` instead of waiting. The same property exists on [LokutorVoice](https://docs.lokutor.com/sdks/godot/speech.md).

A web build is shipped the same way: the browser asks your server for the ticket. [Games in a web browser](https://docs.lokutor.com/guides/game-web.md) has the rest (export settings, microphone, download size).

## Next

* [Actions](https://docs.lokutor.com/sdks/godot/actions.md): let the character do things in the game.
* [Lip sync](https://docs.lokutor.com/sdks/godot/lip-sync.md): blend shapes, jaws, 2D mouths.
* [Speech](https://docs.lokutor.com/sdks/godot/speech.md): written lines, text to speech and speech to text.
* [API reference](https://docs.lokutor.com/sdks/godot/reference.md): every class, property, signal and method.
* [Game characters](https://docs.lokutor.com/guides/game-characters.md): what makes one feel alive.
* [Games in a web browser](https://docs.lokutor.com/guides/game-web.md): exporting for the web, as the games on [lokutor.com/games](https://lokutor.com/games/).


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