Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Variable treating number as a string of text instead of value.

So I've been messing with twine the past couple of days, fun thing to learn I must say.
But horribly annoying when it comes to troubleshooting what's gone wrong, now a quick explenation of what I've done should be in order. I've made two variables $Stamina and $Magic, both these values are increased and decreased by 1 each click the player does through a small code in the sidebar.
<<if $Options is 1>> /* Time, Magic and Stamina will not increase */
<<set $Time += 1; $Stam -= 1; $Magic += 1>>
<</if>> /* Options if end */

This piece of code runs everytime you click a passage as a way to make progress with each click, this part works fine.
But I made a 'debug' tool to help manage things and with that I made it so that it's possible to change Magic and Stamina with a </span> & <<Textbox>> command
<<if $Statmod is 0>> /* if Statmod */

<span id="STATMODSTAM">
<<click "Change Stamina">>
<<replace "#STATMODSTAM">> <<textbox "$Stam" "10" "Settings">> <</replace>>


<span id="STATMODSTAM">
<<click "Change Magic">>
<<replace "#STATMODSTAM">> <<textbox "$Magic" "10" "Settings">> <</replace>>


<span id="STATMODSandL">
<<click "Change Both">>
<<replace "#STATMODSandL">> <<textbox "$SandL" "10" "Settings">> <</replace>>
<<if $SandL gt 0>> <<set $Libido to $SandL; $Stam to $SandL; $SandL to 0>> <</if>>

<<endif>> /* if Statmod END */

Basically I can use it to increase/decrease both the the stats $Stam and $Magic, however, after doing so the very first command I showed you, the one that runs in the sidebar partially stops working.
<<set $Magic += 1>>
This one stops adding 1 as a value to the variable, instead adds "1" as a text string to the variable $Magic. Making the number 10 become 101 and for each passage I click, it adds more becoming 1011, 10111 and so fourth.

I'm slowly going crazy trying to figure out why += doesn't work but the -= works just fine. I desperatly need pointers.
Just to add a bit more information, I am using Twine 2 w/ SugarCube 1.0.26.


  • edited October 2015
    Instead of using "<<set $Magic += 1>>", use "<<set $Magic++>>. It accomplishes the same thing.

    Same for -= 1, use --.

    I don't know if that'll solve the issue, and it's not a universal solution, but just a pointer.

    Twine is iffy with variables in general I find, make sure you're setting $Magic to a number (and not a string) in your StoryInit.
  • You are using a <<textbox>> macro, and as the name of the macro implies it allows a person to enter a Text (which is layman for String) value, which is why your $Stam/ $Magic/$SandL variable contain a String value. When you try to add a number (eg += 1) to these String values Javascript sees that the two values are of different datatypes so it convert the number to a String and then concatenates the two values.

    So what you need to do is convert the input from the user to a number just after they input it. There are a number of different Javascript functions you can use Number, parseInt, and parseFloat depending on the type of number you are expecting them to enter. Because you cant control what the user actually typed into the text field you will need to first check if they actually entered a number instead of some letters or symbols, and you can do that using isNaN

    So something like the following should work.
    <<if not isNaN($Stam)>>
    	<<set $Stam to Number($Stam)>>
Sign In or Register to comment.