In game currency

I’m planning on having a form of in game currency that I’m not sure how to code. The idea is that the currency comes in the forms of copper, silver and gold, 100 coppers being worth 1 silver and 100 silvers being worth 1 gold. The trouble is, I’m not sure how I’d go about coding the game so that, once you have 100 coppers, it’s transformed into 1 silver. Any body have any ideas how I might be able to do this? :blush:

What about creating *if command checks at timed intervals and write conversion commands using the *if route?

2 Likes

You can work it around using the “modulo” mathematical operation.
But I’m not a fan of modulo, so I’m not really sure how it’s proper application. :confused:

I think I also saw a similar thread as yours, talking about 1 gold = 100 silvers = 10k coppers and it’s code in modulo, but that was a long time ago. Let me see if I can’t find it.

2 Likes

use modulo math!

I’m not sure what’s offered as far as truncation of decimals (does the default integer type coerce to integers?), but basically it looks like this, in pseudocode

cash //this is the raw cash value used internally
Gold //number of gold pieces
Silver //number of silver pieces
copper // number of copper pieces

Gold = cash /10000
silver = cash % 10000 //taking the modulus, the remainder part of the division by 10,000
copper = cash % 100 //taking the modulus, the remainder part of the division by 100

This will mean one gold per 10,000 copper, one silver for every remainder of that (coerced to an integer) and then the remainder of that is displayed in copper.

Not sure if this would work?

*create copper 0
*create silver 0

*set copper 434
*gosub counter
amount of copper = {copper}
amount of silver = {silver}
*label counter
*if copper >= 100
    *set copper - 100
    *set silver + 1
    *if copper >= 100
        *goto counter
    *else
        *return

I will happily delete this if I’m wrong.

*create copper 0
*create silver 0
*create gold 0

*set copper 4340
*gosub counter
amount of copper = {copper}
amount of silver = {silver}
amount of gold = {gold}
*label counter
*if copper >= 100
    *set copper - 100
    *set silver + 1
    *if copper >= 100
        *goto counter
*if silver >= 100
    *set silver - 100
    *set gold + 1
    *if silver >= 100
        *goto counter
*return

And this one?
one for copper, silver, and gold

6 Likes

I think @UmbraLamia has the right idea, and then just put in a silver to gold converter in the same vein, if necessary.
But yes, something on a loop.

1 Like

@UmbraLamia, I’ve given it a try but I can’t get it to work. :disappointed:

I think it just needs the addition of $ before the brackets here:

Instead, if you use:


amount of copper = ${copper}
amount of silver = ${silver}
amount of gold = ${gold}

That should show the amounts properly. Otherwise it looks fine to me; I tested it and everything ran correctly.

I coded something similar for one of my WiPs, only with old British money so the monetary unit divisions are not based on 10s. I used *elseif instead…here it is too, for variety, if it’s helpful. (l is pounds, s is shillings, d is pence. I put this in its own scene and used *gosub_scene whenever a character earned or lost money.)


*label top
*if (s >= 20)
  *set l +1
  *set s -20
  *goto top
*elseif (d >= 12)
  *set d -12
  *set s +1
  *goto top
*else
  *goto done
*label done
*return
1 Like

As mentioned, the modulo operator can make this quite easy. Here’s a simplistic example I posted in a similar thread: New game - code advice regarding Wealth/Currency

4 Likes

I seem to remember reading somewhere that with the current version of choicescript, rather than using the % symbol (as in the example you linked—which is a really nifty example too, thank you), one has to use the word ‘modulo’ instead. Is that right?

2 Likes

That appears correct. It seems to have changed in mid October, 2016.

3 Likes

Excellent, memory. Yes, modulo is definitely now a thing. I believe % still works but is being deprecated.

1 Like

% is actually now illegal in final production (due to the original problem of it’s as often as not a mistaken %- or %+). I believe it was left working in the public version so as to not surprise break compatibility.

3 Likes

It sounds like you’re not actually concerned about having separate pools of copper, silver, and gold, and just want it to look like that. That’s relatively easy.

In startup:

*create copper 

(Yea, track it all in copper.)

In any place you display coins:

*label coin_display_subroutine
*temp copper_display copper
*temp silver
*temp gold
*label loop_coins
*if copper_display > 100000
  *set copper_display -100000
  *set gold + 10
  *goto loop_coins
*if copper_display > 10000
  *set copper_display -10000
  *set gold + 1
  *goto loop_coins
*if copper_display > 1000
  *set copper_display - 1000
  *set silver +10
  *goto loop_coins
*if copper_display > 100
  *set copper_display - 100
  *set silver + 1
  *goto loop_coins
*return

Or, something like that.

4 Likes

Thanks for the heads up, I’ve edited the post accordingly :slight_smile: