Search Unity

PlayerPrefs

Discussion in 'Scripting' started by stevedarby02, Jan 4, 2014.

  1. stevedarby02

    stevedarby02

    Joined:
    Jan 4, 2013
    Posts:
    61
    Hi,

    I've created the below to go in a larger piece of code within the Update function. If you need me to paste the remaining code let me know.

    The below works in terms of returning the correct value on the debug, but it doesn't actually update the variable on the inspector when 'o' is pressed, then 'm'. I need it to state a weapon has already been picked up as part of the save.

    Could someone help me understand why? 'haveWeapon' is a bool so i've had to do the player prefs different as SetBool isn't available. Its strange that the debug states true, but the inspector doesn't update and remains false?

    Code (csharp):
    1.        
    2.  if (Input.GetKeyDown("o"))
    3.    
    4.     {
    5.              PlayerPrefs.SetInt("HaveWeapon", haveWeapon ? 1 : 0);
    6.     }
    7.        
    8.        
    9. if (Input.GetKeyDown("m"))
    10.      {
    11.        
    12.               haveWeapon = PlayerPrefs.GetInt("HaveWeapon") == 1 ? true : false;                   
    13.             Debug.Log(haveWeapon); 
    14.       }
    15.  
    Many Thanks (p.s i'm new to using player prefs)
     
  2. tux83

    tux83

    Joined:
    Jan 2, 2014
    Posts:
    19
    hi, try

    Code (csharp):
    1. using PlayerPrefs = PreviewLabs.PlayerPrefs;
    2.  
    3.  
    4. if (Input.GetKeyDown("o"))
    5.  
    6.    
    7.  
    8.     {
    9.  
    10.              PlayerPrefs.SetInt("HaveWeapon", haveWeapon ? 1 : 0);
    11.              PlayerPrefs.Flush();
    12.     }
    13.  
    add this method :
    Code (csharp):
    1.  
    2.     public void OnApplicationQuit()
    3.     {
    4.         PlayerPrefs.Flush();
    5.     }
     
  3. Zeblote

    Zeblote

    Joined:
    Feb 8, 2013
    Posts:
    1,102
    But. If you press o and then m, the value won't be changed? What's there to update?

    Or do you mean it stays changed after you exit play mode? When you exit play mode all variables will be set back to what they were before you started playing.

    You also have to call PlayerPrefs.Save() to actually save the changed prefs, I think.

    That isn't default, is it?
     
  4. stevedarby02

    stevedarby02

    Joined:
    Jan 4, 2013
    Posts:
    61
    Hi,

    I mean press 'o' then exit and load up the game again and press 'm'. It does save the the correct value of 'true' when you see it in the debug, but it doesn't update on the inspector. So whereas the debug states true the inspector remains as false once the game is loaded again and you press 'm'.

    I added PlayerPrefs.Save(); but its still the same result.

    tux83, thanks, but i'm unable to add using PlayerPrefs = PreviewLabs.PlayerPrefs; at the top of the code without it producing an error?

    'The type or namespace name `PreviewLabs' could not be found. Are you missing a using directive or an assembly reference?'

    Thanks
     
    Last edited: Jan 5, 2014
  5. tux83

    tux83

    Joined:
    Jan 2, 2014
    Posts:
    19
  6. stevedarby02

    stevedarby02

    Joined:
    Jan 4, 2013
    Posts:
    61
    Hi,

    I actually did a search after that and found it, downloaded it and tried it. No more errors but the variable is still false even though the debug states it's true.

    Thanks
     
  7. tux83

    tux83

    Joined:
    Jan 2, 2014
    Posts:
    19
    you're Welcome ! ;)
     
  8. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,674
    A convenient pattern I use for this sort of thing is to map a property "live" to the Unity3D PlayerPrefs settings.

    F'rinstance:

    Code (csharp):
    1. public class PLAYERSAVESTATE
    2. {
    3.     const string s_HaveWeapon = "HaveWeapon";
    4.     public static bool HaveWeapon
    5.     {
    6.         get
    7.         {
    8.             return PlayerPrefs.GetInt( s_HaveWeapon, 0) != 0;
    9.         }
    10.         set
    11.         {
    12.             PlayerPrefs.SetInt( s_HaveWeapon, value ? 1 : 0);
    13.         }
    14.     }
    15. }
    16.  
    Now then wherever you use PLAYERSAVESTATE.HaveWeapon, it will be reflected live in the settings and automatically saved when you stop/start the game. The zero given as the second argument in the .GetInt() call is the default value for the first time the player runs the game.

    When the player starts a new game, you can just set it to what you want it to be, using it just like any other variable.

    Kurt
     
  9. stevedarby02

    stevedarby02

    Joined:
    Jan 4, 2013
    Posts:
    61
    Thanks. Just to check.

    Is the static variable 'HaveWeapon' a new variable or should it replace the existing one 'haveWeapon'?

    How would the if statement work in that example?

    Many thanks for your help.