Random passages that don't repeat

If you are requesting technical assistance with Twine, please specify:
Twine Version: latest
Story Format: latest sugar cube

im making a game with lots of random passages and I dont want them to repeat, is there a way to do that.

<<set _psg to either(
    'passage 1',
    'passage 2',
    'passage 3',
    'passage 4',
    'passage 5' 
)>>
<<button 'link text' _psg>><</button>>

this code will be used on every passage and i dont want them to repeat

You can achieve this using an array and the array.pluck method.

You’ll have to initialise your array in the StoryInit special passage or your starting passage:

<<set $passages to ["passage 1", "passage 2", "passage 3", "passage 4", "passage 5"]>>

Then, in the passage where you want the button to appear:


<<set _psg to $passages.pluck()>>


<<button "link text" _psg>><</button>>

Just remember that every time array.pluck runs, it permanently removes a random element from the array - if there is a way for the player to load the passage with the button multiple times, for example by opening/exiting inventory, etc., this may break the game once the array is empty.To prevent this, you can put the array.pluck part in the button element itself and use goto:

<<button "link text">><<set _psg to $passages.pluck()>><<goto "_psg">><</button>>
1 Like

You may also want to add code that checks that the Array of Passage Names still contains items in it, before showing the “link” that will consume one of those names.

eg. If you using the 1st of the <<button> macro bases suggestions…

<<if $passages.length > 0>>
	<<button "link text" `$passages.pluck()`>><</button>>
<</if>>

…or if you’re using the 2nd of those suggestions…

<<if $passages.length > 0>>
	<<button "link text">><<goto `$passages.pluck()`>><</button>>
<</if>>

note: As explained in the Macros Arguments section the documentation, you can use back-quote ` characters to force the evaluation of an expression being used to supply an argument for a macro call.

Because both the Array and the _psg variable contains String values you shouldn’t wrap the argument being passed to the <> macro in quotes…

<<button "link text">><<set _psg to $passages.pluck()>><<goto _psg>><</button>>
1 Like