JavaScript $Return Issue in Sugarcube

Hey all. I’m pretty new to coding in general, so please bear with me here.
I’m trying to implement the return strategy described here: to enable the return command on a cheat/inventory page. The goal is to allow the player to return to the last passage they were on, instead of falling into a loop of inventory or cheat mode passages.

I’ve placed the code at the top of my story javascript:

	predisplay["Menu Return"] = function ($return) {
	if (! tags().contains("noreturn")) {
		State.variables.return = passage();
	}
};

And have applied the appropriate tags on the passages themselves. But when I go to play the game I get this error message:

Apologies! An error has occurred. You may be able to continue, but some parts may not work properly.

Error [tw-user-script-0]: missing } after property list.

I’ve done some googling to try and suss out what that error means, but the little things I’ve tried to fix the code (making sure there’s no missing/excess punctuation) doesn’t work. Any tips? Appreciated!

PS I’m using Twine 2.3.3 and Sugarcube 2.28.2.

Hrm…looks like the problem is elsewhere in your story javascript: If I paste the code into the story javascript of an empty project it works fine for me…

I have to agree with Josh. That’s not where your error is. Although, you wouldn’t want to use $return as your function parameter. Just use something like taskname like in the example since it’s not even used for that function.

Do you have other javascript that would be causing a tag matching error? Sometimes in javascript if you type something that it completely doesn’t expect (like too many closing parenthesis or missing an end quote, for example), it’ll not see the curly bracket and will erroneously report it as missing. It’s still a matching error, but it’s for the wrong one.

1 Like

Well, first thing, that code is really old and it’s not recommended you do things that way anymore.

Second thing, what you’re asking about is actually covered in the SugarCube documentation within the “Arbitrarily long return” section. Funny enough though, the sample code in that section still uses the old way of doing things. :stuck_out_tongue:

The modern version of that code would be:

/* Trigger the following code at the start of navigation to a new passage. */
$(document).on(":passagestart", function (event) {
	/* Make sure the current passage doesn't have a "noreturn" tag. */
	if (!tags().includes("noreturn")) {
		/* If it doesn't, then set $return to the current passage name. */
		State.variables.return = passage();
	}
});

That uses the :passagestart event to trigger that code every time you go to a new passage. It then stores the name of that passage so your code can go back to it, unless the passage has a “noreturn” tag, which you should add to any menu passages. (The comments in the code above explain what the next line of code does.)

Then, in all of your “noreturn” tagged menu passages, you’d just do something like this:

<<link "Return" $return>><</link>>

to provide a “Return” link, which would take you to the last non-menu passage you’d visited, since $return will hold the name of that passage.

Hope that helps! :slight_smile:

This is fantastic. Thank you so much (and to everyone else) for your help! It was in fact an issue with separate code, but HiEv’s code works flawlessly as well (the old code I was using was not).

Thanks again!