流程控制
到目前为止,你已经学习了如何使用 lines-nodes-and-options.md、options.md、jumps.md、detour.md 和 logic-and-variables.md 来编写 Yarn Spinner 脚本。
由于 Yarn Spinner 本身是一门完整的编程语言,它也支持流程控制。
流程控制有多种形式,包括 if 语句 和 条件选项。
if 语句
变量除了存储信息之外,也可以用来控制哪些对话行会展示给玩家。要做到这一点,你可以使用 if 语句。
if 语句可以让你控制一段内容是否显示。
编写 if 语句时,需要提供一个会被检查的表达式;如果该表达式求值为 true,那么 <<if>> 和 <<endif>> 之间的全部内容都会执行。
例如,考虑下面这段 Yarn Spinner 脚本:
<<set $gold_amount to 5>>
Player: I'd like to buy a pie!
<<if $gold_amount < 10>>
Baker: Well, you can't afford one!
<<endif>>
这段脚本会:
- 将变量
$gold_amount设为 5; - 显示台词
Player: I'd like to buy a pie! - 使用 if 语句检查
$gold_amount是否小于10,在这个例子中条件成立,因此会显示台词Baker: Well, you can't afford one!
elseif 和 else
你可以在 if 语句中使用 elseif 和 else 来处理不同情况。
当 if 分支或此前的任意 elseif 分支都没有执行时,才会检查当前 elseif 语句中的表达式。
else 语句没有表达式;如果 if 和任意 elseif 都没有执行,就会执行 else。
例如,考虑下面这段 Yarn Spinner 脚本:
<<set $gold_amount to 5>>
Player: I'd like to buy a pie!
<<if $gold_amount < 10>>
Baker: Well, you can't afford one!
<<elseif $gold_amount < 15>>
Baker: You can almost afford one!
<<else>>
Baker: You can afford a pie!
<<endif>>这段脚本会:
- 将变量
$gold_amount设为 5; - 显示台词
Player: I'd like to buy a pie! - 使用 if 语句检查
$gold_amount是否小于10;如果成立,显示台词Baker: Well, you can't afford one!- 否则,使用
elseif检查$gold_amount是否小于15;如果成立,显示台词Baker: You can almost afford one! - 否则,使用
else作为兜底,显示台词Baker: You can afford a pie!
- 否则,使用
- 用
endif结束if语句块。
在一个 if 语句块中,你可以写任意数量的 elseif,并且可以使用任何 operators,例如:
<<set $gold_amount to 5>>
<<set $reputation to 10>>
<<if $gold_amount >= 5 and $reputation >= 8>>
// The player is both rich and popular
Merchant: You're rich enough and popular enough for me to serve you!
<<elseif $gold_amount >= 10 or $reputation >= 10>>
// The player is either rich enough or popular enough to serve
Merchant: I wouldn't normally, but I'll serve you!
<<else>>
// The player is dirt
Merchant: You're neither rich enough nor important enough for me to serve!
<<endif>>在这个例子中:
- 有两个变量:
$gold_amount用于追踪玩家货币数量,$reputation用于追踪玩家在城镇中的声望; if语句先检查$gold_amount是否大于等于5,并且声望是否大于等于8;如果两个条件都为真,就会显示台词Merchant: You're rich enough and popular enough for me to serve you!- 否则,
elseif检查$gold_amount是否大于等于10(非常有钱),或者$reputation是否大于等于10(很受尊重);如果任一条件为真,就会显示台词Merchant: I wouldn't normally, but I'll serve you!
- 否则,
- 如果以上两组条件都不成立,则
else会显示台词Merchant: You're neither rich enough nor important enough for me to serve!
INFO
if和elseif中使用的表达式必须返回布尔值(即 true 或 false)。例如,<<if 1>>不合法,但<<if 1 == 1>>合法。
条件选项
当你使用 -> 语法向玩家展示 options 时,可能希望让某些选项不可用。你可以为选项添加条件,让它成为条件选项。
例如,如果你有一个名为 $reputation 的变量来追踪玩家声望分数,你可能希望只有在 $reputation 足够高时,某些选项才可用。
给选项添加条件的方法,是在选项末尾增加一个 if 语句。形式如下:
Guard: You're not allowed in!
-> Sure I am! The boss knows me! <<if $reputation > 10>>
-> Please?
-> I'll come back later.当 Yarn Spinner 运行这组选项时,会检查 if 语句里的表达式。如果表达式是 false,该选项会被标记为不可用。
INFO
Yarn Spinner 会始终将选项组中的所有选项都交给游戏;至于被标记为不可用的选项该如何处理,由游戏决定。
例如,不可用选项可以显示给用户但不可选择,这样用户就能知道:如果条件不同,他们本可以说出这句话。
选项中的兜底内容
如果一个选项组中的所有选项都被标记为不可用,游戏可以告诉 Yarn Spinner:没有选中任何选项。发生这种情况时,Yarn Spinner 会跳过这个选项组,执行选项之后的下一段内容。
Guard: Who goes there?
// If the player is a thief, a royal visitor, or a merchant, then
// go run the appropriate conversation for that. The player might be
// some combination of the three, so let them choose.
-> A thief! <<if $player_is_thief>>
<<jump Guard_Thief_Conversation>>
-> A royal visitor! <<if $player_is_royal_visitor>>
<<jump Guard_RoyalVisitor_Conversation>>
-> A merchant! <<if $player_is_merchant>>
<<jump Guard_Merchant_Conversation>>
// But if the player is NONE of those, then none of the options would have
// been available. We'll fall through to here.
Player: I'm nobody!
<<jump Guard_Nobody_Conversation>>