How to make a PlayerPrefs Boolean Array?

I am stuck on this. I am using

public classPlayerPrefsX
{
publicstaticvoidSetBool(stringname, boolbooleanValue)
{
PlayerPrefs.SetInt(name, booleanValue ? 1 : 0);
}

publicstaticboolGetBool(stringname)
{
returnPlayerPrefs.GetInt(name) == 1 ? true : false;
}

publicstaticboolGetBool(stringname, booldefaultValue)
{
if(PlayerPrefs.HasKey(name))
{
returnGetBool(name);
}

returndefaultValue;
}

}

in order to set my booleans,

and was planning on doing this

bool[ ] array = {PlayerPrefsX.GetBool(“string”,false )…}; making array a global Variable

however “GetInt can only be called from the main thread.
Constructors and field initializers will be executed from the loading thread when loading a scene.”

so then i tried

bool[] array  = new bool[somenumber];

awake()
{
  array[0] = PlayerPrefsX.GetBool("string",false);
  ...
}

but I am not sure why I get array out of index when i get to number 2.

I found this interesting.

http://wiki.unity3d.com/index.php/ArrayPrefs2

however I don’t understand how to use their GetBoolArray. I was trying

awake()
{
   PlayerPrefsX.GetBoolArray (String key, bool defaultValue, int defaultSize);
and then going PlayerPrefsX.GetBoolArray(key)[0];
}

But I don’t think that works since my array I created didn’t even have an index of 1;

Any Ideas would be greatly appreciated.

I’m not 100% sure what you are trying to do here with the code you posted, but if you want to save an array of data into PlayerPrefs, why not just use for loops? Use 0 as false and 1 as true for boolean.

for(int i = 0; i < arrayLength; i++)
{
   PlayerPrefs.SetInt("MyBooleanName" + i, myValue);
}

This is the method I am personally using to save arrays of data into PlayerPrefs, it is simple and clean (probably not the most efficient, but hey, it works).

Hope this helps. :slight_smile:

2 Likes

Please use code tags. Otherwise most of the people won’t bother trying to read the code :stuck_out_tongue:

If you need to save an array of booleans, either try to convert them into an int (32 booleans per integer) or if you need way more, consider building a string. Do not save every single boolean as an integer.

1 Like

Awesome will try and tell you how it worked. Thanks!

Will fix haha I did not know how to use them until now :stuck_out_tongue:

So I appears to work well in creating the PlayerPrefs, however how can i store it into a global array now? I need a global array because when I want to reference one of the playerPrefs i pass in the value the player is on. fir example array[valuePlayerIsOn];
If you could help me trough that I think my problem will be fixed :slight_smile:

No, GetBoolArray simply returns a boolean array, nothing more complicated than that. “var boolArray = PlayerPrefsX.GetBoolArray (…)”.

–Eric

But if I want to use the bool array, how do I specify which value I’m referring to? Normally its just array[number]

Right, that’s how you use arrays. Nothing is different; GetBoolArray returns a boolean array. “boolArray[0]” is the first entry.

–Eric

1 Like

So Thanks to everyone that tried to help! What I ended up doing was using the for loop Pinsukka gave me and when wanting to reference a player prefab Just Type PlayerPrefs.GetInt(“MyBooleanName” + reference to the number value the player is on). Since the for loop creates playerPrefabs with the name MyBooleanName0, MyBooleanName1, etc .

Thanks again!

oh okay well I already did it differently, but thanks! I’ll try this way next time.