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!)
public void SaveGame()
{
string savedata = "";
foreach (TradeRoute tr in TradeRoutes)
{
savedata += tr.SaveData() + ";";
}
if (savedata != "")
{
savedata = savedata.Remove(savedata.Length - 1);
}
PlayerPrefs.SetString(StaticValues.GAMESAVE, savedata);
}
Example 2 (DOES NOT WORK!)
public void SaveGame()
{
string savedata = "";
foreach (TradeRoute tr in TradeRoutes)
{
savedata += tr.SaveData() + ";";
}
if (savedata != "")
{
savedata = savedata.Remove(savedata.Length - 1);
}
PlayerPrefs.SetString(StaticValues.GAMESAVE, savedata);
PlayerPrefs.Save();
}
Example 3 (DOES WORK!)
public void SaveGame()
{
string savedata = "";
foreach (TradeRoute tr in TradeRoutes)
{
savedata += tr.SaveData() + ";";
}
if (savedata != "")
{
savedata = savedata.Remove(savedata.Length - 1);
}
PlayerPrefs.SetString(StaticValues.GAMESAVE, savedata);
System.Threading.Thread.Sleep(100);
}
Example 4 (DOES NOT WORK!)
string savedata;
public void SaveGame()
{
savedata = "";
foreach (TradeRoute tr in TradeRoutes)
{
savedata += tr.SaveData() + ";";
}
if (savedata != "")
{
savedata = savedata.Remove(savedata.Length - 1);
}
PlayerPrefs.SetString(StaticValues.GAMESAVE, savedata);
PlayerPrefs.Save();
}