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
Comments
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:
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
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:
Thank you both guys!