Search Unity

PlayerPrefs.SetString saves empty strings

Discussion in 'Scripting' started by Jacksendary, Feb 25, 2015.

  1. Jacksendary

    Jacksendary

    Joined:
    Jan 31, 2012
    Posts:
    408
    Hey I've found PlayerPrefs.SetString to act very weird when I try to use it with local variables. As I'm pretty bad in explaining in word I will mainly focus on the code examples I've found to work and not work seen below.

    Example 1 and 2 are both very similar and none of them works as the saved string is empty. what this lead me to believe was that is may still write data to the hard drive when the method is out of scope and there by possibly have forgotten about the content of the string "savedata".

    I tried to stop/pause the program with thread.sleep (Example 3) and debug break points where I found everything to work just fine, which lead to the assumption about the out of scope stuff mentioned above.

    The last thing I tried was to make the savadata string a global variable (example 4) which also works just fine which then again could seem like the local string some how is lost while writing to the hdd.

    So I guess my actual question is if this is normal behavior or if this is a known issues, or if anyone else have had/seen similar problems? and how would I fix it if I had to use the code from example 1 or 2?

    - Thanks in regard!

    EDIT: I've found today that example 4 does not work either some how, Example 3 works still :s
    EDIT: I forgot to mention, I do also before I run this command below I save another string in the caller.
    EDIT: After more digging it could seem this the object i'm tryin to save may have been destroyed before they're saved correctly, which doesn't make sense as I do not remove them before all saving is completed, does the playerpref run on a seperate thread from the normal UnityEngine? that would explain why thread.sleep also works?

    Apendix:

    Expected saved output: "Sciapra,Smoauwei,0,19851,19851,50,50;Osmides,Sciapra,0,22561,22561,50,50;Sciapra,Okeothilia,0"
    Actual saved output in example 1 and 2:
    ""

    Example 1 (DOES NOT WORK!)
    Code (csharp):
    1.  
    2.     public void SaveGame()
    3.     {
    4.         string savedata = "";
    5.      
    6.         foreach (TradeRoute tr in TradeRoutes)
    7.         {
    8.             savedata += tr.SaveData() + ";";
    9.         }
    10.         if (savedata != "")
    11.         {
    12.             savedata = savedata.Remove(savedata.Length - 1);
    13.         }
    14.         PlayerPrefs.SetString(StaticValues.GAMESAVE, savedata);
    15.     }
    16.  
    Example 2 (DOES NOT WORK!)
    Code (csharp):
    1.  
    2.     public void SaveGame()
    3.     {
    4.         string savedata = "";
    5.      
    6.         foreach (TradeRoute tr in TradeRoutes)
    7.         {
    8.             savedata += tr.SaveData() + ";";
    9.         }
    10.         if (savedata != "")
    11.         {
    12.             savedata = savedata.Remove(savedata.Length - 1);
    13.         }
    14.         PlayerPrefs.SetString(StaticValues.GAMESAVE, savedata);
    15.         PlayerPrefs.Save();
    16.     }
    17.  
    Example 3 (DOES WORK!)
    Code (csharp):
    1.  
    2.     public void SaveGame()
    3.     {
    4.         string savedata = "";
    5.      
    6.         foreach (TradeRoute tr in TradeRoutes)
    7.         {
    8.             savedata += tr.SaveData() + ";";
    9.         }
    10.         if (savedata != "")
    11.         {
    12.             savedata = savedata.Remove(savedata.Length - 1);
    13.         }
    14.         PlayerPrefs.SetString(StaticValues.GAMESAVE, savedata);
    15.         System.Threading.Thread.Sleep(100);
    16.     }
    17.  
    Example 4 (DOES NOT WORK!)
    Code (csharp):
    1.  
    2.     string savedata;
    3.     public void SaveGame()
    4.     {
    5.         savedata = "";
    6.      
    7.         foreach (TradeRoute tr in TradeRoutes)
    8.         {
    9.             savedata += tr.SaveData() + ";";
    10.         }
    11.         if (savedata != "")
    12.         {
    13.             savedata = savedata.Remove(savedata.Length - 1);
    14.         }
    15.         PlayerPrefs.SetString(StaticValues.GAMESAVE, savedata);
    16.         PlayerPrefs.Save();
    17.     }
    18.  
     
    Last edited: Feb 25, 2015
  2. Jacksendary

    Jacksendary

    Joined:
    Jan 31, 2012
    Posts:
    408
    Up I'm still interested in hearing what may be the problem here
     
  3. KelsoMRK

    KelsoMRK

    Joined:
    Jul 18, 2010
    Posts:
    5,539
    There's nothing in your code to prevent the saving of an empty string value so I would imagine it's empty when you save it. Have you checked the value before and after saving? How are you retrieving the value later on?

    And 1 won't work because you're never saving your modifications (unless you quit the game).
     
  4. Jacksendary

    Jacksendary

    Joined:
    Jan 31, 2012
    Posts:
    408
    The only thing I can come to think of is that that following code somehow may be called multiple times (it's a snippet from the place where I save my data)

    EDIT: After adding multiple check for checking for multiple triggering it may seem that it does in fact somehow get called twice, I guess that I can blame the keyboard and not the unity API for this

    Code (csharp):
    1.  
    2.         if (Input.GetKeyDown(KeyCode.Escape))
    3.         {
    4.             SaveGame();
    5.             worldGenerator.GetComponent<WorldGen>().Save();
    6.          
    7.             //PlayerPrefs.Save();
    8.             GuiMode = false;
    9.             TradeRoutes.Add(tradeRoute);
    10.             tradeRoute = new TradeRoute();
    11.             guiMode = true;
    12.             mainMenu.SetActive(true);
    13.             ContinueButton.SetActive(true);
    14.             foreach (TradeRoute route in TradeRoutes)
    15.             {
    16.                 GameObject.Destroy(route.Route);
    17.             }
    18.             tradePanel.SetActive(false);
    19.             toolTip.SetActive(false);
    20.             TradeRoutes = new List<TradeRoute>();
    21.         }
    22.  
     
    Last edited: Feb 25, 2015