Solving loops

Twine Version: 2.3.16
Story Format: SugarCube 2.36.1

Brand-New Twine user.
After I Recently made a Stats, Achievements, and Inventory passage, I realized that I can’t go back to the previous passage with the previous() function. Stats - Inventory - Stats - Achievements, etc. Trapped up in a loop. Is there a solution to this?

1 Like


I feel the best way to circumvent this problem is to use a popup window.

Given an ‘Inventory’ passage exists:

<<link "Inventory">>
Dialog.setup("Your possessions");"Inventory").processText());;
1 Like

Souppilouliouma’s suggestion to use the Dialog API to show “menu” related content within dialog boxes is the better solution.

However if you really want to use Passage Navigation to show such content then I strongly suggest you read the Arbitrarily long return section of the documentation, and stop using passage() based links to return to the previous “Story” related Passage.

1 Like

Related to @Greyelf’s solution, here’s an alternative without JavaScript, done entirely with SugarCube code.

  1. First, create a passage tagged script and enter this to disable Sugarcube’s built-in back and forward buttons.
    config.history.controls = false;

  2. In a passage titled PassageHeader, add this:
    <<if tags().includes('checkpoint')>><<set $checkpoint to passage()>><<endif>>

  3. Next, tag passages that you want the player to be able to return to with the word checkpoint. You may or may not want to do this on every passage. Make sure to tag your Start passage with checkpoint so that there is a checkpoint passage by default.

  4. Then in your inventory pages etc. create links that will go to the last checkpoint passage. For example:
    <<link "Close Inventory">><<goto $checkpoint>><</link>>

I prefer this approach in my games because you do not have to account for unexpected “back” actions on the part of the player. Nor do you have to worry about the difference between back and return (only the first undoes what the player has done in the story; the second keeps what the player has done). Finally there is no need to worry about loops.

In short, you get much finer control over what the player does.

However, you will you want to make very sure there are no dead ends in your story, since there is no back button.

The other advantage of this and @Greyelf’s solution is that you can create inventory pages that go several levels deep—which is not practical with dialog boxes as far as I know.

Aww, since yesterday I just came up with this tag idea but was unable to actually make it up!
Thank you so, so much for the specific codes!

1 Like