- Home /
Loading error when using PlayerPrefs.GetInt with WebPlayer
Hello
Something really odd happens with my game in the version distributed for webplayer (this does not happen in the PC version).
The game starts with a menu, then pressing New Game fires a function which uses PlayerPrefs.getInt to see if the player has already played the game and a "currentLevel" value has been stored in their PlayerPrefs.
Now, whenever I clear my webprefs, simulating a user with no prefs saved then the game only loads part of the scene, part of the textures, basically a broken scene with only part of what should be there. It's as if it starts loading the scene but then something breaks the process (an exception?).
However, after loading the scene my game automatically saves the currentLevel value with playerPrefs.setInt, thus creating a playerpref file on the machine. The following time the game is played everything is right.
Now what's happening here? I tried catching any execptions that may happen when the game is trying to retrieve the player pref like so:
var prefs : int;
//deal with any errors
try{
prefs = PlayerPrefs.GetInt("currentLevel");
} catch (err){
prefs = 0;
}
Does this ring a bell for anyone? Thanks in advance for any help!
Concerning the syntax, you can do something simpler :
var prefs : int;
prefs = PlayerPrefs.GetInt("currentLevel");
if(prefs != 0)
{
// currentLevel was already set OR == 0
}
else
{
// currentLevel was unset
}
There is a second optional parameter in the PlayerPrefs.GetInt() that lets you decide the default value.
Answer by neo187-2 · Mar 23, 2012 at 03:34 PM
Thank you Dakwamine, I did consider using the getInt() version with the defaut value. But then I came across this solution which has solved the problem fortunately:
//deal with PlayerPrefs not existing on machine
if(PlayerPrefs.HasKey("SaveGame")){
prefs = PlayerPrefs.GetInt("SaveGame");
} else {
prefs = 0;
}
The error is no longer happening, with or without the URL Prefs on the machine!
Ok, these are good news! :) If your question is answered, please accept your answer as the correct one (button on the left side of your answer). ^^ Goodbye!
Unless you want to have some special handling when the key isn't present, it can be much simpler to just pass a default value:
//get the saved value, or default to zero
prefs = PlayerPrefs.GetInt("SaveGame", 0);
Answer by aldonaletto · Mar 23, 2012 at 12:52 PM
Could it be the 1M limit for Web Player prefs? PlayerPrefs are not size limited in stand alone versions, but have a 1M limit per Web player URL.
Your answer
Follow this Question
Related Questions
Saving boolean values using PlayerPrefs 1 Answer
PlayerPrefs.GetInt won't load when awake. 0 Answers
PlayerPrefs Question 0 Answers
Loading a save/playerprefs from a different game? 2 Answers
[PlayerPrefs] Shared saves between WebPlayer and Standalone? 0 Answers