0 votes
by (210 points)
sugarcube 2.27 twine2.2.1

I'm making a game where the player has 24 hours to complete their tasks. Each new passage takes away a certain amount of minutes, I have code copied and pasted into every passage, I just change the minutes depending on what they do in each passage. What I want is when hours left becomes 0 or less than 0 for the story to always go to a 'time up' passage. Do I have to use java macros?

time is a variable set in storyinit as $time

It would be great to set up a macro in java that made it so many $time was equal or less than zero the player would be forced to go to the 'time up' passage but I can't seem to get it to work. Not sure if you can put $time into a java script macro, as in, putting variables made in sugarcube into macros made in java script.

I'm also open to reconstructing my entire clock system and running it all through javascript if someone knows how I can do this and have a variable amount of minutes taken from each passage


1 Answer

0 votes
by (44.7k points)
edited by

A little bit of JavaScript is the easiest way to do it, by setting Config.navigation.override to a function which will check your countdown variable, and override the navigation if it's less than or equal to zero.  For example, you could put this in your JavaScript section:

Config.navigation.override = function (destinationPassage) {
	if (hasOwnProperty.call(State.variables, "time") && (State.variables.time <= 0)) {
		return "time up";
	} else {
		return false;  // Proceed as normal

Now, whenever you navigate to a passage, if the $time variable exists and it's less than or equal to zero, then the game will go to the passage named "time up" instead, otherwise navigation will proceed as normal.  (That code uses State.variables to access SugarCube story variables from JavaScript.  Also, hasOwnProperty is the JavaScript method for checking to see if a property exists on an object.)

Have fun!  :-)

P.S. Despite the similarity in the names, Java and JavaScript are quite different beasts.  You'll likely only need JavaScript for anything you do in Twine.