High – Playerpref Value —Playerprefs value turns negative when its over 2billion

Updated on October 6, 2018 in [A] Brackeys Courses
Share on Facebook0Tweet about this on TwitterShare on Google+0Share on Reddit0
5 on October 5, 2018

Hi, guys, I’m working on an idle clicker game and everything seems fine until the earnings value reaches over 2 billion.. Then the value turns negative .. I checked through my codes and all seems fine… It seems unity has a issue with higher playerpref value…

How do I solve this .. Please help…

  • Liked by
Reply
2 on October 5, 2018

Well, the easiest way would be to reduce the number. Is 2 billion really necessary?

The other way would be to implement an exchange rate code, which saves a lower value in the playerPref, but if you load the value it calculates the real higher value. So probably a multiplicator could be enough.

 

on October 6, 2018

Thanks for your response… Its a clicker game and the value could reach as high as trillions…

How do I go about the exchange rate code.. Kindly explain further…

I started using unity few months ago

Helpful
on October 6, 2018

If you use the method from Mouledoux, then you can save your long int numbers as a string in the playerPrefs.

PlayerPrefs.SetString("MyScore", "" + longNumber);

Then if you load the string, you just need to convert it back as an integer. Done.

Show more replies
  • Liked by
Reply
Cancel
1 on October 5, 2018

You’re overflowing the maximum value.

An int’s max value is 2,147,483,647, and if you go over that, it loops back around to negative numbers.

PlayerPrefs doesn’t let you store long long numbers (max value 9,223,372,036,854,775,807)

SO, you’ll need to use something other than PlayerPrefs for saving that number, OR you could maybe store it in chunks of numbers. I’ve seen other clickers do something like every factor of 1000 or so, they just change a suffix.

 

So 1,000 would be stored as 1k

1,000,000,000 would be 1b

99,876,125,244 would be 99.87b, and the other numbers could be stored as 6,125,244 somewhere else off screen.

on October 6, 2018

Thanks for your response… I greatly appreciate it..

What alternative to playerpref can I use..or is it possible to save the value with suffixes like u said??..

Started using unity few months ago so im still new to a lot of things.. Thanks

Show more replies
  • Liked by
Reply
Cancel