There are many different ways to approach procedural generation of content in Twine. This example covers using //macros// as both a storage and method of generating content.
(This example uses the Harlowe 2.1.0 story format.)
[[Using Either]]The //``(either:)``// macro randomly chooses one of the options supplied to it.
<blockquote>
It has been (either: "2", "3", "4", "5", "6", "7", "8") years since I last saw (either: "him", "her", "them") arrive (either: "on a boat", "from over the mountain", "traveling down the long road").
</blockquote>
[[Using Range]]The result of macros can also be saved.
(set: $years to (either: ...(range:2,8)) )
(set: $pronoun to (either: "him", "her", "them") )
(set: $arrival to (either: "on a boat", "from over the mountain", "traveling down the long road") )
<blockquote>
It has been $years years since I last saw $pronoun arrive $arrival.
</blockquote>
[[Using loops]]By using the //``(for:)``// macro, loops can be used with saved values.
(set: $loops to (either: ...(range:2,8)) )
(for: each _i, ...(range:1,$loops))[BB]
(for: each _i, ...(range:1,$loops))[bb]
[[Using the Display Macro]]The //``(display:)``// macro is a way of including content from one passage in another. In Twine, it is a common way to create modular content, placing code in one passage and "calling" it from others. Like other macros, it can be used multiple times.
(display: "Create Bb")
(display: "Create Bb")
(display: "Create Bb")
(set: $loops to (either: ...(range:2,16)) )
(for: each _i, ...(range:1,$loops))[BB]
(for: each _i, ...(range:1,$loops))[bb]The //``(range:)``// macro creates an array of its values starting from the initial to the last one. The expansion operator ``...`` serves as a shorthand to quickly "spread out" values. Combining the two is a fast way to have a range of values that can be used by other macros like //``(either:)``// and //``(for:)``//.
<blockquote>
It has been (either: ...(range:2,8)) years since I last saw (either: "him", "her", "them") arrive (either: "on a boat", "from over the mountain", "traveling down the long road").
</blockquote>
[[Saving choices]]