# GMS 2Currency Logic

#### Dividious

##### Member
Maybe I've burnt myself out a bit... i'm having the hardest time to make this logic work...

100 copper = 1 silver
100 silver = 1 gold

so 10,000 copper = 1 gold
also 145 copper = 1 silver 45 copper

when copper reaches 100 to reset the copper variable and increase silver by 1
when silver reaches 100 to reset the silver variable and increase gold by 1

Think like Graveyard Keeper

was gonna put what i had, but rereading what i had just sounds stupid.. and i'm tired..irritated.. could i get a little nudge or help.. i'm probably overthinking this...

#### Desert Dog

##### Member
Sounds good to me. What's the problem?

Multiple ways to skin a cat. One way is just to keep a count of only one variable (copper). So you would count the player has 10,135 copper coins.

Then just when you display the money, you draw it however you like. E.g. 1 gold, 1 silver, 35 copper.

This way is easier because you only ever have one 'value' you deal with for game logic/calculations.

#### chamaeleon

##### Member
As @Desert Dog says, just use one variable holding the count of the lowest value coin, copper. Then use mod and div to get the number of coins of the various types from that. It should be fairly simple, and I recommend reading about the operators in the manual.

#### TheouAegis

##### Member
...Unless your game suffers from inflation, in which case your copper variable could max out before you know it. For keeping track of 3 variable, make 3 additives.

Code:
``````var i = 0;
repeat 3 coin[i++] += gains[i];
i = coin[0] div 100;
coin[1] += i;
coin[0] -= i;
i = coin[1] div 100;
coin[2] += i;
coin[1] -= i;``````

#### chamaeleon

##### Member
...Unless your game suffers from inflation, in which case your copper variable could max out before you know it. For keeping track of 3 variable, make 3 additives.

Code:
``````var i = 0;
repeat 3 coin[i++] += gains[i];
i = coin[0] div 100;
coin[1] += i;
coin[0] -= i;
i = coin[1] div 100;
coin[2] += i;
coin[1] -= i;``````
Unless it's a game like SwarmSim, I would think counts getting too high wouldn't be an issue..
Code:
``````var money = 10145;
var gold = money div 10000;
var silver = (money mod 10000) div 100;
var copper = money mod 100;``````
And just add or subtract appropriate amounts from money and use the gold, silver and copper calculations for display purposes only.

#### Yal

##### GMC Memer
GMC Elder
Since GM only starts having issues once you're above 2^32, you should be fine if your max allowed money amount is below 4,294,967,296. So if you limit money to 999,999,999 units a single variable should be enough. Or put another way, 999,99,99,99 monies: 999 platinum, 99 gold, 99 silver, 99 copper.

#### chamaeleon

##### Member
Since GM only starts having issues once you're above 2^32, you should be fine if your max allowed money amount is below 4,294,967,296. So if you limit money to 999,999,999 units a single variable should be enough. Or put another way, 999,99,99,99 monies: 999 platinum, 99 gold, 99 silver, 99 copper.
Or go higher by using the int64 data type..
Code:
``````var money = 1 << 60;
show_debug_message(money);
show_debug_message(money div int64(100));
show_debug_message(money div int64(10000));
show_debug_message(money div int64(1000000));
show_debug_message(money div int64(100000000));
show_debug_message(money div int64(10000000000));
show_debug_message(money div int64(1000000000000));
show_debug_message(money mod int64(100));
show_debug_message(money mod int64(10000));
show_debug_message(money mod int64(1000000));
show_debug_message(money mod int64(100000000));
show_debug_message(money mod int64(10000000000));
show_debug_message(money mod int64(1000000000000));``````
Code:
``````1152921504606846976
11529215046068469
115292150460684
1152921504606
11529215046
115292150
1152921
76
6976
846976
6846976
4606846976
504606846976``````
Of course, this should probably not be an issue for most monetary situations in a game (but I'm not an expert on that..)

#### Dividious

##### Member
Tried one of the methods from last night... this is the result...I just dont know if its simple enough and not logically messy or lacking.. A good night sleep was most needed (it rained here) but I dunno it just seems off...

Code:
``````while (silver_coins >= 100)        //If Silver Coin total is more than it's max
{
silver_coins -= 100;            //Minus the 100 Silver Coins
gold_coins++;                    //Generate 1 Gold Coin
}
while (copper_coins >= 100)        //If Copper Coin total is more than it's max
{
copper_coins -= 100;            //Minus the 100 Copper Coins
silver_coins++;                //Generate 1 Silver Coin
}
copper_coins = copper_coins;    //Copper Coins are generic and value increases with quantity``````

#### CloseRange

##### Member
Yeah everything's fine, well I mean
Code:
``copper_coins = copper_coins;    //Copper Coins are generic and value increases with quantity``
that's a bit redundant. It does nothing but it also won't hurt anything to have it there, just might confuse people.

i'd maybe flip the 2 while statements so the cooper -> silver gets calculated first.

Last edited:

#### Catastrophe

##### Member
Yeah, as closerange says, you should flip those while loops. Or it can be incorrect for a whole frame (oh no!) if you have 99 silver and get a 100th copper coin xD

#### Dividious

##### Member
All of ya'll are great! I appreciate the insight!

#### TheouAegis

##### Member
You should also only be running that when you actually add money to the player. Make sure you're not calling it every step.

#### FrostyCat

##### Member
Using loops for this purpose is absurd. Basic arithmetic allows you to do this in constant time and far fewer lines.
Code:
``````copper = money mod 100;
silver = (money div 100) mod 100;
gold = money div 10000;``````