-4

I'm working on an idle clicker game and all seems perfect until the money value hits over 2billion, then the value changes to negative.. It seems unity playerpref have an issue with values over 2,154,000,000...how can I fix this, Below code saves and loads the score..

public int LoadCoinsAmount()
{
    return PlayerPrefs.GetInt("COINS");
}

public void SaveCoinsAmount(int coins)
{
    PlayerPrefs.SetInt("COINS", coins);
}

..Please help

  • Please, read https://stackoverflow.com/help/how-to-ask before asking a question. – Rarblack Oct 09 '18 at 11:36
  • This is a math overflow, you are using a int as variable and his max value is 2,147,483,647. change it to double or float to represent higher numbers. – AlbertoCh Oct 09 '18 at 11:36
  • 2
    @AlbertoCh The obvious choice would be the next widest **integral** type (`long`), not a switch to IEEE floating point numbers. – spender Oct 09 '18 at 11:37
  • in this case you could even go for a `System.Numerics.BigInteger` – nalka Oct 09 '18 at 11:41
  • 2
    All these suggestions are great, but without confirmation from the OP, they are all speculation. Please provide details of the upper bound of your "money value", then you won't get a bunch of confusing and conflicting suggestions. – spender Oct 09 '18 at 11:44
  • Ive added the code i used to save the playerpref.. – Sirwhite2015 Oct 09 '18 at 13:34

2 Answers2

3

Although saving to floats is an option as mentioned by others, I would suggest using the appropriate type which is long in this case. A long has higher precision.

To implement:

public long LoadCoinsAmount()
{
    string coinString = PlayerPrefs.GetString("COINS");
    if (long.TryParse(coinString, out long result))
    {
        return result;
    }

    return 0;
}

public void SaveCoinsAmount(long coins)
{
    PlayerPrefs.SetString("COINS", coins.ToString());
}
Immorality
  • 2,164
  • 2
  • 12
  • 24
-2

As I commented you, you should change your variable type, in this case to float which is the only one supported in Unity to save in PlayerPrefs. So your code would be like:

public float LoadCoinsAmount()
{
    return PlayerPrefs.GetFloat("COINS");
}

public void SaveCoinsAmount(float coins)
{
    PlayerPrefs.SetFloat("COINS", coins);
}
AlbertoCh
  • 120
  • 2
  • 6
  • Yo.. I must say ..You are Awesome.. it worked like a magic.. Thanks – Sirwhite2015 Oct 09 '18 at 17:23
  • 1
    According to [here](https://stackoverflow.com/a/23031245/342842) using floats would **reduce** the available precision (31 bit -> 24 bit). String-encoded longs would be _much_ better (64 bit), especially in the suggested context of a clicker (unit) counter. – Immersive Oct 10 '18 at 04:40
  • Fun game of the day: Will this loop ever complete? `for (float i = 16777216f; i < 16777220f; i++) { Console.WriteLine(i.ToString("R")); }` Do you understand why? – spender Oct 10 '18 at 12:02
  • @Sirwhite2015 If `16777216f == 16777217f` (it does), do you think it's wise to proceed with this as a fix to your problem? – spender Oct 10 '18 at 12:05