I’m trying to store the number of bitcoins players have in my game, so its a lot of decimals (0.000000). I don’t know if I need to split the decimal into 2 ints then put it back together or if you can simply save it using a different method. I am saving it using XML.
public class SaveState
{
public decimal bitcoin = Click.btcoin;
}
You can’t save decimal
numbers.
So instead you will have to save it as a string.
Use decimal.tryParse()
and myDecimal.toString()
You can use decimal.parse()
if you are sure you will not get an exception.
You can writer a wrapper:
public static class PlayerPrefsWrapper
{
public static void SetDecimal(string key, decimal value)
{
PlayerPrefs.SetString(key, x.ToString());
}
public static decimal GetDecimal(string key)
{
decimal value;
return PlayerPrefs.HasKey(key) && Decimal.TryParse(PlayerPrefs.GetString(key), out value) ? value : 0;
}
}
then in your savestate class you just have to do:
Set:
PlayerPrefsWrapper.SetDecimal("bitcoin", bitcoin);
Get:
bitcoin = PlayerPrefsWrapper.GetDecimal("bitcoin");