[Twine2/Sugarcube2] display a message when game reopens after '<<script>>window.close()<</script>>'

0 votes
asked Jan 29 by Loliconman (490 points)

Is there a way for the game to set a remembered variable if a script is ran?(This code being <<script>>window.close()<</script>>)

 

In my game, a game over by a certain character will forcibly close the game(BAD END). While I know this is annoying to some, I want the game to remember this so when you reopen the game; it will goto a passage.

non working example being:

 

<<if <<script>>window.close()<</script>> >>

<<set $remember to 1>>

<<remember $remember is 1>>

<</if>>

*GAME RESTARTS.*

At title(beginning passage)

<if $Remember is 1>> goto [[you meet a terrible fate]]<</if>>

 

basically, I want the game to remember when you get a game over and display a passage once or for multiple game overs.(if possible). 

2 Answers

+1 vote
answered Jan 29 by greyelf (91,050 points)

WARNING: The window.close() function doesn't always close the current window / tab, for example in the current version of Firefox on Windows 10 calling that function may result in a Javascript error in the developer's console instead. In Internet Explore & Edge calling that function may result in the end-user being shown a dialog asking them if they want to close the window / tab.

Whichever technique you use to persist the value that indicates that a bad ending has occurred (eg. using Web Storage to save the value) you will need to do that persistence before calling the window.close() function.

commented Jan 29 by Loliconman (490 points)
good to know. changed it to just restart.
+1 vote
answered Jan 29 by TheMadExile (46,500 points)

I would not use window.close().  Your game is a guest in the player's browser, be a good one.  Attempting to close tabs/windows without permission is not how you go about that.

If you really want to kick the player to the curb on a bad end, then I'd suggest calling the Engine.restart() static method instead.  The code might look like:

<<remember $BADEND to true>>
<<script>>Engine.restart();<</script>>

To redirect the player to the bad end passage on startup, I recommend the Config.navigation.override setting, not a <<goto>> in your starting passage.  For example: (goes in Story JavaScript)

Config.navigation.override = function () {
	if (State.variables.BADEND) {
		return 'you meet a terrible fate';
	}
};

To remove the bad end hijack for the player at some point, do something like the following:

<<forget $BADEND>>
commented Jan 29 by Loliconman (490 points)
Worked great! also using the restart method now. thanks a ton TheMadExile!
Welcome to Twine Q&A, where you can ask questions and receive answers from other members of the community.

You can also find hints and information on Twine on the official wiki and the old forums archive.

See a spam question? Flag it instead of downvoting. A question flagged enough times will automatically be hidden while moderators review it.
...