PlayerPrefs - Chicken and Egg

Hi
I am very close to releasing my first iPod Touch game, however I am struggling to understand how I can initialise my PlayerPrefs.

I am using PlayerPrefs to

  • save my game and allow the player to continue after an interruption or after returning to the home screen
  • save medal stats/scores

I have a single playerpref which is used as a flag: ‘IntialisePrefsOK’. When this is set to 0 (zero) then a function is called which resets all the medals to zero etc. This should only happen once, the very first time the game is launched on a machine. However I cannot understand how I should create the flag in the first place. Would the following code work if the game is played for the very first time on a machine: -

    if (!PlayerPrefs.GetInt("init_prefs_ok")){
	PlayerPrefs.SetInt("init_prefs_ok", 0);
    }

Thanks for your help

you should always pass in a default value. that then will be returned if the key is not found.

I do this in a few of my games by detecting if the key exists. If it does not exist, this is the player’s first time.

if (!PlayerPrefs.hasKey("init_prefs_ok")){ //Is this first time?
    PlayerPrefs.SetInt("init_prefs_ok", 1); //set visited key.
    InitializeNewPlayer() //code to run on first launch.
}

thanks for the replies. The use of haskey is an excellent idea. I will give that a try.