- Home /
Create a list of all PlayPref Keys
Hi there, basically we've created a character creator system that used PlayerPref keys to store the character configuration. I want to create a character select screen but using all the string names of each of the saved characters names. Is there a way I can create a array from all the PlayerPref keys?
Answer by Anxo · Jun 17, 2011 at 12:09 PM
good question.
I would also like the answer to this. This would be my first approach but seams like a headache.
Instead of storing it by name, I would store them by number, and every piece of equipment also. So Hat might be stored in 1, boots in 2 and so on. Then the type of boots would also be a number like black is 1 and blue is 2. Lost yet? Then the first stored character could look like this:
PlayerPrefs.SetString("1","0202");
Then you could write an algorithm that decodes that the first 2 indexes in the string tell you what kind of hat he/she has on and the second 2 indexses tell you what kind of boots he/she has on. Blue Blue. That would allow you to store lots of characters and their setups without storing thousands of variables.
And Storing it by number would allow you to sweep through it using a for loop.
function CheckForCharacter(){
for(var i=0; i<1000; i++)
{
if(PlayerPrefs.HasKey(i))
{
do something
}
else
{
return;
}
}
}
What do you think?
Almost the way in which we've already set it up, but I'v given each a string variable name so I can call in using a "player name" I think the array solution Is probably the way to go, but thanks!
Answer by Twayne · Jun 17, 2011 at 12:23 PM
I use ArrayPrefs. You can load the prefs as an array and save the prefs as an array directly.
It's the best workaround at the moment ;)
Just to have it said: It's not possible to get a list of all stored PlayerPrefs (yet).
ArrayPrefs are great, the only problem is that if you are creating a mobile game, ArrayPrefs is very heavy.
I like the sound of this, I will try it and get back to you, Although apart form using the PlayerPref functions what would be a less CPU heavy alternative to storing character creation data?
I believe this is less heavy than separate string playerprefs. The array ends up being stored as a single string pref with separators. The CPU cost is small because, i think, you would only save when the user clicks DONE or APPLY. Then unity builds the string from the array of strings and saves it to prefs.
Okay here comes the question:
string[] names = PlayerPrefsX.GetStringArray("Names");
for (int i = 0; i < names.Length; i++)
names[i] = prefName+(i+1);
if (!PlayerPrefsX.SetStringArray("Names", names))
print("Can't save names");
How do I add a prefName to an existing string of PrefNames?
Your answer
Follow this Question
Related Questions
Store individual positions of GameObjects in a string 3 Answers
Using Player Prefs 0 Answers
If Statements with PlayerPrefs 1 Answer
Display PlayerPrefs.GetInt? 1 Answer
How to get Steam Username? 1 Answer