Set Number Variable Not Changing

As a note, I’m new to Twine. I’ve spent the last two hours trying to figure this out. I’ve looked at a Beginner’s Guide to Twine 2.1, the Twine Cookbook, and read about variables, but they haven’t given me a clue to fix the issue I’ve been having.

I want to make the sanity meter decrease from 100 to 95 based on a choice. This is how I wrote it to show the sanity variable and the one written in JavaScript in the first section, and how it’s shown on screen on the player profile in the second section:


<<set $sanity to 100>>

State.variables["Sanity"] = 100


Sanity: $sanity %

I’ve tried putting both variations of these in the section after you make the choice, but it didn’t change the 100 after selecting the particular choice:


<<set $sanity -=5>>

<<set $sanity to $sanity -=5>>

Next, I’ve tried putting it within the choice itself, but it doesn’t work for either variation, not even moving from the top to the bottom of this did anything to change it:


<div class="choice">[[Go out there and stop them]]
	<<set $sanity to $sanity -=5>></div>

Then I tried this, but I keep getting a state error, or ‘<<‘ is an unexpected token error, even though it’s Sugarcube:


<div class="choice">[[Go out there and stop them][<<$sanity to $sanity -=5>>]]</div>

<div class="choice">[[Go out there and stop them][State.set $sanity -=5]]</div>

Lastly, I tried links like the one from the Twine Cookbook, but it didn’t work at all either:


<div class="choice">
<<link "Go out there and stop them">>
	<<set $sanity - 5>>
    <<goto "Go out there and stop them">>
<</link>></div>

I’m at the end of my rope here. I’ve scoured online to find the solution to this specific problem with making a set number decrease, but I can’t find it. Please, if anyone can tell me, what am I doing wrong? Is there a solution to change the set 100 variable to decrease? Is it supposed to be a temporary variable instead of a set one?

<<set $sanity -= 5>> should work: I’m not sure why it isn’t working for you. Is it possible that you’re displaying the value before you decrease it? The display is not “live” so the value shown on the page won’t change if you update the variable afterwards…

<<set $sanity to $sanity -= 5>> This one is wrong (hrm. Actually it should work but for weird reasons). But you don’t want “minus-equals” there, you just want “minus.” Try <<set $sanity to $sanity - 5>> or (equivalently) <<set $sanity = $sanity - 5>>

This isn’t actually putting it IN the choice, it’s just putting it before or after the choice. And again, you have a minus-equal where you only want minus.

When you use these “setter links” you don’t put the << – you only put the $sanity to $sanity - 5 part. It treats that part of the link as if it’s already in a <<set>>. So:

[[Go out there and stop them][$sanity -= 5]]
[[Go out there and stop them][$sanity to $sanity - 5]]

Here you’re doing <<set $sanity - 5>> which calculates the subtraction but doesn’t assign it the result to anything, so it just disappears.


But several of your examples should change the variable, so there’s something else going on here. My best guess is that you’re displaying the value first and then changing the variable afterwards, but it’s impossible to tell without seeing more of your code, I’m afraid…

My best guess is that you’re displaying the value first and then changing the variable afterwards, but it’s impossible to tell without seeing more of your code, I’m afraid…

I’m using the Leonora UI Template by lapinlunaire-games on itch.io, I’m using it for my project in particular, if it helps with that. There isn’t a whole lot of coding I’ve added besides the image links and what I have shown in my preformatted text. Specifically for where the sanity is located, I’ve put it into the Character Profile.

Ah. And if you go to another passage after the one where you change the sanity, does it show as changed?

It doesn’t show that it’s changed, even after going to the insert name here section and the one after that; it’s still stuck at 100. I’ve put in every method you suggested into my code, both in the choice itself:

[[Go out there and stop them][$sanity to $sanity - 5]]

And putting it after the choice is made, like you’ve suggested, removing the ‘=’ sign and just having the minus, it still shows up as Sanity: 100. Idk if there’s something wrong with the template I got or what, I might try it with a different template I downloaded and see if it works then.

Edit: Nope, even with the different template, still the same issue.

I think it might be the value first, then changing the variable after, since I want it to be at a max of 100. Do you have a solution for that in particular?

Hrm. I tried grabbing that template and it worked for me. Trying to think what else it could be… you’re sure you’re spelling $sanity the same in both places, including capitalization?

Yes, I triple checked them for spelling errors and capitalization, then copy pasted every one of them just in case. And its still not working.

Variable and Property names are letter case-sensitive.

Your 1st example <<set $sanity to 100>> is refencing a Story Variable named sanity with a lower case s. Your 2nd example State.variables["Sanity"] = 100 is refencing a Property named Sanity with a upper case S.

As those two names are not the same, they can’t be used to access the same value, to do that you’d either need to change both names to start with a lower case s

<<set $sanity to 100>>
State.variables["sanity"] = 100

…or change both names to start with an upper case S

<<set $Sanity to 100>>
State.variables["Sanity"] = 100

…which you do is up to you.

I changed it to the State.variables["sanity"] = 100, instead of the capital, and it still doesn’t work. I went back to make the first letter capital ‘Sanity’, and changed every other variable to ‘$Sanity’ instead of "‘$sanity’:

<<set $Sanity to 100>> State.variables["Sanity"] = 100

And it still doesn’t work. I even tried the same thing in the web version, which I don’t normally use (I use Twine Windows), and its still the same issue.

Edit: Do I need to put State.variables["sanity"] = 100in a different place in JavaScript or something? Or is that the correct place to put that in? (Assuming that everything is spelled and capitalized correctly)

The APIs like State are JavaScript based, so their usage should be either:

  • within that project’s Story JavaScript area.
  • within the body of the <<script>> macro
  • within the argument area of any macro call that supports JavaScript.

eg. In the content of a Passage…

/* assign a value to the $sanity Story variable. */
<<set State.variables['sanity'] to 100>>

/* output a copy of the current value stored in $sanity */
<<print State.variables['sanity']>>

/* compare the current value of $sanity to a number */
<<if State.variables['sanity'] is 100>>...do something...<</if>>

want to make the sanity meter decrease from 100 to 95

You mention a sanity meter but none of the examples you’ve provided so far have the code for a “meter” in them. So you need to explain how you’re implementing this “meter” the $sanity variable is meant to update.

If you’re using Chapel’s Meter Macro Set addon, then the <<updatemeter>> macro needs to be used to update the state of a displayed meter if no Passage Transitioning is occurring.

You mention a sanity meter but none of the examples you’ve provided so far have the code for a “meter” in them. So you need to explain how you’re implementing this “meter” the $sanity variable is meant to update.

That’s my bad, poor wording on my part, I’m not looking for a meter at the moment (perhaps in future implementation, or if its the only way to fix this issue). My main focus is making a 100 go to a 95 upon/after selecting a choice, currently trying to do it like this, at the end:

Sanity: 100

It shows the 100 seen here, but it doesn’t decrease at all, even though I was apparently doing it correctly, kinda.

I’ve also tried copying and pasting it into the Story JavaScript area, and it messed up the UI of the template. I’ve also done it within the <<script>> macro, but it did the same thing is messing up the UI, while also not working.

(I can’t embed the specific screenshot of this UI issue, it’s not letting me)

Also, if you’re able to, can you provide the coding for what you did so I can try to replicate it exactly? Since it works for you, and not for me, I’m starting to think it’s a Twine Sugarcube related issue, or Twine hates me in particular.

In the Title Passage:

added <<set $sanity to 100>>

In the Character Profile:

I changed <span>Label:</span> Information to <span>Sanity:</span> $sanity%

In the Install Passage:

added <<set $sanity -= 5>>

Resulting HTML: Leonora UI Template.zip (162.2 KB)

When the contents of the visited Passage is processed, and that of special passages like StoryCaption, if that processing encounters usage of Naked Variable markup a copy of the the variable/property/element’s current value is added to the page output being generated.

eg. if the following was added to the StoryCaption special Passage

Sanity: $sanity

…then if the current value of $sanity is 100 when that special passage is automatically update near the end of the current Passage Transition process, it would output…

Sanity: 100

note: because the contents of the StoryCaption special Passage is automatically processed during the Passage Transition process, any variable changes done while that transition is occurring will automatically appear when that transition ends.
eg. the $sanity variable changes done in the following link examples will automatically appear in the resulting page update.

/* Markup link based examples */
[[Go out there and stop them][$sanity to $sanity - 5]]
[[Go out there and stop them][$sanity -= 5]]

/* Transitioning Macro link based examples */
<<link "Go out there and stop them" "Go out there and stop them">><<set $sanity to $sanity - 5>><</link>>
<<link "Go out there and stop them" "Go out there and stop them">><<set $sanity -= 5>><</link>>

However, because a copy of the variable’s value was added to the page, any dynamic changes to that variable’s value that occur after the Passage Transition process has finish will not automatically appear in the page.
eg. there is no direct connection between the variable that was outputted and the page, so updating one doesn’t cause the automatic update of the other.

So if such page updating is desired outside the Passage Transition process, then you will need to code that yourself. And the following is one common technique that can be used in recent release of SugarCube.

1: Use the <<do>> macro to identify the area of the page that needs updating.

In this case I’m associating an identity of sanity to the area of the page where the $sanity variable’s value will be outputted.

Sanity: <<do tag 'sanity'>>$sanity<</do>>

2: Use the <<redo>> macro to refresh that identified area as needed.

<<link "Update Sanity">>
    <<set $sanity -= 5>>
    <<redo 'sanity'>>
<</link>>

Thank you and Greyelf for the assistance! The <span>Label:</span> Information to <span>Sanity:</span> $sanity% fixed the issue, I honestly didn’t know you had to do change it in order for it to process.

Can I ask what you had there before that wasn’t working?