# Run game actions from a conversation

A conversation becomes part of the game when it changes something: a reward, an open door, a quest that starts. A **Command** is a named list of Instructions that a Script runs with `@run`. You'll make the guard hand over a gate pass and a few coins, then open the gate while the conversation waits.

## Before you begin

Complete [Introduce your characters](introduce-characters.md), which created the `Village` Compendium, and [Start a conversation near an NPC](talk-to-an-npc.md). You'll also need a game object named `Gate Door` that blocks the entrance, and an `AudioClip` of a creaking gate.

Create a new Script named `Gate Pass` and assign it to the guard's **Dialogue** component.

## Store the player's progress

Commands change game state, so that state needs a home. A **Global Variable** asset keeps values that every Event and Script in the game can read and write.

1. Right-click a folder in the Project window and choose `Create` &rarr; `Visual Script` &rarr; `Variable`.
2. Name the asset `Progress`.
3. Add a **Bool** entry tagged `has-gate-pass`, set to off.
4. Add a **Number** entry tagged `gold`, set to `10`.

The guides that follow add more entries to `Progress`. See [Global Variables](https://docs.visualscript.dev/manual/variables/global-variables) in the Visual Script manual for saving them between sessions.

## Define a Command

Open the `Village` Compendium, click **Add Command…**, and set **Command** to `give-gate-pass`. Then add one Instruction to it:

| Instruction | Configuration |
| :--- | :--- |
| **Set Bool** | **Set** the `has-gate-pass` entry of `Progress`, **Value** on. |

A Command identifier accepts letters, digits, hyphens (`-`), and underscores (`_`). Like an Actor, it's matched exactly, including capitalization.

## Run it from the Script

Write the conversation in `Gate Pass`, with `@run` and the Command identifier on its own line:

```dialogue
Guard: You're the one who drove off that wolf. I owe you.
Guard: Take this. Show it at the gate, and nobody in Ashford will stop you.
@run give-gate-pass
You received a gate pass.
```

When the Script reaches `@run`, it looks up `give-gate-pass` in the Compendiums, starts its Instructions, and moves to the next line straight away.

!!! warning "A missing Command does nothing"
    If no Compendium defines the identifier, `@run` skips it without an error, and the conversation carries on. When an action doesn't happen, check the identifier's spelling in both places first.

## Wait for a Command to finish

Opening the gate takes a moment, and the guard's next line should wait for it. That Command also needs `Gate Door`, which is a scene object, and a Compendium asset can't reference objects in a scene. Define it on the guard's **Dialogue** component instead:

1. Select the guard and expand the **Local Compendium** of its **Dialogue** component.
2. Add a Command named `open-gate`.
3. Add these Instructions in order:

| Instruction | Configuration |
| :--- | :--- |
| **Play Sound Effect** | The creaking gate clip. |
| **Wait for Seconds** | **Seconds** `1.5`. |
| **Set Active** | **Game Object** `Gate Door`, **Is Active** off. |

Then add the Command to the Script with `await`:

```dialogue hl_lines="5 6"
Guard: You're the one who drove off that wolf. I owe you.
Guard: Take this. Show it at the gate, and nobody in Ashford will stop you.
@run give-gate-pass
You received a gate pass.
Guard: Let me get that for you.
@run await open-gate
Guard: Welcome to Ashford.
```

With `await`, the conversation pauses at `@run` until every Instruction in the Command has finished. *Welcome to Ashford.* appears only after the door is gone. Without `await`, it would appear while the gate is still creaking.

Use `await` when the next line depends on the result, such as a door that must be open or a camera that must finish moving. Leave it out for effects that can overlap the next line, such as a sound.

!!! tip "Local for this scene, global for everything else"
    `open-gate` only makes sense on this guard, so it lives on his component. `give-gate-pass` works anywhere, so any Script can use it from the `Village` Compendium. When two definitions share an identifier, the component's Local Compendium wins.

## Pass a value to a Command

The guard also hands over a few coins. You could write a `give-gold-5` Command, but then every amount would need its own Command. Instead, write the amount on the `@run` line as metadata, and let one Command read it.

1. In the `Village` Compendium, add a Command named `give-gold`.
2. Add an **Increment Number** Instruction.
3. Set **Set** to the `gold` entry of `Progress`.
4. Set **Value** to the numeric **Active Line Metadata** Signal, with **Dialogue** set to **Source** and **Metadata** set to `amount`.

Then pass the amount from the Script:

```dialogue hl_lines="5 6"
Guard: You're the one who drove off that wolf. I owe you.
Guard: Take this. Show it at the gate, and nobody in Ashford will stop you.
@run give-gate-pass
You received a gate pass.
Guard: And a few coins for your trouble.
@run await give-gold [amount: 5]
Guard: Let me get that for you.
@run await open-gate
Guard: Welcome to Ashford.
```

Inside a Command, **Source** is the game object that holds the running **Dialogue** component, so the Signal reads the line that guard is playing right now. The `await` keeps `@run give-gold` as the active line until the Command finishes, so the metadata is still there when **Increment Number** reads it.

!!! info "A missing key reads as zero"
    **Active Line Metadata** reads whole numbers and decimals, such as `2.5`. When the active line has no such key, or its value isn't a number, the Signal returns `0`, so a misspelled key gives the player nothing rather than an error.

The same Command now covers every reward in the game: `[amount: 5]` here, `[amount: 50]` for a quest.

## Verify the result

1. Select `Progress` in the Project window so you can watch its values.
2. Enter Play mode and talk to the guard.
3. After *You received a gate pass.*, confirm that `has-gate-pass` is on.
4. After *And a few coins for your trouble.*, confirm that `gold` changed from `10` to `15`.
5. Confirm that the gate creaks, the door disappears, and only then does the guard say *Welcome to Ashford.*

## Where to go next

- **[Lock dialogue behind a requirement](branch-on-game-state.md)** — read `has-gate-pass` and `gold` back to change the conversation.
- **[Compendiums](../../compendiums.md#commands)** — Commands, `@run`, and `@run await` in detail.
- **[Language syntax](../../syntax.md#metadata)** — the metadata format and its reserved keys.
