How to return a reader to the last passage they were in from a link in the sidebar

Sorry if this is a basic question, but I’m fairly new, and I couldn’t find the answer from googling it.

I’m using Twine v2.3.16, Sugarcube v2.36.1

I put a button in the sidebar that will allow the user to change the names of characters. Using StoryMenu, I put a button leading to the section, and I put in the << textbox >> macro, which works well. However, I want the player to be able to change names whenever (As characters that they meet will show up as they meet them). The issue with that is that from the documentation, you can choose which passage the textbox leads them to, but I want it to go back to the passage they were just in. How would I go about doing that?

Also, just a side note, is there a way to add a “Save” button at the bottom, so they can change multiple names at once, and then they can hit save, without having to go back to the section every time?

1 Like

You can use the <<return>> macro when you want to create a link that takes you to the passage you were just at previously. You can also use the previous() function to get the name of the previous passage that the player was in, if you need that.

However, be careful that the player isn’t able to go from one passage with a <<return>> macro to another that has a <<return>> macro, because after that those <<return>> links will just ping-pong the player between those two passages. See the “Arbitrarily long return” tip in the SugarCube documentation if you need to prevent that from happening.

I’m not exactly clear what you’re trying to do here, but basically you can give them the option to save the names, which will add the names to an array, and then you can use that array to populate a <<listbox>> macro with the names so that the player can then select the names from that listbox.

As a really simple example which you could build on, follow these steps. First, create a passage called “NameList” and put this in it:

<<if $nameList.length>>\
	<label>''Pick A Name:''\
		<<listbox "_choice" autoselect>>
			<<optionsfrom $nameList>>
	<<button "Use Selected Name" `previous()`>>
		<<set $currentName = _choice>>

Note that the <<button>> macro there uses the previous() function to send the player to the previous passage when that button is clicked on. The “backquotes” (the accent mark usually found on the tilde “~” key) around the function causes it to pass the value of that function as the passageName parameter.

Then, in the passage where the player enters the names, do something like this:

<<if ndef $nameList>> /* Initialize the $nameList array if it isn't set up yet. */ \
	<<set $nameList = []>>\
<<set _choice = $currentName>>\
<label>''Enter a name:'' <<textbox "_name" $currentName>></label>\
<<button "Add Name To List">>
	/* Add the name in the textbox to the $nameList array. */
	<<run $nameList.pushUnique(_name)>>
	<<set _choice = _name>>
	/* Update the listbox. */
	<<replace "#namelist">>\
		<<include "NameList">>\

<span id="namelist">\
	<<include "NameList">>\

That will give them a textbox where they can add a name to the $nameList array, and any names added to that list can be selected from the dropdown list below. (Just change $currentName in the above to whatever variable you’re using for the current name.)

The <<include>> macro is what is used to display the contents of the “NameList” passage within your name selection passage.

While that may not do precisely what you want, all of the parts for what you want to do are probably there, so you can use that to help figure out how to do exactly what you want.

If you have any questions on the above code which you can’t figure out after looking at the SugarCube documentation, please let me know and I’ll clarify what it’s doing.

Hope that helps! :slight_smile:


How would I go about doing that exactly. Here is what I have currently written

Main Character: <<textbox “$nFirst” “John” “<< return >>”>>

I tried removing the quotation marks surrounding the << return >>, but that didn’t make it work either. Again, sorry if this is basic, but I’m new at this.

Alright, scratch all that, sorry to waste your time, but I’m dumb, and just realized that the << return >> macro would be placed at the bottom of the back, thereby solving both of the issues I was having. I apologize, and thank you for the help.

If you did want to do something like this:

Main Character: <<textbox "$nFirst" "John" "<<return>>">>

you could do it like this:

Main Character: <<textbox "$nFirst" "John" `previous()`>>

as shown in the example code I gave you in my previous post. That would make it go to the previous passage when the user hits ENTER while the cursor is in that textbox.

Have fun! :slight_smile: