Enums
Enums let you create variables whose value is constrained to a pre-defined list of possibilities.
An enum (short for ‘enumeration’) is useful when you have a variable that needs to have a wider range of possible values than true or false, but needs to be more specific than a number or string.
To define an enum you must provide a name, and some cases for it. This is an enum called Food with the cases Apple, Orange, and Pear:
<<enum Food>>
<<case Apple>>
<<case Orange>>
<<case Pear>>
<<endenum>>Declaring the Food enum.
flowchart LR
food_var["$favouriteFood"]
subgraph food_enum ["enum Food"]
direction TB
food_apple["Food.Apple<br>the default"]
food_orange["Food.Orange"]
food_pear["Food.Pear"]
end
food_var ---|"always holds one of"| food_enumOnce you’ve created an enum, you can use it like any other variable:
title: Start
---
<<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>>
===Variables
Using a variable that holds a
Food value.Putting it together
In this longer example, the vendor’s options set an enum variable, $weapon, and an if statement checks which case it holds:
title: Start
---
<<enum Weapon>>
<<case None>>
<<case Sword>>
<<case Crossbow>>
<<case PointyStick>>
<<endenum>>
<<declare $weapon = Weapon.None>>
Vendor: Do you want to buy a weapon?
-> Yes! I'll take the Sword.
Vendor: OK! Here's your sword.
<<set $weapon = Weapon.Sword>>
-> Yes, I want a crossbow.
Vendor: Here's a crossbow!
<<set $weapon = Weapon.Crossbow>>
-> Yes, but just a pointy stick, please.
Vendor: One stick, coming right up.
<<set $weapon = Weapon.PointyStick>>
<<if $weapon == Weapon.Sword>>
Townsperson: Ooh, a big fancy adventurer with their big fancy sword!
<<elseif $weapon == Weapon.Crossbow>>
Townsperson: Don't bring that crossbow near me, adventurer!
<<elseif $weapon == Weapon.PointyStick>>
Townsperson: An adventurer with a stick! Hahah!
<<else>>
Townsperson: An adventurer with no weapon?!
<<endif>>
===Variables
Choosing a weapon, stored in an enum variable.
flowchart TD
weapon_vendor["Vendor: Do you want to buy a weapon?"]
weapon_sword["#lt;#lt;set $weapon = Weapon.Sword#gt;#gt;"]
weapon_crossbow["#lt;#lt;set $weapon = Weapon.Crossbow#gt;#gt;"]
weapon_stick["#lt;#lt;set $weapon = Weapon.PointyStick#gt;#gt;"]
weapon_check{"Which case does $weapon hold?"}
weapon_fancy["Townsperson: Ooh, a big fancy adventurer with their big fancy sword!"]
weapon_near["Townsperson: Don't bring that crossbow near me, adventurer!"]
weapon_haha["Townsperson: An adventurer with a stick! Hahah!"]
weapon_none["Townsperson: An adventurer with no weapon?!"]
weapon_vendor -->|"-#gt; Yes! I'll take the Sword."| weapon_sword
weapon_vendor -->|"-#gt; Yes, I want a crossbow."| weapon_crossbow
weapon_vendor -->|"-#gt; Yes, but just a pointy stick, please."| weapon_stick
weapon_sword --> weapon_check
weapon_crossbow --> weapon_check
weapon_stick --> weapon_check
weapon_check -->|"Weapon.Sword"| weapon_fancy
weapon_check -->|"Weapon.Crossbow"| weapon_near
weapon_check -->|"Weapon.PointyStick"| weapon_haha
weapon_check -->|"else: Weapon.None"| weapon_none