PlayerPrefs, i'm quite confused with it.

Someone gave me the basic code for it. I'm just really confused of how it works. and if I can use it for storing data from Different Users(multiusers)?

I actually need to have a Load game menu, which will ask for the users name, and then load it's level (if the player have played before)

And can i use this as well for displaying the High scores?

When i set it's initial value, how can i change it? and how can i store data/string (name) from a GUI textbox?

I hope someone can teach me about playerPrefs. Please do enlighten me about Using PlayerPrefs.. And sorry for being such a noob.. maybe if i get how PlayerPrefs work, i can be enlightened with my confusions stated above.

UnityAnswers have been a great help to me, Thanks to people who are kind enough to entertain my noob questions.

i tried it, i just want to check whether the entered user was saved.but it doesn't give the right output.

var textFieldString="";
    var check="";

function OnGUI () { textFieldString=GUI.TextField(Rect(25,25,100,30),textFieldString);

`if(GUI.Button(Rect(25,55,100,30),“Save”)) {
saveUser();
}

check=GUI.TextField(Rect(25,95,100,30),check);

if(GUI.Button(Rect(25,120,100,30),“Check”)) {
search();
}

}

function saveUser(){
PlayerPrefs.SetString(“Player Name”, textFieldString);
print(“saved”);
}

function search() {
if(PlayerPrefs.HasKey(check)) {
print(“He played”);
}
else print(“new User”);
}
`

1 Answer

1

Well, the basic functionality of playerPrefs is pretty straight-forward as it only allows you to set or retrieve an int, float or string or check for a key.

http://unity3d.com/support/documentation/ScriptReference/PlayerPrefs.html

The simplest (and least efficient) way to do what you want is to get the user's name and check against it before loading a new level. So, to save a new username just let him input it and set a playerPrefs entry using it:

var username: String;

function getName(){
        // do your input magic here
        saveLevel();
        }

function saveLevel(){
    PlayerPrefs.SetInt(username+"level", Application.loadedLevel);
    }

To check against a name and load it is equally easy:

var username: String;

function getUsername(){
    //get input
    loadLevel();
}

function loadLevel(){
    if(PlayerPrefs.HasKey(username+"level")){
    Application.LoadLevel(PlayerPrefs.GetInt(username+"level"));
    }   
}

Word of advice: while this simplistic way of doing things will certainly work, it's not really suitable for storing and retrieving a large number of variables. If you have to save more than say highscores and level ints then you should look into ArrayPrefs or even an xml saving/ loading scheme.

Your code above is mostly correct. Here's the fixed up version:

var textFieldString="";
    var check="";

function OnGUI ()  {
    textFieldString=GUI.TextField(Rect(25,25,100,30),textFieldString);

if(GUI.Button(Rect(25,55,100,30),"Save")) {
saveUser();
}

check=GUI.TextField(Rect(25,95,100,30),check);

if(GUI.Button(Rect(25,120,100,30),"Check")) {
search();
}

}

function saveUser(){
PlayerPrefs.SetString("PlayerName", textFieldString);
print("saved");
}

function search() {
 if(PlayerPrefs.HasKey("PlayerName")) { 
    if(PlayerPrefs.GetString("PlayerName") == check){
    print("He played");
 }  
 }
 else print("new User");
 }

Just make sure you also check if the key's value is equal to your "check" input.

thanks for the answer.made it a little clearer. but still got some confusions. I tried it, posted the code above. i tried saving a user and checking whether it has been saved.i didn't get the right output :( what's wrong with it?

You were pretty close. I've fixed it for you above.

oh yeah!thanks!i think i'm getting how it works.. when i enter another player name, will the old one be retrieved? where do it saves the new one? thanks for the great help!!!

You should check the link I provided. Unity stores playerPrefs in various locations depending on your pc/ device. When you enter another playername it will overwrite this one. That's why my example saves to a variable playerprefs key ("username"). Any one key can only hold one value unless you use array prefs (so playerName can only hold azzogat f.e). Setting another name to playerName will overwrite azzogat.

aww..i see..i guess i have to learn array prefs as well.. because i need to have to hold atleast 30 players in same computers. Is it far from playerprefs?Will it be hard to search for the name when i use that?thanks really!!hope you won't get tired in answering me.take care!