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>>
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?
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 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.
No worries, we were all noobs once!
Maybe this guide will help you
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.
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.