Multiple in game currencies
I've written a very basic currency script, but I'm wondering how to make it work if I add other currency types. What I mean is, if I have droppable items for coins and for gems, with both of them calling AddCurrency() on pickup, how could I know which currency was picked up?
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class CurrencyController : MonoBehaviour {
public int coins;
public int gems;
public Text coinsDisplay;
void Awake ()
{
UpdateCurrencyUI ();
}
void Update ()
{
AddCurrency (10);
}
void AddCurrency (int amount)
{
coins += amount;
Debug.Log ("You now have " + coins.ToString() + " coins!");
UpdateCurrencyUI ();
}
void RemoveCurrency (int amount)
{
coins -= amount;
UpdateCurrencyUI ();
}
void UpdateCurrencyUI ()
{
coinsDisplay.text = coins.ToString();
}
}
Answer by nathanlink169 · Jun 27, 2017 at 10:08 PM
There are countless of ways to do it: The way that I would personally do it is to create an enum of currency types, and pass in that currency type into the function. For example:
public enum eCurrencyTypes
{
Money,
Gems,
}
And
public void AddCurrency(eCurrencyTypes in_currencyType, int in_amount)
{
m_Currency[in_currencyType] += in_amount;
}
And
private Dictionary<eCurrencyTypes, int> m_Currency = new Dictionary<eCurrencyTypes, int>();
Note that this code is not tested, so there may be some errors, but it should fundamentally work.
What is the dictionary for? Not used them before. Would this not work with just the enum of types?
A Dictionary is just like an array, but ins$$anonymous$$d of passing in an integer to the array, you pass whatever the first value is. You can learn more about dictionaries here.
Essentially, by creating an enum, you're making it much easier to add currencies in the future: You simply need to add the currency type to the enum, and then make sure to initialize it in start.
I've played around with this.
Debug.Log (eCurrencyTypes.Coins.ToString ());
This shows the string "Coins" in the console. So what is 'int in_amount' actually getting added to? Coins hasn't been referenced as an integer, so how can you add an integer to what appears to just be a string within an enum?
An enum isn't a string, nor does it contain a string: the ToString method merely types out the name that you gave the enum option. You can read more on enums here.
Truth be told, enum's are really just glorified integers. If you hover your mouse over one of the options that the enum has, it will show you its integer value. It's simply a convenient way for you to write some code out.
As for "how you can add an integer to what appears to just be a string within an enum", take a look at the link to dictionaries that I posted above, that should explain that question.
Really appreciate these links and your input, thanks. So the initial int value of the enums will be 0, 1, 2 etc. right? Once the function AddCurrency is called, how do you then reference the enums new value to display as text? Would it be something like
coinsDisplay.text = eCurrency.Coin
The examples in the videos and descriptions are slightly different from what I require, and I'm struggling to get my head around referencing the values of the enums themselves rather than their names
No worries man: I wouldn't know this stuff if somebody didn't take the time to tell me!
Add another function inside your currency class:
public float GetCurrency(eCurrencyType in_currencyType)
{
return m_Currency[in_currencyType];
}
And from there, whenever you want to display it:
coinsDisplay.text = CURRENCY_CONTROLLER_NA$$anonymous$$$$anonymous$$GetCurrency(eCurrencyType.Coin).ToString(); // this will display decimal places
coinsDisplay.text = ((int)(CURRENCY_CONTROLLER_NA$$anonymous$$$$anonymous$$GetCurrency(eCurrencyType.Coin))).ToString(); // this will not display decimal places (because you are changing the type to an integer, which doesn't support decimal places.
You can learn more about typecasting (or changing from one type to another) here.
That makes much more sense now. I will try to put this together tonight.
Also, when I call AddCurrency (), it will take 2 arguments right? So something like
AddCurrency (eCurrencyTypes.Coins, 10)
You are a god send :) Never touched dictionaries or enums, but now I have a base on which to learn and start using them efficiently! Thank you very much
No worries man - like I said before, I wouldn't know this stuff if it wasn't for someone $$anonymous$$ching me.
Good learning!