Macro to return to previous passage, but with exceptions

If you are requesting technical assistance with Twine, please specify:
Twine Version: 2.3.16
Story Format: Sugarcube 2.36.1

I have recently switched over from Harlowe to Sugarcube for the first time, so forgive me if this is a foolish question.

In my game, I have an Inventory page. At the foot of the page is the <<return>> macro, to return the player to whatever passage they clicked over to the Inventory from.

The Inventory page also has links to look more closely and interact with objects. For instance, if the white mask is in the inventory, it will be listed as [[white mask]], a clickable link that takes you to a new passage where you can interact with the white mask. At the foot of the white mask page, there’s a link back to the Inventory.

You can see where this is going…

If you go to the Inventory, then go to the white mask passage, then go back to the Inventory…you’re stuck in a loop. There’s no way to return to the most recently visited passage of the main game.

Is there a way to code the <<return>> macro (or something like it) so that it goes to the most recent passage WITHOUT a specific tag? So, for instance, I could tag the white mask passage and all the other object passages with a “donotreturn” tag, and the <<return>> macro could go to the most recent passage WITHOUT that tag? Or, conversely, a way I can specify to <<return>> only to the last visited page with a “maingame” tag?

If not…are there any other workarounds that I’m missing?

Option 1. You might just want to use the dialog popup. Assuming your Inventory passage is called “Inventory”:

<<link "Open Inventory">>
<<script>>
Dialog.setup("Inventory");
Dialog.wiki(Story.get("Inventory").processText());
Dialog.open();
<</script>>
<</link>>

You can then create a similar trigger to open the “Mask” passage from within the “Inventory” passage. The second dialog box will just replace the first.


Option 2. There is a way to negate tags with logic with the exclamation mark meaning “not”

First, tag some passages with donotreturn

Then, in your PassageFooter include:

<<if !tags().includes("donotreturn")>>
<<set $bookmark to passage()>>
<<endif>>

Then you can create a link in any passage like this

<<link "Go to bookmarked passage">><<goto $bookmark>><</link>>

However, this might mess up your history and any attempts to use the return macro. It could also lead to dead ends if you are not careful.


Option 3. As you noted, you could do the opposite by just tagging some passages with maingame, in which case

<<if !tags().includes("donotreturn")>>

would become

<<if tags().includes("maingame")>>

Note the absent exclamation mark.

This doesn’t solve the history complications mentioned above though.

1 Like

Option 2 (and 3?) are covered by the Arbitrarily long return section of the SugarCube documentation.

Using a dialog, like explained in Option 1, is generally the best solution when it comes to showing other content (like Inventory) while still being able to return to the “last” Passage shown.

1 Like

Thank you both!!