> For the complete documentation index, see [llms.txt](https://raster.gitbook.io/zh-hans_choicescript-guide/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://raster.gitbook.io/zh-hans_choicescript-guide/she-qu-zhi-nan/liu-lan/ming-ling/choice.md).

# \*choice

## 选项

`*choice` 命令是 ChoiceScript 的基本命令，它定义了玩家与故事互动时所选择的选项。

## 用法

普通（非条件性）选项应在前面加上井号 `#` 符号，如下例所示：

```
你更喜欢哪个？
*choice
  #一匹马。
    你选择了一匹马！
    *finish
  #一头驴。
    你选择了一头驴！
    *finish
  #一匹角马。
    你选择了一匹角马！
    *finish
  #以上都不喜欢。
    你不喜欢它们中的任何一个！
    *finish
```

单个 `*choice` 语句中包含的 `#选项` 数量没有真正的限制，但对于典型的 ChoiceScript 游戏来说，2-4 个选项最为常见。

任何 `#选项` 若要被视作有效，最终都必须以 \[\[Finish|\*finish]]、\[\[Goto|\*goto]]、\[\[Goto\_scene|\*goto\_scene]] 或 \[\[Ending|\*ending]] 命令结束，并且必须在下一条 `#选项` 之前完成。然而，这个过程是立即发生（如上面的简单示例所示），还是在该特定 `#选项` 的扩展脚本中稍后发生，都无关紧要。（为了不必使用这些结束命令，必须使用另一个命令：\[\[Fake choice|\*fake\_choice]]。）

## 嵌套选项

你也可以使用嵌套选择，让其中一个 `*choice` 的选项导向另一个 `*choice`，如下例所示：

```
你更喜欢哪个？
*choice
  #一匹马。
    你想要哪种颜色的马？
    *choice
      #黄色。
        你的马现在是黄色！
        *finish
      #绿色。
        你的马现在是绿色！
        *finish
      #红色。
        你的马现在是红色！
        *finish
  #A donkey.
    Which color do you want your donkey to have?
    *choice
      #Yellow.
        Your donkey is now yellow!
        *finish
      #Green.
        Your donkey is now green!
        *finish
      #Red.
        Your donkey is now red!
        *finish
  #A pony.
    Which color do you want your pony to have?
    *choice
      #Yellow.
        Your pony is now yellow!
        *finish
      #Green.
        Your pony is now green!
        *finish
      #Red.
        Your pony is now red!
        *finish
  #None of these.
    You don't like any of these!
```

请注意，即使存在嵌套选择，在原始 `*choice` 的下一个 `#选项` （即 "Donkey" 或 "Pony" 之前）之前，始终要有一个 `*finish`、`*goto`、`*goto_scene` 或 `*ending` 命令。

如果您是 ChoiceScript 的新手，与其使用嵌套选择（因为很难精确控制缩进），不如更多地使用 \[\[Goto|\*goto]] 和 \[\[Label|\*label]] 命令来简化操作，如下例所示：

```
你更喜欢什么？
*choice
  #一匹马。
    *goto HorseColor
  #A donkey.
    *goto DonkeyColor
  #A pony.
    *goto PonyColor
  #None of these.
    You don't like any of these!
    *finish

*label HorseColor
你想要哪种颜色的马？
*choice
  #黄色。
    你的马现在是黄色的了！
    *finish
  #Green.
    Your horse is now green!
    *finish
  #Red.
    Your horse is now red!
    *finish

*label DonkeyColor
Which color do you want your donkey to have?
*choice
  #Yellow.
    Your donkey is now yellow!
    *finish
  #Green.
    Your donkey is now green!
    *finish
  #Red.
    Your donkey is now red!
    *finish

*label PonyColor
Which color do you want your pony to have?
*choice
  #Yellow.
    Your pony is now yellow!
    *finish
  #Green.
    Your pony is now green!
    *finish
  #Red.
    Your pony is now red!
    *finish

*finish
```

在上面的例子中，原始的 `*choice` 语句使用 `*goto` 命令直接跳转到下方新的 `*label` 部分，并通过标签名（例如“HorseColor”）进行引用。

### 多重 `*choice`

ChoiceScript 有一个鲜为人知的功能，即可以在单个页面上为玩家提供多个选择，每个选择包含两个或更多选项。这解释起来绝非易事，因此，结合 ChoiceScript 集成开发环境，你只需点击下面的“结果”按钮，即可查看这段代码在游戏中实际呈现的效果。

不过，在您自己的游戏中采用这种多项选择时，需要注意两个主要限制：

1. `*choice` 行上的每个条目必须仅为单个单词，请注意这些单词会在游戏中显示。（王洛木：这个单词仅支持字母和数字的组合）
2. 所有第二个及后续的 `#选项` 必须完全相同（在上面的例子中，分别是 'Sash' 和 'Emblem' 组的名称）。只有第一级的 `#选项` 文本在措辞上可以有所不同（上面例子中的 'Order' 类型）。

## 进阶 `*choice`

虽然普通 `#选项` 在大多数情况下已经足够，但 `*choice` 命令是所有 ChoiceScript 命令中最灵活的命令之一，它允许使用 \[\[If|\*if]]、\[\[Selectable\_if|\*selectable\_if]] 和 \[\[Hide\_reuse|\*hide\_reuse]]，\[\[Allow\_reuse|\*allow\_reuse]] 和 \[\[Disable\_reuse|\*disable\_reuse]] 等条件选项。

以下示例应能让你更好地理解 `*choice` 命令和复杂条件 `#选项` 的真正灵活性，特别是展示了如何使用 `*if` 为 `*selectable_if`、`*hide_reuse` 和普通 `#选项` 等分配前提条件。同时也要注意 `*selectable_if` 的多功能性。

我们在此示例中包含了 `*temp` 命令和随后的 `*label`，以便你愿意的话，可以直接复制整个内容并粘贴到场景 .txt 文件中，以在游戏中显示。然后相应地更改默认值，以查看每种情况下对显示选项的不同影响，并在 true 和 false 之间调整各种条件。

```
*comment 暗影行者
*temp shadowwalk 31

*comment 红外视觉
*temp infravision 31

*comment 敏捷性
*temp agility 51

*comment 手持火炬
*temp lit_torch_held false

*label throne_room
*choice
  #穿过王座厅，从远端的门道离开。
    *if shadowwalk > 20
      你踮着脚尖穿过王座厅，走进一条潮湿的通道。
      *goto damp_passage
    *else
      你笨拙地绊倒在龙尾上，随即被吞食殆尽。
      *ending
  *if (shadowwalk > 30) and ((infravision > 30) or (lit_torch_held = true))
    *hide_reuse #搜查王座厅的每个角落。
      你搜遍每个角落……徒劳无功。
      *goto throne_room
  *if (shadowwalk < 31) and ((infravision < 31) and (lit_torch_held = false))
    #搜查王座大厅，避开沉睡的巨龙。
      你被龙尾绊倒。她醒来一口把你吞下肚……嗝！
      *ending
  *selectable_if ((agility > 50) and (shadowwalk < 31)) #跃向阳台。
    你来到阳台，抓起宝藏，从敞开的窗户离开。
    *goto castle_grounds
  *if shadowwalk > 30
    *selectable_if (agility < 51) #悄然跃上摇摇欲坠的阳台。
      你未能抵达阳台，却轻盈落地，未惊醒巨龙。
      *goto throne_room
    *selectable_if (agility > 50) #用吊灯到达阳台。
      你来到阳台，抓起宝藏，从敞开的窗户离开。
      *goto castle_grounds
  *hide_reuse #在沉睡的巨龙周围跳支吉格舞……轻点！
    你（有些无谓地）在龙周围跳着无声的吉格舞。Zzzzzzz.
    *goto throne_room
  #砍下沉睡巨龙的头颅。
    好样的……你不过是惹恼了她罢了。焦炭冒险家！真美味！
    *ending

*label damp_passage
你沿着潮湿的通道往下走。
*ending
*label castle_grounds
你踏入了城堡的庭院。
*ending
```

## 注意事项

### 结合 `*selectable_if`、`*hide_reuse` 和 `*disable_reuse`

请注意，`*selectable_if` 可以与 `*hide_reuse`、`*disable_reuse` 或 `*allow_reuse` 结合使用，前提是复用命令位于其之前，例如：

```
*comment 设定金钱
*temp money 5

*label Home

你有 ￥${money} 可以花。你想买点什么？

*choice

    *hide_reuse *selectable_if (money >= 2) #一根冰棒，售价 2 元。

        好选择，冰棍真好吃！

        *goto Home

    *disable_reuse *selectable_if (money >= 4) #一块巧克力华夫饼，售价 4 元。

         价格略高，但物有所值。

         *goto Home

    #购物完毕。

        你离开糖果店。

        *finish
```

在这里，如果玩家没有足够的钱（由 `*selectable_if` 命令确保），购买冰棒和华夫饼的选项将可见但被禁用（"变灰"）。此外，如果之前已经选择过冰棒选项，它将被隐藏（得益于 `*hide_reuse`），而华夫饼选项在之前被选择过后会变灰（得益于 `*disable_reuse`）。否则，这些选项将是可选择的。

### 当“not”不等于“不是”的时候——一个 ChoiceScript 的怪癖

以下这段使用布尔条件的代码是完全有效的：

```
*choice
  *if (变量名) #这个选项 1
    *goto next
```

你可能想知道为什么在本维基中（如上面的长示例所示），我们倾向于使用以下格式：

```
*choice
  *if (变量名)
    #这个选项 1
      *goto next
```

这种格式同样完全有效。采用这种奇特表达方式的原因是，当需要检查某个布尔值是否为**假**（即不为真）时，这种格式完全有效且能按预期正常工作：

```
*choice
  *if not (变量名)
    #这个选项 1
      *goto next
```

而此格式：

```
*choice
  *if not (var) #This option 1
    *goto next
```

实际上会完&#x5168;***忽略***&#x90A3;个特定的 “**not**” ，从而将代码视为检查**真**值，而非**假**值。

因此，我们建议始终将 `*if` 条件放在单独的一行上，无论是检查真值还是假值，以避免因使用不同格式而混淆问题。

解决此问题的另一种方法是添加另一组括号来包裹 “not”，这样选项就可以放在同一行，例如：

```
*choice
  *if (not (变量名)) #这个选项 1
    *goto next
```

同样地，请注意：

```
*choice
  *selectable_if not (变量名)#This option 1
     *goto next
```

上面这个示例也会简单地忽略那个"not"，而将该行视为检查真值，而非假值。此外：

```
*choice
  *selectable_if (变量名)
    #这个选项 1
      *goto next
```

实际上对于 `*selectable_if` 是无效的（会导致“解析错误”，尽管对于普通的 `*if` 来说完全没问题）。

有两种方法可以通过 `*selectable_if` 来检查布尔值为假的情况，第一种是：

```
*choice
  *selectable_if (变量名 = false)#这个选项 1
     *goto next
```

第二种是：

```
*choice
  *selectable_if (not (变量名))#这个选项 1
     *goto next
```
