Gold, Silver Bronze Money system. Need help removing coins...

I want to use the “Gold, Silver Bronze” money system in my game. My addCoins function works as I want it to… I’m having trouble thou with my removeCoins function… Is there anybody who can help me out with it? :frowning:

	public void addCoins(int _coins)
	{
		bronzeCoins += _coins;
		if(bronzeCoins > 999)
		{
			silverCoins += bronzeCoins / 1000;
			bronzeCoins = 0;
			if(silverCoins > 99)
			{
				goldCoins += silverCoins / 100;
				silverCoins = 0;
			}
		}
		Debug.Log("Gold: " + goldCoins + " Silver: " + silverCoins + " Bronze: " + bronzeCoins);
	}
	
	public void removeCoins(int _coins)
	{
		Debug.Log("Gold: " + goldCoins + " Silver: " + silverCoins + " Bronze: " + bronzeCoins);
	}

Hi !

I’m not sure what I’m going to tell you is what you want but in fact if I want to use a system of Gold,silver,bronze coin in my game I think i would use just an integer.

For exemple WOW system :

100 bronze coins = 1 silver
100 silver coins = 1 gold

so in fact having 1 gold is same as having 1 00 00 bronze coins
When you display how many you have on your screen (or in debug log) maybe you just have to enter your number in a table (for exemple) and use the two last field of your table as bronze coin, the two before as your silver coin and firsts fields as gold coins.

Does it help you ?

Did some research and came up with this solution:

	public void addCoins(int _coins)
	{
		coins += _coins;
		calculateCoins(coins);
	}
	
	public void removeCoins(int _coins)
	{
		int oldC = coins;
		coins -= _coins;
		if(coins < 0)
		{
			coins = oldC;
		}
		calculateCoins(coins);
	}
	

	
	public void calculateCoins(int _coins)
	{
		int g;
		int s;
		int c;
		
		c = _coins % 100;
		_coins = (_coins - c) / 100;
		s = _coins % 100;
		g = (_coins - s) / 100;
		
		goldCoins = g;
		silverCoins = g;
		bronzeCoins = c;
		
		Debug.Log("GOLD: " + g + " SILVER: " + s + " COPPER: " + c + "   COINS: " + coins);
	}

The only thing I want to do now is to make “calculateCoins” return the values…

This code gives me an error:

	public int calculateCoins(int _coins)
	{
		int g;
		int s;
		int c;
		
		int[] result = new int[3];
		
		c = _coins % 100;
		_coins = (_coins - c) / 100;
		s = _coins % 100;
		g = (_coins - s) / 100;
		
		goldCoins = g;
		silverCoins = g;
		bronzeCoins = c;
		
		result[0] = c;
		result[1] = s;
		result[2] = g;
		
		return result;
		
		Debug.Log("GOLD: " + g + " SILVER: " + s + " COPPER: " + c + "   COINS: " + coins);
	}

Error: “Cannot implicitly convert type int[ ]' to int’”

Oh ! of course !

this is your error, result is not a int but a int[ ]

or you can use something like that

// variable coin represents how many coin you have
// let's put coin at 55230

float gold;
float silver;
float copper;

int g;
int s;
int c;

if (coin != 0)
{
gold = coin /10000; g = (int) gold;   // 55230 / 10000 = 5.5230    gold = 5.230     g = 5
silver = (coin - (g * 10000))/100; s = (int) silver;   // (55230 - ( 5 * 10000) )/100 = 52       silver = 52,30     s=52 
copper = coin - (g * 10000) - (s * 100) ; c = (int) copper;   // 55230 - (5*10000) - (52 * 100) = 30    copper = 30  c=30
}
else
{
g=0;
s=0;
c=0;
}
debug.log ("I have " + g + " gold, " + s + " silver, " + c + " copper");
// I have 5 gold 52 silver 30 copper

It works perfectly for me (i attached the script on a random object in my scene)

using UnityEngine;
using System.Collections;

public class Gold : MonoBehaviour {
public int coin = 55230;

	void Start () 
	{
     
	    float gold;
	    float silver;
	    float copper;
	     
	    int g;
	    int s;
	    int c;
	     
	    if (coin != 0)
	    {
		    gold = coin /10000; g = (int) gold;   
		    silver = (coin - (g * 10000))/100; s = (int) silver;   
		    copper = coin - (g * 10000) - (s * 100) ; c = (int) copper;   
	    }
	    else
	    {
		    g=0;
		    s=0;
		    c=0;
	    }
	    Debug.Log ("I have " + g + " gold, " + s + " silver, " + c + " copper");
	
	}
	
}
1 Like

I’m not by my computer right now, but it should be “public int[ ] calculateCoins(int _coins)” right?

To get the gold do I write, ie: “Debug.Log(calculateCoins[2]);”??

yes I think what you wrote should work.
Oh last thing, I think you should not insert code line after a “return”, because when a compiler read a “return” it doesn’t read what you have after

// _coins = 55320 for example
c = _coins % 100;   // c = 553,2 and it's a FLOAT not a INT

 _coins = (_coins - c) / 100;    //_coins = (55320 - 553.2) / 100  = 547.668 ?????

s = _coins % 100;   // s = 547.668 % 100 = 5.47668 ..... etc....

g = (_coins - s) / 100;

I don’t think this part of the code will works…

I recommend you to use this kind of code :

gold = coin /10000; g = (int) gold;  

            silver = (coin - (g * 10000))/100; s = (int) silver;  

            copper = coin - (g * 10000) - (s * 100) ; c = (int) copper;

Why are there floats there at all? You’re dealing in discrete quantities of coins. Keeping things to int makes it simpler:

private int[] CalculateCoins ( int total )
{
  int[] results = new int[3];
  results[0] = total % 100;  // Copper
  total /= 100;  // Divide the total, so it now represents silver
  results[1] = total % 100;  // Silver
  results[2] = total / 100;  // Gold
  return results;
}

Or to make it more general:

private int[] CalculateCoins ( int total, int numTypes, int step = 100 )
{
  int[] results = new int[numTypes];
  for(int i = 0; i < numTypes - 1; i++)
  {
    results[i] = total % step;
    total /= step;
  }
  results[numTypes-1] = total;
  return results;
}

iam courius if this " project" working or is it finisched ?
scripts available ?

I actually would prefer Properties, as Gold and Silver are easily derived values and don’t need to be stored on their own. Also, I’ve never liked magic arrays, where you mentally associate an index with a variable (gold =0, silver =1, copper =2). Not to difficult in the case of currency, but a practice I discourage in general.

private int _coins;

public int Gold {get {return Silver/100;} } //or _coins/100000
public int Silver {get {return _coins/1000;}}
public int Copper {get { return _coins; }}

If you don’t do the property route that’s fine (especially since it could force you up to a float if you have a LOT of gold), but I recommend your own currency data structure instead of a magical int[3]. I REALLY hate magical arrays if you can avoid them.

Yes, definitely do not store separate values of gold/bronze/silver whatever when in fact they are just handy denominations, powers of 10 or 100 in your case.

As suggested multiple times above, (including by the OP), keep a single value of your riches, and then display it however you like, by a modulo-and-devide process.

Make like this

    public class WafclastCoins
    {
        private const ulong SilverInCopper = 100;
        private const ulong GoldInCopper = SilverInCopper * 100;
        private const ulong PlatinumInCopper = GoldInCopper * 100;

        private ulong Coins { get; set; }

        public WafclastCoins() => Coins = 0;
        public WafclastCoins(ulong startCoins) => Coins = startCoins;

        public void Add(ulong gold, ulong silver, ulong copper)
        {
            Coins += gold * GoldInCopper;
            Coins += silver * SilverInCopper;
            Coins += copper;
        }

        public ulong GetCopper()
        {
            return Coins % SilverInCopper;
        }
        public ulong GetSilver()
        {
            return Coins % GoldInCopper / SilverInCopper;
        }
        public ulong GetGold()
        {
            return Coins / GoldInCopper;
        }

        public static ulong GetCopper(ulong baseDenomination)
        {
            return baseDenomination % SilverInCopper;
        }
        public static ulong GetSilver(ulong baseDenomination)
        {
            return baseDenomination % GoldInCopper / SilverInCopper;
        }
        public static ulong GetGold(ulong coins)
        {
            return coins / GoldInCopper;
        }