Adding and subtracting from variable sometimes yields incorrect results

I have an issue I have not been able to troubleshoot.

eg1. If I click “add” the result is 100, then click “add” again result is 100100 (that’s not a typo), then click “subtract” the result is 100000, the click "subtract again the result is 99900.

eg2. But, if I click “add” the result is 100, then click “subtract” the result is 0 (zero), then click “add” the result is 100, then click “add” again the result is 200.

eg3. Additionally doing the revers of the first example and I start with two clicks of the “subtract” button it works correctly.

StoryInit contains:

<<if $variable isnot "defined">> <<set $variable to "">> <</if>>

Passage contains:

<<textbox "$variable" $variable >>

<<button "subtract">>
     <<set $variable to $variable - 100>>

<<button "add">>
    <<set $variable to $variable + 100>>

Thanks for help!

1 Like

I’m not familiar with SugarCube, but setting a variable to “” means it becomes a string. So adding 100 to 100 will be 100100 and not 200. Try setting $variable to 0 instead maybe?

Edit: Oh, maybe it’s because the text input is returning a string. Maybe look at this forum topic…


I see yea it does. That escaped me. I corrected that and it works fine.

Unfortunately I used it because I didn’t want any value visible until the player added a value. This was for clarity. I tried to put the init in a span and style the initial value but I was unable to effect it. Is this possible?

Thanks for the article. Will read in a sec. I just discovered that even after correcting the inits which worked perfectly if I type a number into one of those fields it will behave strange. Something about the keyboard numbers appear to be a string instead of integers. I’m not too worried about it but will investigate for learning.

Textboxes create a string (aka text). You’ll want the <<numberbox>> macro instead (that creates an integer aka a number).

What are you trying to do here?
If it’s to update the displayed value of the variable, you can do that with <<replace>> macro, or use Cycy’s liveupdate custom macro.

(also, we have a Twine category to post your Twine related questions :wink: I’ve moved that post again, but you might want to post there directly)


Just for additional context, in JavaScript (and therefore in SugarCube) + is used for adding numbers and for concatenating strings, and the rules for what kind you get are complicated. So probably what’s happening here is if you set $variable to 0 in StoryInit, it starts off as a number, but once you change the textbox, now it’s text (a string).

Putting a standalone + before a string will try to convert it to a number, so you could write +$variable + 100 but if the user types “abc” into the textbox you’ll get a NaN (Not a Number) value (which will infect anything you add it to), so you really do want a numberbox here.


Re: NumberBox. I read up on TextBoxes and never noticed mention of NumberBox. Should have assumed there was one and just tried it. As it stands I removed the TextBoxes and used buttons to add and subtract desired value chunks as needed so all mouse or finger controlled.

Re: The script is to update the values. I am an amateur or noob. I’ll experiment with your suggestion since the reloading script felt dodgy to do.

Re: Twine category I will do so. thanks. I wasn’t sure.

Thanks for the info. It clarifies some things for me. I had read up on the contamination and watched a YouTube video about it.

No worries, we were all noobs once!
Maybe this guide will help you :wink:

Values are saved everytime you change passage (including re-loading the current passage - not F5 reload, but a link loading the current passage). So if you change the value multiple time before moving on to the next page, it will save the final value.

The project is all on a single passage. Has init, footer and header though.

All your calculations/interactions will still work, even if you don’t move passages, but nothing will be saved in the State/Cache/Savefile until you move to a new passage (or reload your current passage).

A passage reload in SugarCube is something like this:

:: PassageName

[[Reload|PassageName]] or [[Reload|passage()]]

These do not function inside the buttons. BTW I have used two reloads because in some instances it does not update. I know it’s ugly but it works. This was just an exercise project to learn. Publishing the twee bellow if you’re interested. Thanks for the help. (2.8 KB)

1 Like