Knots
了解 Pixi’VN 中 knot 和 label 的工作原理、如何通过 ink 脚本或 JavaScript 执行它们,以及原生 ink 与 Pixi’VN 集成之间的区别。
如官方文档所述,故事由多个相互连接的部分组成,在 ink 术语中,这些部分称为 knots。在 ink 中,knot 的开头通过在 knot 名称左侧添加至少两个等号来标识,也可以选择在右侧添加等号。
=== london ===没有,它们是同一个概念。在 Pixi’VN 中,当你使用 ink 创建 knot 时,系统会在内部创建一个 label 并将其保存到注册表中。
执行 knot
你可以通过以下方法执行 knot:
你可以使用 Divert 运算符执行 knot,其语法为 -> start。这对应 Pixi’VN 库中的 jump 功能。
-> after例如:
=== start ===
Start
-> after
=== after ===
After
End
-> DONE你可以使用 Tunnels 运算符执行 knot,其语法为 -> start ->。这对应 Pixi’VN 库中的 call 功能。
-> guard_dialogue ->或者,使用 Threads 语法,在 \<- 符号后面写上 knot 的名称:
<- guard_dialogueThreads
与原生 ink 不同,ink + Pixi’VN 集成不支持“Threads”,因此 \<- 符号表示调用 knot,而不是使用“Thread”运算符。
为简化操作,在 ink + Pixi’VN 集成中,->-> 和 -> DONE 都能正确结束 label,因此两者是等效的。
例如:
VAR reputation = 0
-> start
=== start ===
You are in the square.
* [Talk to the guard]
-> guard_dialogue ->
You return to the square.
-> start
You don't see this line because you are sent back to the city before.
* {reputation >= 1} [Ask for a favor]
The guard helps you.
-> END
=== guard_dialogue ===
The guard looks at you.
* [Greet]
"Hello citizen."
~ reputation += 1
->->
* [Insult]
"Hey you!"
~ reputation -= 1
->->其他功能
参数是一种特别有用的临时变量。任何 knot 或 stitch 都可以接收一个值作为参数,该值将在对应 knot 内被视为局部临时变量:
=== start ===
* [Accuse Hastings]
-> accuse("Hastings")
* [Accuse Mrs Black]
-> accuse("Claudia")
* [Accuse myself]
-> accuse("myself")
=== accuse(who) ===
"I accuse {who}!" Poirot declared.
"Really?" Japp replied. "{who == "myself":You did it?|{who}?}"
"And why not?" Poirot shot back.参数是在不同 knot 或 stitch 之间传递临时值的正确方式,因为普通的 temp 变量无法在声明它们的 stitch 之外访问。
临时变量(包括参数)可以安全地用于递归 knots 中:
-> add_one_to_one_hundred(0, 1)
=== add_one_to_one_hundred(total, x) ===
~ total = total + x
{ x == 100:
-> finished(total)
- else:
-> add_one_to_one_hundred(total, x + 1)
}
=== finished(total) ===
"The result is {total}!" you announce.
-> ENDknot 的地址也可以作为参数传递。使用 -> 表示某个参数是跳转目标:
=== sleeping_in_hut ===
You lie down and close your eyes.
-> generic_sleep(-> waking_in_the_hut)
=== generic_sleep(-> waking) ===
You sleep perchance to dream etc. etc.
-> waking
=== waking_in_the_hut ===
You get back to your feet, ready to continue your journey.默认情况下,跳转到另一个 knot 的操作会在下一个 step 执行,因此只有在流程继续后,才会显示跳转目标的内容:
=== start ===
It is, first and foremost...
-> add2
=== add2 ===
...a story about the twelve labors of Hercules.
-> DONE如果将跳转语句直接放在文本之后,中间不换行,那么目标 knot 会与前面的文本在同一个 step 中执行,因此两者会一起显示:
=== start ===
It is, first and foremost...-> add2
=== add2 ===
...a story about the twelve labors of Hercules.
-> DONE