Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

[SOLVED] How to Tell if PlayerPrefs is Null?

Discussion in 'Scripting' started by ChrisIsAwesome, Jul 30, 2017.

  1. ChrisIsAwesome

    ChrisIsAwesome

    Joined:
    Mar 18, 2017
    Posts:
    184
    So basically I have a high score and if no high score is found (is null), set high score to equal current score.

    I've tried using the PlayerPrefs.HasKey function but it doesn't work at all. The various ways I have tried to use it:

    Code (CSharp):
    1. // Method 1 for returning if false
    2. if (!PlayerPrefs.HasKey ("bestTime"))
    3. {
    4.      // Stuff
    5. }
    6.  
    7. // Method 2 for returning if false
    8. if (PlayerPrefs.HasKey ("bestTime") == false)
    9. {
    10.      // Stuff
    11. }
    12.  
    13. // Method 3 for returning if false
    14. if (PlayerPrefs.GetString ("bestTime") == null)
    15. {
    16.      // Stuff
    17. }
    18.  
    19. // Method 1 for returning if true
    20. if (PlayerPrefs.HasKey ("bestTime"))
    21. {
    22.      // Stuff
    23. }
    24.  
    25. // Method 2 for returning if true
    26. if (PlayerPrefs.HasKey ("bestTime") == true)
    27. {
    28.      // Stuff
    29. }
    30.  
    31. // Method 3 for returning if true
    32. if (PlayerPrefs.GetString ("bestTime") != null)
    33. {
    34.      // Stuff
    35. }
    Every single one of the above don't return anything.

    Any help would be greatly appreciated!

    - Chris
     
    jacky06211 and adikecapan like this.
  2. ChrisIsAwesome

    ChrisIsAwesome

    Joined:
    Mar 18, 2017
    Posts:
    184
    PS: I've deleted the PlayerPref at start to test things and so I KNOW it returns null, there's just no function I can find to determine whether it's null.
     
  3. GameDevJon

    GameDevJon

    Joined:
    Jul 21, 2017
    Posts:
    25
  4. GameDevJon

    GameDevJon

    Joined:
    Jul 21, 2017
    Posts:
    25
    when you "get" the value to display on the highscore, if that parameter doesn't exisist, it will use the default parameter. Which you can set as highscore.
     
    ChrisIsAwesome likes this.
  5. ChrisIsAwesome

    ChrisIsAwesome

    Joined:
    Mar 18, 2017
    Posts:
    184
    Thank you! Didn't notice the default parameter!
     
  6. Brathnann

    Brathnann

    Joined:
    Aug 12, 2014
    Posts:
    7,144
    HasKey should work just fine. Odd you're having issues with it. But good you found a solution that works for you.
     
  7. fafase

    fafase

    Joined:
    Jul 3, 2012
    Posts:
    160
    In case someone is reading this, I am wondering if the following is the right behaviour:

    Code (CSharp):
    1. string str = PlayerPrefs.GetString("key", null);
    this seems not to return null as would be expected but more likely a string.default.

    so the following would fail:

    Code (CSharp):
    1. string str = PlayerPrefs.GetString("key", null);
    2. if(str == null){ }
    but instead required:

    Code (CSharp):
    1. string str = PlayerPrefs.GetString("key", null);
    2. if(string.IsNullOrEmpty(str) == true){ }
     
    StealthBeast3xx0 and manirana94 like this.
  8. manirana94

    manirana94

    Joined:
    Apr 16, 2018
    Posts:
    7


    your last code is work for me
     
  9. Oneiros90

    Oneiros90

    Joined:
    Apr 29, 2014
    Posts:
    77
    I just ran into this problem... wtf Unity? I provided a default value, I want that value back if the key doesn't exist.
     
  10. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,719
    Fair enough, and that is what the API promises, but obviously not what it delivers.

    Even as far back as Unity5, supplying a default null to GetString() will give you in fact an empty string if the key in question doesn't exist.

    Before you get too exercised, note that:

    a) yes it is annoying because it's not what the API promises
    b) once identified it can be trivially worked around
    c) non-null default strings still work precisely as expected
    d) Unity is unlikely to change it because it would probably break their own internal stuff
    e) this behavior is analogous to a public / serializable string field in a MonoBehaviour/ScriptableObject that you never fill out before serializing. Hint: that won't be null either, EXCEPT in the case that it is a new never-before-serialized field.

    Warts are warts. Show me an API without warts. We know about this wart, as engineers we can deal with it.
     
    GordonRNorthSkyGames likes this.
  11. Oneiros90

    Oneiros90

    Joined:
    Apr 29, 2014
    Posts:
    77
    I agree with all your points @Kurt-Dekker, it is just annoying to waste time on these undocumented little defects in the API
     
    Kurt-Dekker likes this.
  12. Lurking-Ninja

    Lurking-Ninja

    Joined:
    Jan 20, 2015
    Posts:
    9,904
    And how do you propose to distinguish if you didn't pass a second parameter or if you pass null into it? I'm genuinely curious. Because that's the problem.
     
    Eristen likes this.
  13. Oneiros90

    Oneiros90

    Joined:
    Apr 29, 2014
    Posts:
    77
    The methods are separated:
    Code (CSharp):
    1. public static string GetString(string key, string defaultValue)
    2. public static string GetString(string key)
    There is not a single method defined like this:
    Code (CSharp):
    1. public static string GetString(string key, string defaultValue = null)
     
  14. Lurking-Ninja

    Lurking-Ninja

    Joined:
    Jan 20, 2015
    Posts:
    9,904
    Actually, they aren't: https://github.com/Unity-Technologi...me/Export/PlayerPrefs/PlayerPrefs.bindings.cs
    Code (CSharp):
    1. // Returns the value corresponding to /key/ in the preference file if it exists.
    2.         public extern static string GetString(string key, string defaultValue);
    3.  
    4.         public static string GetString(string key)
    5.         {
    6.             return GetString(key, "");
    7.         }
    But this is secondary, the interesting question is the C++ side. Obviously we only can guess, but most likely the string becomes an std:string (which is null-terminated and cannot be null) because of the variable length.
    So I think you end up with an empty string with a null-terminator in it. And then in case of the missing key you get back the empty string which becomes an empty managed string after marshalling. And the second definition of the GetString with the empty string second parameter gives the hint in to this direction.
    But again, it's an educated guess since we don't have the source code for the other side.
     
    Kurt-Dekker and Oneiros90 like this.