# 重要的 ChoiceScript 命令与技巧

> <https://www.choiceofgames.com/make-your-own-games/important-choicescript-commands-and-techniques/>

ChoiceScript 编程语言重要技巧指南。如果您对本文件有任何疑问，请在 [ChoiceScript 论坛](http://www.choiceofgames.com/forum/categories/choicescript-help)发帖。

## 初学者请不要直接从这里开始学习！

在阅读这份技巧之前，请确保您已经阅读过了我们[基础的 ChoiceScript 介绍](/zh-hans_choicescript-guide/guan-fang-zhi-nan/choicescript-jian-jie.md)页面。

## 其他重要命令

### \*title

此命令用于设置游戏的标题。它必须与 `*create` 和 `*scene_list` 一起放在 `startup.txt` 文件的顶部。

```
*title 龙之抉择
```

**谷歌 Play 商店对 Android 游戏标题设置了 30 个字符的限制。**&#x867D;然 ChoiceScript 允许任意长度的标题，但如果您计划通过我们发布游戏，您需要为游戏起一个不超过 30 个字符（包括空格）的名称。

### \*author

此命令用于设置游戏的作者署名行。它必须与 `*create`、`*title` 和 `*scene_list` 一起，在 `startup.txt` 文件顶部使用。

```
*author Dan Fabulich 和 Adam Strong-Morse
```

### \*ifid

此命令在您的游戏中嵌入一个 IFID（互动小说标识符）。它就像是图书的 ISBN 号。您需要随机生成一个 IFID。幸运的是，您可以直接在这里生成一个：

（王洛木：这是一个官方的功能，我没有这个功能的代码所以无法构建，您必须访问这一页才能手动获取一个 IFID。）

<https://www.choiceofgames.com/make-your-own-games/important-choicescript-commands-and-techniques/>&#x20;

将该行复制并粘贴到你的 `startup.txt` 文件顶部，靠近 \`\*title 的位置。（添加 IFID 是必需的，以便在[导出为 HTML](/zh-hans_choicescript-guide/guan-fang-zhi-nan/dao-chu-bing-fa-bu-nin-de-you-xi.md) 时支持保存游戏进度。）

（王洛木：或者你在导出 HTML 的时候也会在编译页面找到一个。）

### \*temp

此命令与 `*create` 非常相似，但它创建的是一个仅存在于当前文件中的临时变量。与 `*create` 不同，您可以在任何地方使用 `*temp`，并且无需为 `*temp` 变量指定初始值。

```
*temp conversation
*set conversation "a little less"
*temp action "a little more"
```

注意：`*temp` 变量会在你执行 `*finish` 或 `*goto_scene` 时被清除。（如果你想跳转到另一个场景文件而不清除 `*temp` 变量，你可能需要使用 `*gosub_scene`。详见下文。）

### \*comment

此命令不执行任何操作；`*comment` 后的任何文本都会被忽略。它有助于在文本中添加仅作者应阅读的备注。

### \*page\_break

插入一个没有单选按钮的“Next”按钮。游戏将在后续页面继续。

```
You turn the corner slowly.  Blood rushes through your ears.  As you open the door...
*page_break
... the masked murderer attacks!
```

默认情况下，`*finish` 按钮显示 "Next Chapter"，`*page_break` 按钮显示 "Next"。您可以让按钮显示其他文字：

```
*page_break On with the show!
*finish The show is over!
```

我们建议按钮标题不超过五个字；超过这个字数在屏幕上（尤其是在移动设备屏幕上）会显得非常奇怪。

### \*input\_text

提供一个文本框，供用户指定变量的值，例如用户的姓名。

```
  Please enter your name.
  *input_text name
 
  Your name is ${name}, is that right?
```

ChoiceScript 不允许玩家将输入留空。如果您想允许留空，可以使用：

```
  *input_text name allow_blank
```

### \*fake\_choice

此便捷命令的行为与 `*choice` 完全相同，但在选项正文中不允许使用任何命令；因此无需使用`*goto` 或 `*finish`。

```
  What color do you prefer?
 
  *fake_choice
    #Red
      Red is the color of roses.
    #Blue
      Blue is the color of the sea.
    #Green
      Green is the color of spring.
 
  What an excellent choice!  And what flavor of ice cream would you like?
 
  *fake_choice
    #Vanilla
    #Chocolate
    #Strawberry
 
  Mmm, delicious!
  *finish
```

### \*image

此命令用于插入图像。将图像放置在“mygame”文件夹中，并输入图像文件的名称，如下所示：

```
  *image beauty.jpg
```

如果愿意，您可以在图像名称后指定对齐方式（“left”、“right”或“none”），如下所示：

```
  *image beauty.jpg left
```

默认情况下，图像会单独一行居中显示，但如果您将图像左对齐或右对齐，文本将环绕图像。（用 CSS 术语来说，图像将“浮动”到左侧或右侧。）如果您希望图像单独一行左对齐显示，请使用“none”。

如果你的图像对理解故事至关重要（例如，图像上含有玩家需要阅读的文字），那么你应该在图像对齐方式后输入替代描述。这段文字将供无法看到图像的用户（例如视力受损的用户）访问。

```
  *image beauty.jpg left Beauty
```

### \*save\_checkpoint

您可以使用 `*save_checkpoint` 命令来保存一个检查点。之后，您可以为玩家提供使用 `*restore_checkpoint` 命令恢复到先前检查点的选项，如下所示：

```
  *choice
    #Continue to the next chapter.
      *finish
    #Restore to the previous checkpoint.
      *restore_checkpoint
```

当你执行 `*restore_checkpoint` 时，玩家将“穿越回”调用 `*save_checkpoint` 的那一刻。

### 百分比属性和公平数学

在属性屏幕上显示的属性通常范围在 0 到 100 之间。要确保玩家永远不会意外获得超过 100 点的领导力可能很困难。为了帮助解决这个问题，我们提供了特殊的命令，可以将属性保持在 0 到 100 之间。

```
*set leadership %+20
*set strength %-10
```

“%+”和“%-”运算符被称为“公平数学”运算符。其设计理念是，当你的领导力分数越高时，提升变得越困难，而降低则越容易。根据公平数学规则：

* 公平加法：`(x %+ y) = (x + (100-x)*(y/100))`
  * 高分难以提升：`(90 %+ 20) = (90 + 2) = 92`
  * 低分易于提升：`(10 %+ 20) = (10 + 18) = 28`
* 公平减法：`(x %- y) = (x - x*(y/100))`
  * 高分易于降低：`(90 %- 20) = (90 - 18) = 72`
  * 小数值难以降低：`(10 %- 20) = (10 - 2) = 8`
* 50 这个数值增加或减少的难度是相等的。
  * `(50 %+ 20) = (50 + 10) = 60`
  * `(50 %- 20) = (50 - 10) = 40`

**我们建议您始终并且仅使用公平数学来更新百分比。**&#x5982;果您在决定使用公平数学分配多少点数时遇到困难，以下是一些指导原则：

* 平均而言，公平数学的效果仅为简单加法的一半。因此，例如，`%+20` 通常只给玩家增加大约 10 点。
* 如果您想给予小幅提升，请设置为 `%+10`。如果您想要大幅提升，请设置为 `%+40`。
* 技能为 50% 的玩家具有平均水平。
* 60% 的技能属于良好；70% 则堪称优秀。
* 80% 的技能几乎不可能达到。

### \*if 语句的类型

除了 `*if leadership > 15`，你还可以用 `*if`*语句*做更多事情。以下是几个技巧：

* 相等与不等
  * 等于：`leadership = 40`（领导力等于四十吗？）
  * 不等于：`leadership != 40`（领导力不等于四十吗？）
  * 大于：`leadership > 40`（领导力大于四十吗？）
  * 小于：`leadership < 40`（领导力是否小于四十？）
  * 大于或等于：`leadership ≥ 50`（领导力是否大于或等于五十？）
  * 小于或等于：`leadership <=40`（领导力是否小于或等于四十？）
* 和、或、非（必须使用括号）
  * 和：`(leadership > 30) and (strength > 40)`
  * 或：`(leadership > 60) or (strength > 70)`
  * 非：`not(strength > 70)`
  * 复杂括号：`((leadership > 60) and (agility > 20)) or (strength > 80)`
* 比较文本：
  * `lover_name = "Jamie"`
  * `"2" = 2（这是真的！）`
* 将变量设为真或假：
  * `*set finished false`
  * `*set correct guess = "blue"`

### 使用  `@{}` 进行多重替换

多重替换与 `${}` 替换有些类似，但你需要提供一个变量和一个选项列表。该变量必须是数字或 `true` 或者 `false` (其中 `true`=1, `false`=2)。 `@{}`将插入与数字对应的选项。

```
There @{count is one thing|are two things|are three things} here.
You are on the @{is_evil Dark|Light} Side of the Force.
Behold the dragon@{plural s|}!
Behold the dragon@{(dragons > 1) s|}!
Behold the dragon@{(dragons = 1) |s}!
```

第一个词应为变量名，后跟一个空格，然后是多个由竖线 `|` 分隔的选项。允许存在空选项。你也可以通过将更复杂的表达式包裹在括号中来使用它们。

请注意，第一个选项的编号是 #1。如果你希望第一个选项的编号为零，请在括号内为变量加 1，像这样：

```
There @{(count+1) are no things|is one thing|are two things} here.
```

你可以在 `@{}` 中嵌套 `${}` 替换，但不能在另一个 `@{}` 内部嵌套 `@{}`。

### 条件选项

有时我们想向玩家表示某个选项此时对他们不可用。我们可以这样做：

```
  How will you handle this?
  *choice
    #Try to talk them out of it.
      They cannot be dissuaded.
      *finish
    #Force them to relent.
      They back down, for now.
      *finish
    *selectable_if (president) #Abuse my presidential powers to silence them
      This works; you will never hear from them again.
      *finish
```

如果你不是总统，你会看到 \`Abuse my presidential powers to silence them\`（滥用总统权力）的选项，但它会以灰色显示；点击时不会高亮。这给玩家一个提示：如果他们重新玩游戏，通过之前做出不同的选择，或许能够选择那个选项。\
如果我们想完全隐藏选项，可以使用 `*if` 而非 `*selectable_if`，如下所示：

```
  How will you handle this?
  *choice
    #Try to talk them out of it.
      They cannot be dissuaded.
      *finish
    #Force them to relent.
      They back down, for now.
      *finish
    *if (president) #Abuse my presidential powers to silence them
      This works; you will never hear from them again.
      *finish
```

在这个例子中，如果你不是总统，你甚至不会看到滥用总统权力的选项。

有时我们只是想阻止某些选项被重复使用。我们可以通过 `*hide_reuse` 和 `*disable_reuse` 修饰符来实现，如下所示：

```
  *label start
  *choice
    *hide_reuse #One.
      The loneliest number that you'll ever do.
      *goto start
    *disable_reuse #Two.
      Two can be as bad as one.
      *goto start
    #I can't decide!
      Well, think it over.
      *goto start
    #Done.
      OK!
      *finish
```

如果玩家选择 `#One`，该选项将被隐藏；如果玩家选择 `#Two`，该选项将变为不可选（呈灰色显示）。

（请注意，如果使用 `*restore_checkpoint` 回退时间，将会重新启用玩家在保存检查点后可能使用过的任何 `*disable_reuse` 或 `*hide_reuse` 选项。）

### 使用 `*gosub` 和 `*gosub_scene` 的子程序

除了使用 `*goto` 命令外，您还可以使用 `*gosub` 命令跳转至标签，然后使用 `*return` 命令跳回调用 `*gosub` 的那一行。

```
  *choice
    #快乐。
      你很快乐！
      *gosub saying
      希望你能快乐很长一段时间！
      *finish
    #Sad.
      You're sad.
      *gosub saying
      Maybe you'll be happier soon!
      *finish
  *label saying
  这一切也终将过去。
  *return
```

如果选择“快乐。”，游戏将显示：

> 你很快乐！这一切也终将过去。希望你能快乐很长一段时间！

对于需要在多个地方复制粘贴的代码片段来说，这非常有用。

“子程序 （Subroutines）”是在程序运行过程中调用的微型子程序。`*gosub` 之所以如此命名，是因为它能激活一个子程序。通过在使用 `*return` 命令前两次或多次使用 `*gosub`，可以实现子程序的嵌套调用。

```
  开始一，
  *gosub two
  结束一。
  *finish
 
  *label two
  开始二，
  *gosub three
  结束二。
  *return
 
  *label three
  三。
  *return
```

该代码将显示：

> 开始一，开始二，三。结束二。结束一。

你也可以使用 `*gosub_scene` 来访问另一个场景文件，然后使用 `*return` 返回到离开的位置。与 `*goto_scene` 类似，你也可以使用 `*gosub_scene` 访问另一个文件中的特定标签，并在完成后使用 `*return` 返回。

```
  *gosub_scene invitations cordial
```

**警告：**&#x4E00;般来说，你的 ChoiceScript 越简单越好。滥用 `*gosub` 和 `*gosub_scene` 可能会创建极其复杂的程序。这通常不是个好主意；复杂的游戏并不一定比简单的游戏更有趣，但复杂的游戏制作起来要困难得多。如果你认为需要很多子程序，请考虑一下：如果你的游戏更简单，是否会更好。

### 有问题吗？

若对本文件有疑问，请在 [ChoiceScript 论坛](http://www.choiceofgames.com/forum/categories/choicescript-help)发帖咨询。


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://raster.gitbook.io/zh-hans_choicescript-guide/guan-fang-zhi-nan/zhong-yao-de-choicescript-ming-ling-yu-ji-qiao.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
