Search Unity

Help with PlayerPrefs

Discussion in 'Scripting' started by balu89, Apr 28, 2008.

  1. balu89

    balu89

    Joined:
    Apr 15, 2008
    Posts:
    30
    Hi,
    I need some help with PlayerPrefs pleeeaseee... :(

    My camera have two scripts, I need a way to activate one and desactivate the other from my option's menu (I has 2 GUI.Toggle) and that selection stores like a player preference... Any Idea??

    Thanks... :?
     
  2. seon

    seon

    Joined:
    Jan 10, 2007
    Posts:
    1,441
    On the Camera scripts, in the Start() function add something like....

    if (the variable for this camera is false) {
    gameObject.active = false;
    }

    So the GameObject of the camera will de-activate itself if your variable is not true, or stay on if it is true.

    the problem with using 2 toggles in this case is that you cannot guarantee that 1 or the other is on, unless you check for that specifically in your game options code, so it could be that BOTH cameras get turned off because neither toggle was set to true.
     
  3. balu89

    balu89

    Joined:
    Apr 15, 2008
    Posts:
    30
    Thanks you gave me a good hint to continue, but I have one more question, do you know in what script I have to store the values for playerPrefs?? and how??


    :wink:
     
  4. seon

    seon

    Joined:
    Jan 10, 2007
    Posts:
    1,441
  5. DocSWAB

    DocSWAB

    Joined:
    Aug 28, 2006
    Posts:
    615
    One reason why this is a very good idea is that it prevents the player from editing their prefs on the fly after they've played the game once or even during gameplay.

    We discovered that on Windows, the player prefs are read from the registry every time they are accessed, rather than cached inside Unity.

    That lead to some very unpleasant exploits when players figured out they could edit the registry in real time to change how the game worked.

    So reading in once and then accessing your in-game version is not only convenient, it prevents this sort of confusion and uncertainty.
     
  6. balu89

    balu89

    Joined:
    Apr 15, 2008
    Posts:
    30
    Thanks for the advice!!!! You are great...

    Sorry, but I'm new in this, I did a game manager but I don't know how playerPrefs works, how to store the selection of the gui.toggle inside a playerPrefs considering that is a bool(true or false)...

    :oops:
     
  7. DocSWAB

    DocSWAB

    Joined:
    Aug 28, 2006
    Posts:
    615
    Store your boolean value in prefs as a string, then read back in as a a string and reset your boolean based on checking the string content. The boolean casts to a string as True and False (uppercase), so use that when you check after you read it in.


    Code (csharp):
    1. // save boolean
    2. showTutorials = false;
    3. PlayerPrefs.SetString("Show Tutorials", ""+showTutorials);
    4. // get it back
    5. temp = PlayerPrefs.GetString("Show Tutorials", "True");
    6.     if (temp == "True") {
    7.     showTutorials = true;
    8.     } else {
    9.     showTutorials = false;
    10.     }
    11.  
     
  8. balu89

    balu89

    Joined:
    Apr 15, 2008
    Posts:
    30
    Thanks... I put the code in my game manager script, but it have to be inside a special function or not?? :?
     
  9. DocSWAB

    DocSWAB

    Joined:
    Aug 28, 2006
    Posts:
    615
    Those are just fragments -- you have to use them where you were setting and getting the player prefs. And of course the variable names are just for example.

    The default value in the GetPrefs call is used in case the key was never set before. So you use whatever state you want to be the default state there.
     
  10. balu89

    balu89

    Joined:
    Apr 15, 2008
    Posts:
    30
    I know that I 'm doing something wrong, but I dont know what. And I get so confused!!! Pleasee help...

    This is my script that is attached to a Gui.Text in a scene call options:

    Code (csharp):
    1.  
    2. static var moveSlow = false;
    3. static var moveFast = true;
    4.  
    5.  
    6. function OnGUI () {
    7.    
    8. moveSlow = GUI.Toggle (Rect ((Screen.width/2)-60,(Screen.height/2)-100,150,15), moveSlow, "Move Slow");
    9.  
    10. PlayerPrefs.SetString("Move Slow", "moveSlow");
    11.  
    12.      
    13.  
    14. moveFast = GUI.Toggle (Rect ((Screen.width/2)-60,(Screen.height/2)-70,150,15), moveFast, "Move Fast");
    15.  PlayerPrefs.SetString("Move Fast", "moveFast");
    16.  
    17. }
    18.  
    19.  
    And this is the code that I have in a game manager script that is in the first scene.


    Code (csharp):
    1.  
    2. function Pref () {
    3.     temp = PlayerPrefs.GetString("Move Slow", "True");
    4.        if (temp == "True") {
    5.    moveSlow = true;
    6.      } else {
    7.    moveSlow = false;
    8.    
    9.    }
    10.    temp = PlayerPrefs.GetString("Move Fast", "False");
    11.    if (temp == "False") {
    12.    moveFast = false;
    13.    } else {
    14.    moveFast = true;
    15.  
    16.    }
    17.  
    18.     }  
    19.  
    20.  
    21.  
    The problem is that the changes that I made, didn't store for the next game session... What I have to do to all the changes keeps store in the player preferences???

    Thanks... :?: :?: :?: