It looks like you're new here. If you want to get involved, click one of these buttons!
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:
Comments
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!
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) 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).
<<textbox>>
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-
<<textbox>>
passages, or at least before you try using$bankActionAmount
as a number, do something like one of the following: Again, choose only one of the above.3. Never use
parseInt()
without a radix (that's the second argument). Ever. I'm serious. UsingparseInt()
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 useparseInt()
without the radix.