Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Best Practice for Smart "Return" in SugarCube 2

With SugarCube we have the <<back>> and <<return>> macros.
There doesn't seem to be built in functionality to "return" to the most recent passage that does not have a certain tag.
I need this functionality because I have StoryMenu passages that change state (you can wear things inside the inventory), so the user can't just navigate backwards as that would undo the wear action.
Vanilla <<return>> is also not viable since I have multiple StoryMenu passages. If both of them use <<return>> as the escape mechanism, deadlock can occur where I visit them in succession, so that <<return>> in each passage refers to the other StoryMenu passage (I've seen this in a couple of games D:).

I'm wondering what would be the best way to build in this functionality?
As a widget or pure JS function? (what advantages/disadvantages)
For JS function, I'm thinking something like:
// defined in StoryJavascript
window.returnToStory = function() {
	// returns the (string) passage name of the latest story passage
	var p = State.length - 1;
	while (p-- > 0) {
		// somehow extract passageTitle from State.index(p)
		if (!Story.get(passageTitle).tags.contains("menu")) {
			return passageTitle;
	return "No previous story passage - something went wrong...";

// inside the StoryMenu passages
[[Return|<<= returnToStory()>>]]

How can I extract the passageTitle from a moment? The documentation doesn't describe what a moment(state) object is...

As for building it as a widget, only the "tags" story function seems promising and I have no idea how to see the passage names past the previous one inside the story.


Sign In or Register to comment.