Howdy, Stranger!

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

[SugarCube]Textbox and numbers

edited October 2015 in Help! with 2.0
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 */
<<else>>
<<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 1>> /* if Statmod */

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

|

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


|

<span id="STATMODSandM">
<<click "Change Both">>
<<replace "#STATMODSandM">> <<textbox "$SandM" "10" "Settings">> <</replace>>
<</click>>
</span>
<<if $SandM gt 0>> <<set $Magic to $SandM; $Stam to $SandM; $SandM 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.

Edit: This post took almost two weeks to get approved for posting, I no longer need help to this as I change the whole thing to act differently. However, I'll leave it up as I still would like to know why textbox didn't set $Magic as a number.

Comments

  • Tomo wrote: »
    However, I'll leave it up as I still would like to know why textbox didn't set $Magic as a number.
    Quite simply, because that's not what it does. The <<textbox>> macro, as its name implies, sets the given $variable to the text (i.e. string) entered by the player in its input field. If you wish that text to be converted into an actual number afterwards (rather than a numeric string), then that's something you'll have to do separately.

    For example, since you appear to be forwarding to the passage "Settings" afterward, you may be able to do something like the following at the top of that passage:
    <<set $Stam to Number($Stam), $Magic to Number($Magic), $SandM to Number($SandM)>>\
    
    That will attempt to ensure that each of the $variables is an actual number.
  • ......
    Thank you for the quick response and solution, since the <<textbox>> worked fine for the other variables, i didn't think that would be why.
  • The reason why applying $Stam -= 1 to the numeric string afterward worked as expected is because - is either: the arithmetic subtraction operator or the unary minus operator. Both operators only work upon actual numbers. In this case you were actually using the combo -= arithmetic subtraction assignment operator, but the result is the same. Since the operators only work upon actual numbers, using them forces the coercion of their operands into numbers.

    On the other hand, the reason why applying $Magic += 1 to the numeric string afterward did not work as expected is because + is one of three operators: the arithmetic addition operator, the unary plus operator, or the string concatenation operator. The first two only work upon actual numbers, while the latter only works upon strings. Again, in this case you were using the combo += operator, which is either the arithmetic addition assignment operator or the string concatenation assignment operator. Since one of the operands is a string, the system assumes that it should use the string concatenation operator instead of the arithmetic addition operator. Thus, instead of the string operand being coerced into a number for arithmetic addition, the number operand is coerced into a string for string concatenation.

    Welcome to the wonderful world of dynamic typing and operator overloading. :)
Sign In or Register to comment.