0

I have a problem with NSUserDefaults.

I followed Steve's answer from this one: Saving highscores with NSUserDefaults

And now, I have a problem. When my GameScene is to be over, and just before the transfer to the GameOverScene is initialized, I do this:

    if scene.score > Score.highScore {
        var defaults = NSUserDefaults()
        defaults.setInteger(scene.score, forKey: "highScore")
        println("score1, highscore1")
        println(scene.score)
        println(Score.highScore)
    }

And you can see I compare the gameScene score to the highscore, and then set the highscore's Integer to the scene's one, that player achieved. Yet, program print's out the current score correctly, leaving the highScore the same, at 0. Before I tried to do:

Score.HighScore = scene.score // changing Score.HighScore to the scene.score manually. 

Unfortunately, did not work. Here's the full HighScore Class, that handles saving and retrieving highscore: http://pastebin.com/dz3b3WAk

Any help appreciated.

EDIT: or maybe, someone is aware of easier solution of saving something to NSUserDefaults, without all that complication?

Community
  • 1
  • 1
Betkowski
  • 491
  • 1
  • 4
  • 17

1 Answers1

1

Steve's answer in that related question does not use NSUserDefaults at all, and instead he is archiving & retrieving the high score from a "highScore.archive" file store in your app's Document directory.

Any solution you come up with should forget about user defaults and instead rely on your HighScore object that you instantiate (which loads and saves into that highScore.archive file).

Michael Dautermann
  • 88,797
  • 17
  • 166
  • 215
  • Any idea then how can I acces that archive from different scene, and modify it? – Betkowski Nov 23 '14 at 13:12
  • When you instantiate (or create) that object in a different scene, it will load up the high score that's currently saved in the "`highScore.archive`" file. So all you need to make certain of is that the previous scene has properly saved off the new high score (e.g. the "`SaveHighScore().ArchiveHighScore(highScore: Score)`" bit in Steve's answer). And to load a high score, you can do: `var retrievedHighScore = SaveHighScore().RetrieveHighScore() as HighScore`. Makes sense? – Michael Dautermann Nov 23 '14 at 13:16
  • It does! Now It print's out the score as it is, and the highscore with the correct Int of score. But (I know, I'm such a pain, and I'm sorry), when I put retrievedHighScore in my SKLabel, it comes up with this: score: 5, highscore: – Betkowski Nov 23 '14 at 13:23
  • That's because your SKLabel is expecting something it can use (e.g. the raw high score number, converted to text?), versus what you are handing it, which is the HighScore/Score object (it doesn't know what to do with it). – Michael Dautermann Nov 23 '14 at 13:41
  • Ok, I get it. RetrieveHighScore returns NSObject. How Can I convert that object to String so I can pass it to SKLabel? – Betkowski Nov 23 '14 at 13:53
  • 1
    See what Steve did with RetrieveHighScore? He made it into a `HighScoreObject` and was able to do `println` on it. – Michael Dautermann Nov 23 '14 at 13:55
  • It works! I used retrievedHighScore.highScore for the SKLabel. The only, last problem I have now, Is when I test it on my Ipad, set the highscore, close the game, and then open it again, the highScore is missing, like it's not being saved. Any ideas? – Betkowski Nov 23 '14 at 14:06
  • 1
    Make sure to `SaveHighScore.ArchiveHighScore()` at the first available opportunity. – Michael Dautermann Nov 23 '14 at 14:38
  • Ok, sorted, I had SaveHighScore in GameViewController's did Move to view, so everytime the game was to be launched, it was reseting the score's value. Thanks for all your help Michael! – Betkowski Nov 23 '14 at 14:54