Linking to a random passage from a set Twine 2.4.1 Harlowe 3.3.1

Hi! How can I link to a random passage? For example, I have 1, 2, 3, and 4 passages and I want 1 to randomly link to 2, 3, OR 4.

twine 2.4.1
harlowe 3.3.1

You can use the (either:) macro to randomly select an name from a list of Passage Names…

(set: _passage to (either: "Second", "Third", "Forth"))

…and the (link-goto:) macro to create a link that targets that randomly determined Passage Name…

(link-goto: "Continue", _passage)

The following demonstrates using both of the above together to display a link that targets a randomly determine Passage…

(set: _passage to (either: "Second", "Third", "Forth"))
(link-goto: "Continue", _passage)

And the following demonstrates how you can achieve the same outcome without the need for the (temporary) variable…

(link-goto: "Continue", (either: "Second", "Third", "Forth"))
1 Like

Greyelf’s answer give equal chances to each passage to be selected. If it’s not your intent, you might want to look at the (random:) macro rather the (either:) one.

For instance, the following code will give 60% chance to go to the 'Second" passage, and only 20% for each of the two others.

(set: _roll to (random:1,5))
(if: _roll <4)[[["Continue"| "Second"]]]
(else-if: _roll is 4)[[["Continue"| "Third"]]]
(else:)[[["Continue"| "Fourth"]]]
1 Like

I have update my previous comment to make it clear that the line of code that call the (set:) macro needs to be combined with the line of code that calls the (link-goto:) macro.

A macro is a means to assign a “name” to a “block of code” that you want to be able to execute multiple times within a project.

And the Harlowe Manual lists within the List of macros section of the documentation all the macros that have been pre-defined by the story format’s Developer.

You can also define your own custom macros using the (macro:) macro, and associated (output:) and (output-data:) macros

1 Like