Repeating an Event

Twine Version: 2.3.5
Story Format: Harlowe 3.1.0

So I have a basic script I want to repeat multiple times, but I’m not sure how to do that.

Basically, I have an event that causes the variable $food to take on a randomized value, which I’ve set up as follows:

(set: $food to (random: 1,3))
(if: $food is 1)[(set: $food to “Doritos”)]
(if: $food is 2)[(set: $food to “Cheetos”)]
(if: $food is 3)[(set: $food to “Popcorn”)]

But as I’m not a programmer, I’ve no idea how to add the decided $food string to the $foodstorage array, nor how to have it so that the above script will repeat so that it adds multiple randomized food items to the Food Storage array.

Any help is appreciated!

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
1 Like