Money and Banking mechanics

Twine Version: 2.3.16
Story Format: SugarCube 2.36.1

I want to include two separate money indicators in my game. One for $Gold that is carried on the person, and one for $Bank that is not carried on the person. So if the person is mugged, for example, they only risk losing what they have on them.

I have these codes added and working:

<<nobr>><<set $Gold to 100>><<set $Bank to 100>><</nobr>>

Bank: <<print $Bank>> (in sidebar)

Gold: <<print $Gold>> (in sidebar)

But I also want to have a bank that the player can go to and deposit or withdraw. I cannot yet make this work.

I tried this:

How much would you like to deposit?<<textbox "$deposit" "Enter the amount here"  autofocus>><<button "Deposit">><<replace "#deposit">>$deposit has been deposited<</replace>><</button>>
<span id="deposit"></span>

and an alternative version:

How much would you like to deposit?
<<checkbox "$10gold" false true autocheck>> 10 Gold<<nobr>><<$10gold = true set $Gold to $Gold + 10>><</nobr>>

And how to have this reflected in the sidebar stats:

<<set $Gold to $Gold - "$deposit">>
<<set $Bank to $Bank + "$deposit">>
if not enough funds, cannot deposit or withdraw 

I am just starting out in Twine 2, but have some HTML5 experience. Any help would be greatly appreciated.

1 Like

I’m going to start from the ground up. First we need to create the sidebar…in addition to the print function you used we need to wrap the numbers in HTML elements so they can be updated.

In a passage titled StoryCaption tagged nobr

Gold: <span id="sidebar_gold"><<print $Gold>></span><br>
Bank: <span id="sidebar_bank"><<print $Bank>></span><br>
<span id="sidebar_status"></span>


Next we need to set the default values on the story’s start. You had 100 for the bank and 100 for the gold, but I am setting the bank to zero to begin with.

In a passage titled StoryInit


<<set $Gold to 100>>
<<set $Bank to 0>>
<<set $change to 0>>


Now we can start making the main form for withdrawals and deposits.

  • One thing to note is that the Sugarcube text box treats numbers as words by default, so we have to use Math.trunc() at a certain point to read the text box as a number.
  • Also, to set a variable to a number greater or less than itself, we can use the += and -= operators rather than what you did.
  • The rest is just a matter of updating the HTML elements we defined before.

In your Start passage:

How much would you like to deposit/withdraw?

<<textbox "$change" "Enter amount">>

<<link "Withdraw">>
<<if $change lte $Bank>>
	<<set $change to Math.trunc($change)>>
	<<set $Gold += $change>>
	<<set $Bank -= $change>>
	<<replace "#sidebar_gold">><<print $Gold>><</replace>>
	<<replace "#sidebar_bank">><<print $Bank>><</replace>>
	<<replace "#sidebar_status">>Successful withdrawal<</replace>>
<<else>>
	<<replace "#sidebar_status">>Not enough gold in bank<</replace>>
<<endif>>
<</link>>

<<link "Deposit">>
<<if $change lte $Gold>>
	<<set $change to Math.trunc($change)>>
	<<set $Gold -= $change>>
	<<set $Bank += $change>>
	<<replace "#sidebar_gold">><<print $Gold>><</replace>>
	<<replace "#sidebar_bank">><<print $Bank>><</replace>>
	<<replace "#sidebar_status">>Successful deposit<</replace>>
<<else>>
	<<replace "#sidebar_status">>Not enough gold in wallet<</replace>>
<<endif>>
<</link>>
1 Like

Thnak you very much for that. I removed the text lines “Successful deposit” and “Not enough gold in wallet” just for consistency of style, but it works perfectly.

1 Like