1. Hey! Guest! The 36th GMC Jam will take place between February 27th, 12:00 UTC - March 2nd, 12:00 UTC. Why not join in! Click here to find out more!
    Dismiss Notice
  2. NOTICE: We will be applying a Xenforo update on Tuesday 25th of February. This means that from approximately 10:00 to 14:00 BST the forums will be offline (or possibly longer). Sorry for the inconvenience! Official Announcement here.

GMS 2 Currency Logic

Discussion in 'Programming' started by Dividious, Jan 22, 2020.

  1. Dividious

    Dividious Member

    Joined:
    Sep 16, 2018
    Posts:
    11
    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...

    much thanks in advance...
     
  2. Desert Dog

    Desert Dog Member

    Joined:
    Jul 30, 2019
    Posts:
    71
    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.
     
  3. chamaeleon

    chamaeleon Member

    Joined:
    Jun 21, 2016
    Posts:
    1,107
    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.
     
    SnotWaffle Studios and Dividious like this.
  4. TheouAegis

    TheouAegis Member

    Joined:
    Jul 3, 2016
    Posts:
    7,449
    ...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;
    
     
    Dividious likes this.
  5. Catastrophe

    Catastrophe Member

    Joined:
    Sep 22, 2019
    Posts:
    265
    Last edited: Jan 22, 2020
    Dividious likes this.
  6. chamaeleon

    chamaeleon Member

    Joined:
    Jun 21, 2016
    Posts:
    1,107
    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.
     
    Dividious likes this.
  7. Yal

    Yal GMC Memer GMC Elder

    Joined:
    Jun 20, 2016
    Posts:
    4,214
    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.
     
    Dividious likes this.
  8. chamaeleon

    chamaeleon Member

    Joined:
    Jun 21, 2016
    Posts:
    1,107
    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 likes this.
  9. Dividious

    Dividious Member

    Joined:
    Sep 16, 2018
    Posts:
    11
    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

     
  10. CloseRange

    CloseRange Member

    Joined:
    Jul 2, 2016
    Posts:
    1,008
    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: Jan 23, 2020
    Dividious likes this.
  11. Catastrophe

    Catastrophe Member

    Joined:
    Sep 22, 2019
    Posts:
    265
    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 likes this.
  12. Dividious

    Dividious Member

    Joined:
    Sep 16, 2018
    Posts:
    11
    All of ya'll are great! I appreciate the insight!
     
  13. TheouAegis

    TheouAegis Member

    Joined:
    Jul 3, 2016
    Posts:
    7,449
    You should also only be running that when you actually add money to the player. Make sure you're not calling it every step.
     
    Dividious likes this.
  14. FrostyCat

    FrostyCat Member

    Joined:
    Jun 26, 2016
    Posts:
    4,878
    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;
    
     

Share This Page