- Home /
How can I store a number larger than 2147483647 in PlayerPrefs?
I recently discovered that Unity refuses to save an int larger than 2,147,483,647 in PlayerPrefs. The reason why this occurs is well-documented, but what is a workaround for this problem?
I need to store a number that can become incredibly large - indeed, larger than two billion - in PlayerPrefs. How would I go about doing this?
@Jamora's suggestion of converting the value to/from a string is probably the best one. For the sake of completeness, here is another solution. You could also convert a long/ulong into two integers and then put them back together. So you could save two integers in PlayerPrefs:
ulong l = 335147483647;
void Start () {
Debug.Log (l);
int iHigh = (int)((l & 0xFFFFFFFF00000000) >> 32);
int iLow = (int)(l & 0x00000000FFFFFFFF);
Debug.Log (iHigh+","+iLow);
l = ((ulong)iHigh << 32) | (ulong)iLow;
Debug.Log(l);
}
The only problem with splitting into two integers is that you need to save both of them separately using two keys, which can potentially be problematic to deal with.
@Eric5h5 - Yea. The only benefit is size, and only then if you had enough entries to push the 1 meg limit.
Answer by Jamora · Aug 31, 2013 at 01:07 AM
You can store your large number as a string and then parse it into a long - or whatever type you are using - when needed.
Both Jamora and meat5000 suggested a great solution! (Too bad I can't flag both of them as having the correct answer!)
Here it is in practice:
import System.Convert;
var $$anonymous$$yLongNumber : long;
function Update()
{
if (Input.Get$$anonymous$$eyDown("1"))
{
$$anonymous$$yLongNumber++;
PlayerPrefs.SetString("SavedNumber", "" + $$anonymous$$yLongNumber);
}
if (Input.Get$$anonymous$$eyDown("2"))
{
Debug.Log(GetNumber());
}
}
function GetNumber()
{
return System.Convert.ToInt64(PlayerPrefs.GetString("SavedNumber"));
}
Thank you so much for your help!
Answer by Eric5h5 · Aug 31, 2013 at 01:44 AM
You could use the technique in ArrayPrefs2, namely converting the bytes of the long to a base64 string. This has the advantage of obfuscating the actual value to some extent, if you care about that, as well as being somewhat more compact than a straight ToString() conversion for very large numbers (but less compact for small numbers).
function Start () {
var x : long = 123456789123456;
SetLong("foo", x);
var y = GetLong("foo");
Debug.Log (y);
}
function SetLong (key : String, x : long) {
var bytes = System.BitConverter.GetBytes(x);
PlayerPrefs.SetString(key, System.Convert.ToBase64String(bytes));
}
function GetLong (key : String) : long {
var bytes = System.Convert.FromBase64String(PlayerPrefs.GetString(key));
return System.BitConverter.ToInt64(bytes, 0);
}
Answer by meat5000 · Aug 31, 2013 at 12:58 AM
Tried declaring a long instead of an int?
Look under Predefined Types in this link
Your answer
Follow this Question
Related Questions
Using Player Prefs for a Leaderboard 1 Answer
Convert class of int vars into an array 0 Answers
PlayerPrefs / RegEdit 1 Answer
Store Current Level Using Preferances? 1 Answer
Converting from an Int to a Float and back with PlayerPrefs 1 Answer