Please use the Preformatted Text option when including code examples in your comments, it makes the example easier to copy-n-paste and stops the forum’s software converting valid Standard quotes into invalid Typographical (curly) quotes.
warning: The following code examples have not been tested, they may contain syntax errors.
Because your $food
variable can only be one of a limited number of values at any time those values are known in programming as being ‘mutually exclusive’. So in the situation where you want to determine which of those values the variable equals you would use the (else-if:)
macro for the 2nd and later checks…
(set: $food to (random: 1,3))
(if: $food is 1)[(set: $food to "Doritos")]
(else-if: $food is 2)[(set: $food to "Cheetos")]
(else-if: $food is 3)[(set: $food to "Popcorn")]
The following example assumes that the $foodstorage
variable has previously being initialised to an empty array like so (set: $foodstorage to (a:))
, which was likely done in your project’s startup tagged Passage.
(set: $food to (random: 1,3))
(if: $food is 1)[(set: $foodstorage to it + (a: "Doritos"))]
(else-if: $food is 2)[(set: $foodstorage to it + (a: "Cheetos"))]
(else-if: $food is 3)[(set: $foodstorage to it + (a: "Popcorn"))]
note: If the above $food
variable is only needed within the context of the ‘current’ Passage then you could change it from a Story Variable $food
to being a Temporary Variable _food
instead.
One method you can use to execute a block of code from multiple places, is to place that code within a ‘child’ Passage and using the (display:) macro to include the contents of that ‘child’ Passage within the ‘current’ Passage.
eg. If you place code like the following in a ‘child’ Passage named Add Random Item to Storage
{
(set: _food to (random: 1,3))
(if: _food is 1)[(set: $foodstorage to it + (a: "Doritos"))]
(else-if: _food is 2)[(set: $foodstorage to it + (a: "Cheetos"))]
(else-if: _food is 3)[(set: $foodstorage to it + (a: "Popcorn"))]
}
…then you can ‘execute’ the contents of that ‘child’ Passage within the ‘current’ Passage like so…
Blah blah...
(display: "Add Random Item to Storage")
More blah blah