Sanity meter not diminishing value when variable should change via link click

Using Twine 2 and SugarCube 2.

Running into an issue where I’m trying to update a variable via a link click.
I had this working with a simpler sanity bar, but this new one isn’t working.

I’m using this for the meters: Custom Macros
Using the prettycode in the Javascript section.

In StoryInit the meter macro is defined:

<<newmeter '$sanitymeter' 1>>
	<<animation 300ms>>
    <<colors '#741376' '#B45AB6' '#707EBA'>>
    <<label 'Sanity' 'white' left>>
    <<sizing 20%>>
<</newmeter>>

In some passages I have it displayed.

In the Javascript area I have this code to return someone to a game over passage when sanity = 0

Config.navigation.override = function (destPassage) {
	var StoryVar = State.variables;
	if (StoryVar.sanitymeter <= 0) {
		return "Lost Sanity Game Over";
	}
	return destPassage;
};

And then this code in one of the passages should take the meter down to 0 on click and then return the user to the game over passage

[[Pick up a key|key room][$sanitymeter -= 1]]

Instead it’s going to the next passage.

In actual usage this click would likely diminish the sanity meter by .1 or something of the like until eventually if they’ve picked up enough items to effect them negatively they’d reach 0 and then go to game over, but for now I’m just trying to make sure I have the logic working!

I’m not well versed in JavaScript, but I know using the passage :: PassageDone with a simple <<if>> macro works:

:: PassageDone
<<if $sanitymeter lte 0 && passage() is not "Lost Sanity Game Over">><<goto "Lost Sanity Game Over">><</if>>

EDIT: this works with PassageReady too, or PassageFooter or PassageHeader.

For the meter itself, you need to update it every time you change the value (with his <<updatemeter>> macro). You can find the code in his documentation here

This works, but keep in mind that using the <<goto>> macro to automatically forward the player to a different passages breaks history navigation, (see here). Using Config.navigation.override is generally a better way of doing that.

@lkingino, your code works for me as is. Are you sure that the code in your game is the same? If so, are you setting $sanitymeter correctly? You can check its value in debug mode in the lower right panel.

Edit: I see you’re setting the meter’s name to '$sanitymeter', so, to be clear, that’s just the name of the meter—it doesn’t actually bind the meter to the variable $sanitymeter, or create the variable if it doesn’t already exist. You need to specifically create it with <<set $sanitymeter = 1>>.

2 Likes

Lost my extra time to work on this after Christmas vacation!

Yes, I did copy the code (rather than rewrite it here).

Interesting, documentation seemed to indicate that creating the meter would set it as default 1.
But I’ll try using “set” !

Manon - thanks for the note re the “updatemeter” - will take a look there too :slight_smile:

1 Like

Just following up to say that using the set macro did the trick, thanks!

2 Likes