# Build a merchant conversation

A merchant conversation is a loop with a price tag: the player browses, buys while they can afford it, and leaves when they're done. You'll build Odile's potion stall, show the player's gold in her lines, and lock the purchase once their purse runs dry.

## Before you begin

Complete [Let the player ask questions](question-hub.md) and [Lock dialogue behind a requirement](branch-on-game-state.md). This guide reuses the `gold` entry of the `Progress` Global Variable, which starts at `10`.

Set up the merchant:

1. Add a **Number** entry tagged `potions` to `Progress`, set to `0`.
2. In the `Village` Compendium, add an Actor with **Actor** `Merchant` and **Name** `Odile`.
3. Create a game object for her stall with a **Dialogue** component and a trigger zone, as in [Start a conversation near an NPC](talk-to-an-npc.md).
4. Create a Script named `Potion Stall` and assign it to her **Dialogue** component.

## Show the player's gold

A **Variable** in a Compendium lets a line display a changing value. Open the `Village` Compendium and add two Variables:

| Variable | Value |
| :--- | :--- |
| `gold` | The **Variable** Signal, reading the `gold` entry of `Progress`. |
| `potions` | The **Variable** Signal, reading the `potions` entry of `Progress`. |

{% raw %}
Wrap a Variable's identifier in double curly braces to place its value in a line. Write the opening of `Potion Stall`:
{% endraw %}

{% raw %}
```dialogue
Merchant: Welcome to the Green Kettle! Herbs, tonics, and potions.
Merchant: You've got {{gold}} gold on you. What'll it be?
```
{% endraw %}

Dialogue replaces the placeholder with the entry's current value each time the line plays, so the player reads *You've got 10 gold on you.*

{% raw %}
!!! warning "Unknown Variables show their braces"
    When no Compendium defines a Variable, the placeholder stays in the text as written, such as `{{gold}}`. If braces appear in Play mode, check the identifier's spelling.
{% endraw %}

## Sell a potion

Buying needs a Condition that checks the price and a Command that takes the gold and hands over the potion. Add both to the `Village` Compendium:

| Entry | Configuration |
| :--- | :--- |
| Condition `can-afford-potion` | **Compare Numbers**: the `gold` entry of `Progress`, **Greater Or Equal**, `25`. |
| Command `buy-potion` | **Subtract Number** removing `25` from `gold`, then **Increment Number** adding `1` to `potions`. |

Next, turn the opening into a shop loop with a Label, as in [Let the player ask questions](question-hub.md):

{% raw %}
```dialogue
Merchant: Welcome to the Green Kettle! Herbs, tonics, and potions.

# shop
Merchant: You've got {{gold}} gold on you. What'll it be?

* @if can-afford-potion A healing potion, please. (25 gold)
  @run await buy-potion
  Merchant: One healing potion. That makes {{potions}}. Don't drink them all at once.
  @go shop

* Where do your herbs come from?
  Merchant: The hills east of here, when the wolves let me pick them.
  @go shop

* Nothing today.
  Merchant: Come back when your pockets are heavier!
```
{% endraw %}

{% raw %}
Every pass through `# shop` reads `{{gold}}` again, so the line always shows the current amount. The price sits in the choice text, which keeps it visible even while the player can't afford it.

`@run await` matters here: it finishes the purchase before Odile's reply, so `{{potions}}` already includes the potion she just sold.
{% endraw %}

## Give the player some money

The player starts with `10` gold, so the potion is locked. Test both states by changing the authored value of `gold` in `Progress` to `60`.

With `60` gold, the player can buy two potions. After the second, `gold` drops to `10`, `can-afford-potion` becomes false, and the potion choice stays in the list but can't be picked.

!!! tip "One Condition for every price"
    `can-afford-potion` compares against a fixed `25`. For a stall with several prices, put the price in each choice's metadata, such as `[price: 40]`. A choice is the active line while its `@if` runs, so a single `can-afford` Condition can compare `gold` with **Active Line Metadata** `price`, the way [a reusable skill check](example-1.md) reads its threshold.

## Open a full shop instead

If you use the **Inventory** module, Odile can hand over to its shop interface and pick the conversation up again afterward. Add a Command named `open-shop` to her **Dialogue** component's **Local Compendium**, because it refers to scene objects:

| Instruction | Configuration |
| :--- | :--- |
| **Open Merchant UI** | **Merchant** set to her **Merchant** component, **Client** set to **Target**, **Skin** set to a Merchant Skin, and **Await** on. |

Then call it from a choice:

```dialogue
* Show me everything you've got.
  @run await open-shop
  Merchant: Pleasure doing business!
  @go shop
```

**Target** is the player who walked into Odile's trigger zone, so their Bag pays. Because both `@run` and the Instruction await, Odile's next line plays only after the player closes the shop. The Dialogue Skin's **Show Text During Command** setting decides whether her last line stays on screen behind the shop.

## Verify the result

1. Set `gold` in `Progress` to `60`.
2. Enter Play mode and talk to Odile. Confirm that she mentions `60` gold.
3. Buy a potion. Confirm that she says *That makes 1*, and that the next prompt mentions `35` gold.
4. Buy a second potion, and confirm that the potion choice can no longer be picked.
5. Pick *Nothing today.* and confirm that the conversation ends.

Return `gold` to `10` afterward.

## Where to go next

- **[Build a quest giver](quest-giver.md)** — track a task through several conversations.
- **[Compendiums](../../compendiums.md#variables)** — Variables, Commands, and Conditions in detail.
- **[Scripts](../../scripts.md#choice-and-text-options)** — hide unavailable choices instead of showing them locked.
