Howdy, Stranger!

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

Problem with numeric values in textboxes (Sugarcube)

I have searched the forums for an answer to this question, but have been unsuccessful.  I am creating an RPG using Twine 1.4.2 and Sugarcube 1.08.  I am working on a bank system and have encountered really strange behavior on how calculations are handled with player-entered numbers.  I'm sure it's something I'm doing wrong, but I have no idea what.

I have this code:

How much will you want to deposit or withdraw?

<<textbox "$bankActionAmount" 0>>

[[Deposit|bankActionDeposit]]
[[Withdrawl|bankActionWithdrawl]]
Now, for an example of what is happening.  The player starts off with 150 coins in hand ($player.coins) and 0 coins in the bank ($player.bank).  So, I enter a deposit of 20 and click "Deposit," which then processes the "bankActionDeposit" passage:

<<set $player.coins -= $bankActionAmount>>
<<set $player.bank += $bankActionAmount>>
Then I have the following on the next screen to post the results of the transaction:

<b>Coins on hand:</b> <<print $player.coins>>
<b>Coins in bank:</b> <<print $player.bank>>
Using the above senario, entering a deposit of 20 coins, here is the resulting output:

Coins on hand: 130
Coins in bank: 020


The "020" is odd, but it's technically the correct value, but now, I go back and withdrawl 10 coins.  Here is the result:

Coins on hand: 13010
Coins in bank: 10

So, now the $player.bank value is normalized, but the $player.coins is wrong.  I see what it is doing.  In the first instance, it is adding the 0 + 20 and in the second example, it is adding the 130 + 10, as if it was catting two strings together.  So, I guess I'm not understanding why it's not processing it as numeric values.  Any assistance is greatly appreciated!

Comments

  • It's been a very long time since I made anything with Twine. What I attached was made with Twine 1.4.1 and some old version of SugarCube from around April, I think.

    See attached TWS.

    Notice the "<<set $deposit = parseInt($deposit)>>" line. There's almost certainly a better way, but I tested it and it worked just fine, so maybe you can use that until someone else tells you the "right" way. ;)

    Hope that helps! :)
  • Small addition to Sharpe's post to explain why you need to use the parseInt() method.

    HTML Text Input fields like the one used by SugarCube's <<textbox>> macro (and also the vanilla <<textinput>> macro) see everything entered into them as text.
    When you enter 123 into the field you see it as a number but the browser sees it as three characters. (the character 1 followed by the character 2 followed by the character 3)

    <<set $player.bank to 0>>
    <<textbox "$bankActionAmount" 0>>
    <<set $player.bank += $bankActionAmount>>
    So when you try to do arithmetic like the third line in the above code, what you are actually trying to do is add the number stored in the $player.bank variable to the text stored in the $bankActionAmount variable which can cause a problem. To get around this problem you need to convert the text in $bankActionAmount into a number and you can do that by using either parseInt (number contains NO decimal points) or parseFloat (number contains decimal points).

  • 1. The problem is that &lt;&lt;textbox&gt;&gt; yields a string and you're not converting that string into a number before attempting to use it as a number.

    2. At the top of your post-&lt;&lt;textbox&gt;&gt; passages, or at least before you try using $bankActionAmount as a number, do something like one of the following:

    Prefix it with the unary plus operator (this is not the addition operator, which also a plus sign)
    <<set $bankActionAmount to +$bankActionAmount>>

    Use Number() without the new operator
    <<set $bankActionAmount to Number($bankActionAmount)>>

    If your amounts will be whole numbers only, use parseInt() with a radix of 10
    <<set $bankActionAmount to parseInt($bankActionAmount, 10)>>

    If your amounts will be decimals, use parseFloat()
    <<set $bankActionAmount to parseFloat($bankActionAmount)>>
    Again, choose only one of the above.

    3. Never use parseInt() without a radix (that's the second argument).  Ever.  I'm serious.  Using parseInt() without the radix can lead to incorrect parsing in browsers that still have significant market share (as of today).  Since you cannot control what a player may enter, never use parseInt() without the radix.
  • Thank you all! No more pulling my hair out (at least for now). :D
Sign In or Register to comment.