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

# Unity: actions

> Let a Unity character call your game's functions from the conversation: AddAction with arguments, actions 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: add an entry under **Game actions the character can use**.

| Field | What to write |
| :- | :- |
| **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.* |
| **On Called** | What happens in the game: any method, like `Gate.Open`. |

## In code, with arguments

```csharp theme={null}
using System.Collections.Generic;
using System.Linq;
using Lokutor;
using UnityEngine;

public class Shop : MonoBehaviour
{
    static readonly Dictionary<string, int> Prices = new Dictionary<string, int> { ["iron sword"] = 20, ["healing potion"] = 5 };

    int gold = 30;
    readonly List<string> bag = new List<string>();

    void Awake()
    {
        GetComponent<LokutorAgent>().AddAction("give_item", "Sell an item to the player: takes the gold and puts the item in their bag.", GiveItem,
            new ActionParam("item", "Which item", ParamType.String, true, Prices.Keys.ToArray()));
    }

    string GiveItem(ActionArgs args)
    {
        string item = args.GetString("item");
        if (!Prices.TryGetValue(item, out int price)) return "There is no " + item + " in the shop.";
        if (gold < price) return "The player has only " + gold + " gold: not enough.";
        gold -= price;
        bag.Add(item);
        return "Gave the " + item + ". The player has " + gold + " gold left.";
    }
}
```

* An **`ActionParam`** is a name, a description, a type (`String`, `Number`, `Integer`, `Boolean`), whether it is required, and the values allowed, if they are few.
* The **handler** runs on the main thread with the arguments the character chose (`GetString`, `GetInt`, `GetNumber`, `GetBool`, `Has`, or the raw `Json`), 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

`AddActionAsync` takes a handler that awaits. The character waits for the result, and stays in `Thinking`:

```csharp theme={null}
shopkeeper.AddActionAsync("fetch_sword", "Bring the sword from the back of the shop.", async args =>
{
    await WalkTo(backRoom);
    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 `SetContext`.

## During the game

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

```csharp theme={null}
shopkeeper.AddAction("show_secret_door", "Show the player the door behind the shelf. Only if they said the password.", args => OpenDoor());
shopkeeper.RemoveAction("give_item");           // sold out
shopkeeper.ActionCalled += (name, args, result) => Debug.Log(name + " " + args.Json + " -> " + 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:

```csharp theme={null}
king.AddAction("react", "Call this with every answer, before speaking: how the last thing the player said made you feel.", args =>
{
    animator.SetTrigger(args.GetString("feeling", "bored"));
    return "Done.";
}, new ActionParam("feeling", "Your reaction", ParamType.String, true, "bored", "amused", "laugh", "offended"));
```

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