枚举

在 Yarn Spinner 中,枚举允许您创建变量,其值仅限于预定义的可能性列表。

[!信息] 当您的变量需要具有比简单的更广泛的可能值时,枚举(“枚举”的缩写)非常有用 true 或者 false,但需要比 number 或者 string.

在 Try Yarn Spinner 中运行此示例

要定义枚举,您必须提供名称及其一些情况。这是一个名为的新枚举 Food 与案例 Apple, Orange, 和 Pear:

<<enum Food>>
  <<case Apple>>
  <<case Orange>>
  <<case Pear>>
<<endenum>>

创建枚举后,您可以像使用任何其他变量一样使用它:

// Declare a new variable with the default value Food.Apple
<<declare $favouriteFood = Food.Apple>>
 
// You can set $favouriteFood to the 'apple', 'orange' or 'pear'
// cases, but nothing else!
<<set $favouriteFood to Food.Orange>>
 
// You can use enums in if statements, like any other type of value:
<<if $favouriteFood == Food.Apple>>
  I love apples!
<<endif>>
 
// You can even skip the name of the enum if Yarn Spinner can 
// figure it out from context!
<<set $favouriteFood = .Pear>>