Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice

How to create currency?

Discussion in 'Scripting' started by Ch123ad, Feb 20, 2012.

  1. Ch123ad

    Ch123ad

    Joined:
    Jan 15, 2012
    Posts:
    53
    I am super new to unity scripting and I am creating a game. I want to make currency for the game. How could I create a currency system which will let user buy items/earn money
     
    amritsahani likes this.
  2. renman3000

    renman3000

    Joined:
    Nov 7, 2011
    Posts:
    6,697
    var moneyInAccount : float;
    var money : float;


    function earnedMoney()
    {
    moneyInAccount = moneyInAccount + money;
    }


    this is a very simple idea, easily implemented.
     
    rafasulaiman and amritsahani like this.
  3. Ch123ad

    Ch123ad

    Joined:
    Jan 15, 2012
    Posts:
    53
    Thanks :)
     
  4. justinlloyd

    justinlloyd

    Joined:
    Aug 5, 2010
    Posts:
    1,680
    You can also use the .NET Decimal type, which lets you then easily deal with all of those pesky amounts like dollars and cents, pounds and pennies, and so forth. Renman3000 has given a good starting point and is correct, it is very easy to implement.
     
  5. renman3000

    renman3000

    Joined:
    Nov 7, 2011
    Posts:
    6,697
    This is true. I developed a money system my last project. It worked but i had to manually adjust for cents into dollars, if there is something that can help account for hat, even better!

    Good luck!!!
     
  6. jsipich

    jsipich

    Joined:
    Feb 3, 2012
    Posts:
    87
    Limiting redundancies C#/JS:

    Code (csharp):
    1. moneyInAccount += money;
    "+=" goes the same for other operators.

    Also - to automatically have a "money" value show up as X.xx - C# allows you to use "M" after assigning a variable (I haven't done this in Unity tho).

    Code (csharp):
    1. decimal moneyInAccount = 0.00M;
    2. decimal money = 0.00M;
    The "M" literally stands for "Money" haha!
     
    Last edited: Feb 21, 2012
  7. renman3000

    renman3000

    Joined:
    Nov 7, 2011
    Posts:
    6,697
    So in js can i use this?

    And decimal is a var type ( or only in c#?)
     
  8. sheva

    sheva

    Joined:
    Nov 22, 2010
    Posts:
    157
    The best way to make a currency system is to keep your money variable in PlayerPrefs XML file. In this way it won't be change if you turn off Unity or Stop the scene. To Change the variable value you have to use differenet functions like PlayerPresfs.SetInt .
    Check this out :
    http://unity3d.com/support/documentation/ScriptReference/PlayerPrefs.html
    Is very useful.

    Good Luck !

    Sheva
     
  9. jsipich

    jsipich

    Joined:
    Feb 3, 2012
    Posts:
    87
    Indeed. JS can use +=, -=, etc. It's just the shorthand method of using operators for simple math.

    x = x + y;

    Is the same as:

    x += y;

    Whatever is on the left is added to whatever is on the right. Multiply, divide, subtract, etc - all work the same. As for "decimal" and "M" - I'm not too sure about JS. I just remember using them to create a Windoze app in C# to keep track of my paycheck from work... you know... in case they "messed up" somewhere on my check ha!
     
  10. renman3000

    renman3000

    Joined:
    Nov 7, 2011
    Posts:
    6,697
    Yah I am just wondering, what is the difference between

    money = 0.00M
    and
    money = 0.00f


    and I will have to look into it, but you declared the var type as decimal. Never seen that before brah!
     
  11. RingOfStorms

    RingOfStorms

    Joined:
    Oct 23, 2010
    Posts:
    584
    For currency I don't see why it would be so hard to take cents into dollars. You would simply do something as the following.
    This is starting with 2 dollars and 420 cents...
    Code (csharp):
    1.  
    2. public float moneyDollar = 2.0f;
    3. public float moneyCents = 420.0f;
    4.  
    5. void Update () {
    6.     if(moneyCents >= 100) {
    7.         moneyCents -= 100;
    8.         moneyDollar += 1;
    9.     }else{
    10.         return;
    11.     }
    12. }
    13.  
    that would end with 6 dollars and 20 cents.
    Having the floats public would also allow you to add money/take away money through other scripts, like a merchant selling you something.

    Also, if you wanted to display it as $6.20 you could simply use a label like this:
    Code (csharp):
    1.  
    2.     void OnGUI() {
    3.         float moneyDollar = 6.0f;
    4.         float moneyCents = 20.0f;
    5.         GUI.Label(new Rect(10,10,70,50), "Money: $"+moneyDollar+"."+(moneyCents/10));
    6.     }
    7.  
     
    Last edited: Feb 21, 2012
    dlove13 likes this.
  12. npsf3000

    npsf3000

    Joined:
    Sep 19, 2010
    Posts:
    3,830
    Surprised to see people using floats.

    When dealing with money I'd only ever use Decimal or Int64.
     
  13. jsipich

    jsipich

    Joined:
    Feb 3, 2012
    Posts:
    87
    "M" - 2 decimal places.

    I don't believe I've ever used a float - it's accuracy is only limited to 6 numbers. For conventional fractions I use "double" - since it yields a greater accuracy (15, I believe). For currency I always use "decimal" for it's insane accuracy (10 to the -+ 28th) and no rounding problems - however it is much much much slower to use than a standard int.

    Personally, for a game currency - I wouldn't recommend it over "double" - since we're not dealing with literal money so accuracy and decimal notation might not be needed when you can just truncate. Or for the quick route - an int/100 cast as a double will yield a number with 2 decimal places (provided you have a remainder - again why "M" is used to dictate displaying a value with 2 decimals).

    Many ways to tackle "game" currency but the standard accuracy for any type of currency should be of type decimal. It's bigger, slower, but more accurate. All depends on what you want out of your game.

    Personally, I find people like whole numbers better than calculating change. Which is why everyone says "5 bucks" when the quoted price is "4.99". Humans usually refer to finances as literals and not reals.

    Just my 0.02M;
     
    Last edited: Feb 21, 2012
  14. renman3000

    renman3000

    Joined:
    Nov 7, 2011
    Posts:
    6,697
  15. npsf3000

    npsf3000

    Joined:
    Sep 19, 2010
    Posts:
    3,830
    M simply represents the decimal type - not money.

    The decimal type is just a float [like the single and double] except that it differs in three key area's:

    1) Internally it contains a decimal number - so numbers like 0.1 can be represented accurately.

    2) The scale of the range is relatively small but the precision is remarkably high.

    3) Decimal does not have NaN, infinity etc. Small numbers round to zero, large numbers through an exception.

    Float, Double, Int, Double all have different ToString formats - so don't confuse what ToString() returns with the data-structure. For example, M does not have 2 decimal places but about 28. Just a tiny difference :p



    Short demonstration: http://pastebin.com/yW0RNkJv
     
    Last edited: Feb 21, 2012
  16. jsipich

    jsipich

    Joined:
    Feb 3, 2012
    Posts:
    87
    I gotta re-read that chapter in my C# book - it briefly went over the differences but it sounds like it didn't fully sink in ha!