#### Howdy, Stranger!

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

# Gold, Silver, Copper calculation

Hey fellas,

I'm thinking of setting up a traditional gold silver copper system for which each 100 coppers need to turn into 1 silver etc. I wrote this.
```<<for \$i = 0; \$i lt 10; \$i++>>
<<if \$copper > 100>><<set \$copper -= 100>><<set \$silver += 1>><<endif>>
<<if \$silver > 100>><<set \$silver -= 100>><<set \$gold += 1>><<endif>>
<<endfor>>
```

Now I don't want to add the above to every passage where there is gold interaction. Is there a way for me to put it into a separate passage and when there is a gold interaction have the story run through that separate gold recalculation passage and then to the next relevant passage without the user seeing it? Is there another way for the gold to be recalculated automatically?

Thanks

• edited January 2016
You need to state which story format you are using when asking a question, as answers can be different for each one. Based on the syntax of your example I am going to assume you are using the version of SugarCube that comes with Twine 2.

note: I am use to the 10cp = 1sp, 10sp = 1gp conversion rate.

The simplest method to do what you asked is to place that code into a new passage named something like Gold Recalculation, and then to call it inside the other passages using a <<display>> macro like so:
```<<display "Gold Recalculation">>
```

side note:
Generally when implementing a currency system it is a good idea to only store one value which represents the total number of the smallest unit that a person has and to then transform that number into larger units at the time the total value needs to be displayed.

So using your units of gold, silver, and copper as an example you would record the currency the player has as the total number of coppers then when you wanted to display that value you would transform it into gold, silver, and copper amounts.

eg. If the player has 2 gold, 5 silver and 20 copper you would store that total value as 20520 coins based on your conversion rates (or 270 based on mine)
=> (2gp * 100 * 100) + (5sp * 100) + 20cp = 20520
=> (2gp * 100) + (5sp * 10) + 20cp = 270
• I would suggest either using a widget or, as greyelf suggested, displaying a non-story-path passage (via <<display>>).

Additionally, I'll second their recommendation to use a single coin denomination internally and to split it out only for display purposes.

Beyond that, to address what you were attempting to do, you really do not want to use a loop for this sort of thing. What you should have been trying is something like the following:
```<<silently>>
<<if \$copper gt 100>>
<<set \$silver += Math.trunc(\$copper / 100)>>
<<set \$copper %= 100>>
<</if>>
<<if \$silver gt 100>>
<<set \$gold += Math.trunc(\$silver / 100)>>
<<set \$silver %= 100>>
<</if>>
/* So on and so forth. */
<</silently>>
```
• Math.trunc
Aaah, that's what I was missing. I was looking around for an Integer function but could not find any.

Thank you both guys!