How can I link back and forth between a set sequence of passages and a random one in Twine Sugarcube?

Hi! I am creating an interactive story in twine (sugarcube). I have a set of 7 passages, let’s call them a, b, c, 1, 2, 3, 4. I need a,b,c to appear in random order and 1, 2, 3, 4 to always appear sequentially. I am linking back and forth between the set sequence and the random order. For example:

1 -> b -> 2 -> a -> 3 -> c -> 4
and for another player it could be…
1 -> c -> 2 -> b -> 3 -> a -> 4
and for another player it could be…
1 -> a -> 2 -> c -> 3 -> b -> 4

Is there a way to do this? Thank you very much for your help!

1 Like

Here’s how I’d do it in AXMA, which isn’t quite Twine, but here’s the structure theoretically.

I would have a control passage that starts it and sets an incrementing variable, just for simplicity, I’d have one passage that handles the random text on its own.

:: control
<<if !$controller>><<set $controller = 0>><<endif>>
<<set $controller ++>>
<<if $controller eq 1>><<goto 'Passage1'>>
<<elseif $controller eq 2>><<goto 'randompassage'>>
<<elseif $controller eq 3>><<goto 'Passage2'>>
<<elseif $controller eq 4>><<goto 'randompassage'>>
<<elseif $controller eq 5>><<goto 'Passage3'>>
<<elseif $controller eq 6>><<goto 'randompassage'>>
<<elseif $controller eq 7>><<goto 'Passage4'>>
<<else>><<goto 'sequenceexit'>>
<<endif>>

:: randompassage
<<print either('One random thing this passage says.','Another random thing this passage says','A third random thing this passage says.')>>
[[Click to continue|control]]

:: Passage1
The first passage that will show. 
[[Click here|control]]

:: Passage2
The second passage that will show.
[[Click here|control]]

::Passage3
The third passage that will show.
[[Click here|control]]

:: passage 4
The last passage that will show.
[[Click here|control]]

:: sequencexit
Now the sequence is over.

The simplest way is to simply create an array of passage names in your StoryInit passage, like this:

<<set $passages = ["a", "b", "c"].shuffle()>>

The Array.shuffle() method will put the items in that array in a random order.

Then, in passage “1” you’d have the link to the next passage like this:

<<link "Next Passage Link Text" $passages[0]>><<set $nextLink = "2">><</link>>

That will send it to the first passage in the shuffled $passages array, and will set $nextLink to the name of the passage to go to after that. (See the <<link>> macro for details.)

You’ll need to copy the above code into passages “2” and “3”, except add one more to each of the two numbers in that above line of code in each of those passages (e.g. ...$passages[1]..."3"... for passage “2”, etc.).

Inside the “a”, “b”, and “c” passages, you’d have the following code to link to the next passage:

<<link "Next Passage Link Text" $nextLink>><</link>>

That will send it to the correct next passage based on the value of $nextLink, which we’re setting in passages “1” through “3”.

You can change the “Next Passage Link Text” to whatever you want in each of those lines of code.

Finally, in passage “4”, since you won’t need those variables anymore, you should get rid of them, like this:

<<unset $passages, $nextLink>>

and that should do the trick! (See the <<unset>> macro for details.)

Have fun! :slight_smile:

1 Like

The problem with HanonO’s code is that the “randompassage” passage could print out any of those three messages 0 to 3 times, so you likely won’t see all three messages. You could fix that by using an array and the Array.pluck() method to remove each choice after it was used, however putting passage text in arrays like that can get unweildy, especially if they’re large and contain lots of single and double quote markers.

Also, generally speaking, it’s best to avoid using the <<goto>> macro when you can, since execution continues after the <<goto>>, which can lead to executing unexpected code if you aren’t careful.

Finally, I wouldn’t recommend having a passage just to redirect to other passages. The Twine editor can slow down a bit as you get more passages, so it’s best to minimize your number of passages and just go directly to the next passage.

1 Like

In addition to the already listed downsides, HanonO’s code does not play well with the story history—assuming you’re not disabling it. Specifically, the use of <<goto>> within a passage to send the player to another passage without their input or interaction disrupts their ability to use the story history, because attempting to rewind will simply cause them to be thrown forward again—and with no clue as to what’s going on.

1 Like

Hi, sorry for horning in on the Twine advice. I’m using AXMA which is similar to Twine and was just presenting the way I would accomplish the task theoretically, hopefully to be interpreted into actual working Twine code. Thanks for the actual help, @TheMadExile and @HiEv .

1 Like

Are there any clear advantages with AXMA over Twine?

I use 6.1 and enjoy it because it’s like a self-contained version of Twine - it handles the player UI (saves, text size, disable sound and UI animation, etc) and has nearly everything I need and want to use built in: UI reconfiguration automatically based on screen/window size, image, sound, music handling, a side menu, real-time timers, keyboard shortcuts, highlight-click text commenting/uncommenting, find any text via the navigator, in-passage search and replace, and a lot more.

It has a lot of quirks I’ve learned to exploit or work around, however for some reason the main author UI of 6.1 on PC has a weird bug in that you can’t predict where you need to grab passages with the mouse to move them. I work around that when I’m on my PC by not rearranging passages and text-searching by typing the passage name or text I know is in it (it’s more workable than it sounds, but this baffles people who initially look at it). I primarily use it in OSX on the Mac.

There is a new version (AXMA-JS) which is different and completely made in JavaScript and lets you do anything you know how to do with it. Problem is I don’t know JS, so easy stuff that I’m used to accomplishing with 6.1’s macros now is reinterpreted into its JS equivalents and they haven’t yet translated the manual into English from its native Russian. It might make more sense to someone who already knows JS.

1 Like

@HiEv That worked perfectly. You are brilliant and a hero! Thank you very much for the detailed and helpful response.
T

1 Like