---
title: "商店与交易"
description: "做一个让玩家花金币买药水和剑的商店，一条行为负责检查价格，买不起的时候告诉他。"
---
<div v-pre>

# 商店与交易

> 一个玩家能买东西的商店：玩家看得到自己的金币和在卖什么，点「购买」，金币扣掉，再告诉他买到了什么。金币不够的话，就告诉他不够。整个商店只要一条行为，每个按钮只负责告诉它买的是哪件、多少钱。

---

## 你要做的东西

- **金币显示在画面上**
- **商店页**，每件商品一个购买按钮：Potion（20 金币）、Iron Sword（50 金币）
- **买东西**：金币按价格扣掉，弹个提示说买了什么，同时告诉 AI
- **金币不够**：「Not enough gold: Iron Sword costs 50.」什么都不扣
- **买到的东西进玩家的背包**

### 怎么运作

```
The player taps "Buy Iron Sword"
  → The button sets off the behavior "Buy", passing item = Iron Sword, price = 50
  → The behavior checks: Gold ≥ {param.price}?
    → Yes: Gold minus {param.price}, notice "You bought an Iron Sword", the AI is told
    → No: the notice from "If the conditions don't hold, say", and nothing else happens
```

`{param.price}` 和 `{param.item}` 代表按钮传进来的值。所以不管有多少件商品，一条行为就够了。

---

## 一步一步来

### 第 1 步：建变量

点卡底下 **还可以加** 里的 **＋ 变量**。

#### Gold

| 字段 | 值 |
|-------|-------|
| 名称 | Gold |
| 类型 | 数值 |
| 初始值 | `100` |
| 范围 | `0` 到（不填） |
| 行为规则 | `The player's gold. The shop takes its prices off automatically. You may also change it in the story: quest rewards, robbery, a treasure chest.` |

最小值 `0` 是一道保险：行为本来就会检查玩家买不买得起，但设了范围以后，金币无论如何都不会显示成负数。

点 **放到玩家界面上**，让 Gold 一直显示在聊天里。

#### 背包

最省事的背包来自 **玩家界面** → **模板** → **背包**：一个背包页、拿到新东西时的弹窗，还有一个 **背包** 列表变量，它的行为规则会告诉 AI 剧情里给了玩家什么就往里加。见[背包与装备](./inventory.md)这篇食谱。

---

### 第 2 步：建一条「购买」行为

点 **还可以加** 里的 **＋ 行为**。

| 部分 | 设置 |
|------|---------|
| 什么时候触发 | **玩家点了某个按钮**，按钮 `buy` |
| 如果…… | Gold **≥** `{param.price}` |
| 效果 | **修改变量**：Gold 减去 `{param.price}` |
| | **显示通知**：`You bought: {param.item}`，样式选 **成功** |
| | **告诉 AI**：`The player just bought {param.item} for {param.price} gold. Add it to their Inventory.` |
| 条件不满足时告诉玩家 | `Not enough gold: {param.item} costs {param.price}.` |

不写最后这一行的话，买不起的玩家点了按钮，什么反应都没有。

**告诉 AI** 会给 AI 塞一句话，只管下一次回复。配上背包模板的行为规则，AI 会把东西放进背包，并弹出「得到了」的提示。

::: info 让东西稳稳地进背包
行为的效果能改数值和文字，但没法往列表里加一行。这里是 AI 在 **告诉 AI** 的提醒下把东西加进去的。如果需要分毫不差：

- **能数的东西用数值记**：给药水单独建一个数值变量，再单独做一条「买药水」行为，效果里多一个 **修改变量** Potions 加上 `1`
- **代码效果**：在行为的效果里加一段 **代码**，写一行 `ctx.push("Inventory", { name: "Potion", note: "Heals a little." })`。见[行为 · 代码行为](/zh/creator/automation#代码行为)
- **界面代码**：下面写代码的做法会自己写这个列表
:::

---

### 第 3 步：做商店页

点顶栏中间的 **玩家界面**。用 **新建一页** 加一页（选 **空白页**），起名叫「商店」。

1. 加一个 **文字** 部件，写 `Gold: {Gold}`（用 **插入变量的值…**）
2. 每件商品加一个 **文字** 部件写名字和介绍，再加一个写着 `Buy · 20 gold` 的 **按钮**
3. 在按钮的 **按下时，依次做** 里加 **让一条行为生效** → 购买，然后点两次 **＋ 传一个值进去**：`item` = `Potion`，`price` = `20`。剑就是 `Iron Sword` 和 `50`
4. 加一个按钮，步骤选 **跳到页面**，跳回对话页；再在对话页上加一个「商店」按钮跳到这一页

想把买不起的商品显示出来，就把购买按钮复制一个，把复制的那个弄成灰色的样子，再用 **什么时候显示** → **变量满足条件时**：灰色的那个填 Gold **小于** `20`，正常的那个填 Gold **大于等于** `20`。灰色的按钮照样能让行为生效，所以点它会弹出「Not enough gold」的提示。

把 **编辑** 关掉就能点着试试这一页。预览不会调用 AI，但能看到金币在变。

::: tip 会变的价格
价格可以来自一个变量：在 **＋ 传一个值进去** 里写 `{{Potion price}}`，不写 `20`。改了这个变量（比如过节时用一条行为改），所有按钮都跟着变。
:::

---

### 第 4 步：试玩

点顶栏的 **试玩**。

1. Gold 显示 100。打开 **商店**
2. 买一瓶 Potion：Gold 80，「You bought: Potion」。发一条消息，AI 会提到这件事，药水出现在背包里
3. 再买一瓶：Gold 60
4. 买 Iron Sword：Gold 10
5. 随便再买点什么：「Not enough gold: Potion costs 20.」Gold 还是 10

试玩右边的 **这回合** 能看到购买行为有没有触发，没触发的话是为什么。

**如果出了问题：**

| 现象 | 可能的原因 | 怎么改 |
|---------|-------------|-----|
| 点购买没反应 | 按钮的 **让一条行为生效** 选的是别的行为，或者这条行为没 **启用** | 两个都检查一下 |
| 金币没变 | 传进去的值的名字和行为里的 `{param.…}` 不一样 | 按钮上是 `price`，行为里是 `{param.price}`，拼写要一样 |
| 金币不够时没有提示 | **条件不满足时告诉玩家** 没写 | 把那句话写上 |
| 金币变成负数 | 少了条件，或者 Gold 没设最小值 | 保留 **如果……**，**范围** 从 `0` 开始 |
| 背包里一直没出现这件东西 | AI 没加进去 | 行为里保留 **告诉 AI**，背包变量的行为规则也留着，或者用上面那几种分毫不差的做法 |

---

## 写代码的做法：聊天下方的商店面板

这个版本用界面代码画商店。它检查金币时还是用 **购买** 这条行为，所以价格和「金币不够」的提示都留在画布上；买成以后，它自己把东西加进一个 `inventory` 列表。这里假设你有一个叫 `inventory`、初始值为 `[]` 的列表/表格变量。

**面板 → 界面代码** → 打开 `index.tsx`，把默认的 `return <Chat />` 换掉：

```tsx
export default function MyWorld() {
  const api = useYumina();
  const msgs = api.messages || [];

  const gold = Number(api.variables["Gold"] ?? 0);
  const inventory = Array.isArray(api.variables.inventory) ? api.variables.inventory : [];

  const shopItems = [
    { name: "Potion",     price: 20, icon: "🧪", desc: "Restores a little health" },
    { name: "Iron Sword", price: 50, icon: "⚔️", desc: "A plain iron sword" },
  ];

  const buy = async (item) => {
    // Run the "buy" behavior and wait for it; firedIds is empty if its
    // condition failed (it shows its own "not enough gold" notice)
    const result = await api.executeActionAndWait("buy", { item: item.name, price: item.price });
    if (result.firedIds.length > 0) {
      const current = Array.isArray(api.variables.inventory) ? api.variables.inventory : [];
      api.setVariable("inventory", [...current, item.name]);
    }
  };

  return (
    <Chat renderBubble={(msg) => {
      const isLastMsg = msg.messageIndex === msgs.length - 1;
      return (
        <div>
          <div
            style={{ color: "#e2e8f0", lineHeight: 1.7 }}
            dangerouslySetInnerHTML={{ __html: msg.contentHtml }}
          />

          {isLastMsg && (
            <div style={{
              marginTop: "16px",
              padding: "16px",
              background: "rgba(15, 23, 42, 0.6)",
              borderRadius: "12px",
              border: "1px solid #334155",
            }}>
              {/* Gold */}
              <div style={{
                display: "flex", alignItems: "center", gap: "8px", marginBottom: "16px",
                padding: "10px 14px", background: "linear-gradient(135deg, #78350f, #92400e)",
                borderRadius: "8px", border: "1px solid #b45309",
              }}>
                <span style={{ fontSize: "20px" }}>💰</span>
                <span style={{ color: "#fde68a", fontSize: "16px", fontWeight: "bold" }}>{gold} Gold</span>
              </div>

              {/* Items for sale */}
              <div style={{ display: "flex", flexDirection: "column", gap: "8px", marginBottom: "16px" }}>
                {shopItems.map((item) => {
                  const affordable = gold >= item.price;
                  return (
                    <div key={item.name} style={{
                      display: "flex", alignItems: "center", justifyContent: "space-between",
                      padding: "10px 14px", background: "rgba(30, 41, 59, 0.8)",
                      borderRadius: "8px", border: "1px solid #475569",
                    }}>
                      <div style={{ display: "flex", alignItems: "center", gap: "10px" }}>
                        <span style={{ fontSize: "22px" }}>{item.icon}</span>
                        <div>
                          <div style={{ color: "#e2e8f0", fontSize: "14px", fontWeight: "600" }}>{item.name}</div>
                          <div style={{ color: "#64748b", fontSize: "12px" }}>{item.desc}</div>
                        </div>
                      </div>
                      <button
                        onClick={() => buy(item)}
                        style={{
                          padding: "6px 16px", borderRadius: "6px", fontSize: "13px", fontWeight: "600", whiteSpace: "nowrap",
                          background: affordable ? "#047857" : "#4b5563",
                          border: "1px solid " + (affordable ? "#10b981" : "#6b7280"),
                          color: affordable ? "#a7f3d0" : "#9ca3af",
                          cursor: "pointer",
                          opacity: affordable ? 1 : 0.6,
                        }}
                      >
                        {item.price} Gold
                      </button>
                    </div>
                  );
                })}
              </div>

              {/* Inventory */}
              {inventory.length === 0 ? (
                <div style={{
                  padding: "20px", textAlign: "center", color: "#475569", fontSize: "13px",
                  background: "rgba(30, 41, 59, 0.4)", borderRadius: "8px", border: "1px dashed #334155",
                }}>
                  Inventory is empty
                </div>
              ) : (
                <div style={{ display: "grid", gridTemplateColumns: "repeat(auto-fill, minmax(80px, 1fr))", gap: "8px" }}>
                  {inventory.map((name, idx) => {
                    const def = shopItems.find((s) => s.name === String(name));
                    return (
                      <div key={idx} style={{
                        display: "flex", flexDirection: "column", alignItems: "center", gap: "4px",
                        padding: "10px 6px", background: "rgba(30, 41, 59, 0.8)",
                        borderRadius: "8px", border: "1px solid #475569",
                      }}>
                        <span style={{ fontSize: "24px" }}>{def ? def.icon : "📦"}</span>
                        <span style={{ color: "#cbd5e1", fontSize: "11px", textAlign: "center" }}>{String(name)}</span>
                      </div>
                    );
                  })}
                </div>
              )}
            </div>
          )}
        </div>
      );
    }} />
  );
}
```

**代码做了什么：**

- `api.executeActionAndWait("buy", { item, price })` 让购买行为生效，传的值和玩家界面按钮传的一样，然后等它跑完
- `result.firedIds` 列出跑过的行为。空的就说明条件没过：行为已经弹过「金币不够」的提示，什么都不会加
- `api.setVariable("inventory", [...current, item.name])` 把东西加进列表
- 买不起的商品显示成灰色，但照样能点，这样玩家能看到提示
- `repeat(auto-fill, minmax(80px, 1fr))` 让每行尽量多放几个背包格子，放多少看宽度

用这个做法的话，把行为里 **告诉 AI** 那句改成 `The player just bought {param.item} for {param.price} gold.`，免得 AI 也往背包里加一次。

**面板 → 界面代码** 里文件旁边的状态应该是绿色的 **正常**。

::: tip 不想自己写代码？
点画布右上角的 **创作助手**，说说你想要什么样的商店。
:::

---

## 再进一步

### 更多商品

再加一个按钮（或者在 `shopItems` 里加一行），写上它的名字和价格。行为不用动。

### 卖东西

再建一条行为「出售」，按钮 `sell`，Gold 加上 `{param.price}`，**告诉 AI** 写 `The player sold {param.item}. Remove it from their Inventory.`

### 赚金币

- **每 N 回合**（比如 `3`）：**修改变量** Gold 加上 `10`
- **AI 说出关键词** `you win the fight, the reward is yours`：Gold 加上 `25`（任意一个关键词出现就触发）
- AI 自己来，靠 Gold 的行为规则，给剧情里的战利品和奖励
- [任务追踪](./quest-tracker.md)里的任务奖励

### 会讨价还价的店主

想要一个能用角色口吻回话、偶尔打个折的商人，就加一个界面型的 [AI](/zh/creator/ais)，**回答格式** 里加一个价格字段，再用按钮的 **叫一个 AI** 来调用它。

---

## 速查

| 你想要 | 怎么做 |
|---------------|-------------|
| 记金币 | 一个最小值为 `0` 的数值变量 |
| 整个商店只用一条行为 | **玩家点了某个按钮**，值用 `{param.item}` / `{param.price}` 来读 |
| 把商品和价格传进去 | 按钮步骤 **让一条行为生效** → **＋ 传一个值进去** |
| 检查玩家买不买得起 | **如果……** Gold **≥** `{param.price}` |
| 买不起时告诉他 | **条件不满足时告诉玩家** |
| 确认买到了 | **显示通知**，样式选 **成功** |
| 让东西进背包 | **告诉 AI** 加背包模板、每件商品一个数值变量、一个 **代码** 效果，或者界面代码 |
| 买不起的显示成灰色 | 按钮做两个版本，用 **什么时候显示** |
| 从代码里跑同一条行为 | `api.executeActionAndWait("buy", { item, price })` |

---

::: tip 这是食谱 #3
按钮把值传给一条行为，这个做法适合一切有价格或者从菜单里选的东西：打造、训练、雇人。见[行为 · 按钮传值](/zh/creator/automation#按钮传值)。
:::

</div>
