> ## 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: actions

> Let a Godot character call your game's functions from the conversation: add_action with arguments, LokutorAction in the Inspector, actions that take time, changing them during play, and testing them

An action is a function of your game that the character can decide to call: sell an item, open a gate, start a quest, get angry. It is a [tool call](https://docs.lokutor.com/sdk/tool-calling.md) of the voice agent, set up from the game.

## In the Inspector

Without code: under **Game Actions** of the LokutorAgent add a `LokutorAction`.

| Field | What to write |
| :- | :- |
| `action_name` | What the character calls it: `open_gate`. Letters, digits and `_`. |
| `description` | When to use it, in plain words: *Opens the town gate. Only after the player gives the password.* |
| `reply` | What the character is told happened: *The gate is now open.* |
| `target`, `method` | The node and the method to call, without arguments. |

## In code, with arguments

```gdscript theme={null}
extends Node3D

const PRICES := {"iron sword": 20, "healing potion": 5}

@onready var shopkeeper: LokutorAgent = $Shopkeeper/Head/LokutorAgent

var gold := 30
var bag: Array[String] = []

func _ready() -> void:
    shopkeeper.add_action("give_item", "Sell an item to the player: takes the gold and puts the item in their bag.", _give_item,
        {"item": {"type": "string", "description": "Which item", "enum": PRICES.keys()}})

func _give_item(args: Dictionary) -> String:
    var item: String = args.get("item", "")
    if not PRICES.has(item):
        return "There is no %s in the shop." % item
    if gold < PRICES[item]:
        return "The player has only %d gold: not enough." % gold
    gold -= PRICES[item]
    bag.append(item)
    return "Gave the %s. The player has %d gold left." % [item, gold]
```

* **`params`** maps each argument to a description (`{"item": "Which item"}`) or to a JSON-schema dictionary with `type`, `description`, `enum`... Every argument is required unless its dictionary says `"optional": true`.
* The **handler** runs on the main thread with the arguments the character chose, and returns a sentence saying what happened. The character answers with it, so write facts: what changed, what failed and why.
* **Check the arguments.** They come from a language model: an item that does not exist, a price it made up. The game decides; the sentence tells the character.

## Actions that take time

A handler may `await`. The character waits for the result, and stays in `THINKING`:

```gdscript theme={null}
func _ready() -> void:
    shopkeeper.add_action("fetch_sword", "Bring the sword from the back of the shop.", _fetch_sword)

func _fetch_sword(_args: Dictionary) -> String:
    $Shopkeeper.walk_to($BackRoom)
    await $Shopkeeper.arrived
    return "The sword is on the counter."
```

Keep it to a few seconds. For something longer, return at once ("On my way.") and tell the character later with `set_context`.

## During the game

Actions can be added and removed at any time; the character knows from its next reply.

```gdscript theme={null}
shopkeeper.add_action("show_secret_door", "Show the player the door behind the shelf. Only if they said the password.", _show_door)
shopkeeper.remove_action("give_item")           # sold out
shopkeeper.action_called.connect(func(action_name, args, result): print(action_name, args, result))
```

An action that is not there cannot be called, which is a better lock than a sentence in the persona.

## Reactions

An action does not have to change the world. One that only tells the game how the character feels drives its face and body:

```gdscript theme={null}
func _ready() -> void:
    king.add_action("react", "Call this with every answer, before speaking: how the last thing the player said made you feel.", _react,
        {"feeling": {"type": "string", "description": "Your reaction", "enum": ["bored", "amused", "laugh", "offended"]}})

func _react(args: Dictionary) -> String:
    $King.play_mood(args.get("feeling", "bored"))
    return "Done."
```

[Game characters](https://docs.lokutor.com/guides/game-characters.md) puts this together with animation.

## Test them without talking

The handler is a function: call it. To check that the agent is told what you think, run the conversation against the [mock server](https://docs.lokutor.com/guides/game-dev-server.md).


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