Hi. I recently created a game and now I am to the point where I want to be able to save high scores. I get the concept of NSUSERDEFAULTS, but I just can't figure out how I detect whether or not the user made a new high score. I guess the REAL question is: How do you detect a new high score?
Hi. I recently created a game and now I am to the point where I want to be able to save high scores. I get the concept of NSUSERDEFAULTS, but I just can't figure out how I detect whether or not the user made a new high score. I guess the REAL question is: How do you detect a new high score?
You say that you "get the concept" of NSUserDefaults, so describe how you plan to use this class to help you here. IOW, what will NSUserDefaults give you the ability to do?
You say that you "get the concept" of NSUserDefaults, so describe how you plan to use this class to help you here. IOW, what will NSUserDefaults give you the ability to do?
By my understanding, NSUSERDEFAULTS gives me the ability to save short pieces of information (such as an integer or number). As for using it in my code, I can't really put it into code yet. I need to know how I detect whether or not the user made a new HIGH score. Not just any other score. If I didn't have some way of knowing whether or not it's their highest score, it would just display the score that the person just made.
...NSUSERDEFAULTS gives me the ability to save short pieces of information (such as an integer or number)...
Well, that's half of what it does. Usually implicit in the concept of saving information is the ability to retrieve it, and perhaps this is already obvious to you.
Now, knowing that you can save and/or retrieve your current high score, is it obvious how to determine if the player's score is a new high score or not?
Well, that's half of what it does. Usually implicit in the concept of saving information is the ability to retrieve it, and perhaps this is already obvious to you.
Now, knowing that you can save and/or retrieve your current high score, is it obvious how to determine if the player's score is a new high score or not?
Wouldn't that mean I would have to retrieve every single score the player makes though?
Why would you have to do that? The player could have 5 previous scores or 5,000 -- you're only interested in one of those scores, though.
ok. here's what i have so far:
How I save it:
NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults];
[prefs setInteger:yourHighScoreValue forKey:@"highScore"];
[prefs synchronize];
How I retrieve it:
NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults];
NSInteger yourHighScoreValue = [prefs integerForKey:@"highScore"];
How I change the score label to match high score:
if(yourHighScoreValue == nil) {
yourHighScore.text = @"0";
} else {
yourHighScore.text = [[NSString alloc] initWithFormat:@"%@",yourHighScoreValue];
}
______________________
Now all I need is a way to make yourHighScoreValue change to the new high score when the user makes a new high score.
How I save it:
NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults];
[prefs setInteger:yourHighScoreValue forKey:@"highScore"];
[prefs synchronize];
How I retrieve it:
NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults];
NSInteger yourHighScoreValue = [prefs integerForKey:@"highScore"];
How I change the score label to match high score:
if(yourHighScoreValue == nil) {
yourHighScore.text = @"0";
} else {
yourHighScore.text = [[NSString alloc] initWithFormat:@"%@",yourHighScoreValue];
}
______________________
Now all I need is a way to make yourHighScoreValue change to the new high score when the user makes a new high score.
OK, so 'yourHighScoreValue' appears to be holding the current high score from the user defaults. Do you have another variable that's holding the latest score? What has to be done with these variables to detect a new high score?
Also, NSInteger is an intrinsic type, so you should be checking 'yourHighScoreValue' for equality to 'nil'. In fact, all you need to do is what's in that 'else' clause.
OK, so 'yourHighScoreValue' appears to be holding the current high score from the user defaults. Do you have another variable that's holding the latest score? What has to be done with these variables to detect a new high score?
Also, NSInteger is an intrinsic type, so you should be checking 'yourHighScoreValue' for equality to 'nil'. In fact, all you need to do is what's in that 'else' clause.
Ok. I did what you said and here is my new code :
How I saved it:
NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults]; [prefs setInteger:score_value forKey:@"highScore"];
[prefs synchronize];
___________________
(score_value is the current score)
___________________ How I retrieved it:
NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults];
NSInteger yourHighScoreValue = [prefs integerForKey:@"highScore"];
How I changed the label to match it:
yourHighScore.text = [NSString stringWithFormat:@"%d",yourHighScoreValue];
________________________
This method worked, but it updates everytime the player makes a new score and therefore displays their current score. I tried setting up an if statement saying if the current score is greater than yourHighScoreValue then execute how I retrieved it and change the label. This made the number not change at all.
How I saved it:
NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults]; [prefs setInteger:score_value forKey:@"highScore"];
[prefs synchronize];
___________________
(score_value is the current score)
So, you're saying that every time the player makes a new score, it writes that to the user defaults as the high score? Is that what you want to be doing?
Quote:
Originally Posted by Whitehk
...I tried setting up an if statement saying if the current score is greater than yourHighScoreValue then execute how I retrieved it and change the label. This made the number not change at all.
If you're retrieving the value from the user defaults, what value is that going to return?
So, you're saying that every time the player makes a new score, it writes that to the user defaults as the high score? Is that what you want to be doing?
If you're retrieving the value from the user defaults, what value is that going to return?
My thought process was that if I make an if statement saying that if the current score is higher than their high score then replace the old high score with the current score (i do this by retrieving the NSUSERDELFAULTS which is the current high score) . If it's not, it displays the old high score (which I can do by simply making an else counterpart to the if statement and displaying the old high score without retrieving the new NSUSERDEFAULTS data). I dont want it to write the user defaults every time they make a new score. I want it to write it every time they make a score higher than they've ever made.
We actually use a binary file to store all our save data.
Using NSUserDefaults certainly is one approach. If you want to do that, I recommend that on initialization, you read the data for your high scores. Load this information into your own data structures to maintain the high scores. Then, when you a player plays a game, adjust the data structures.
When you receive an event that the app will exit, then write out that information to the NSUserDefaults. You can of course choose other times to write this out if you want.
Also, you can store NSArray data in NSUserDefaults if you want as well. So you could always create an NSArray to say store the name and score together.
My thought process was that if I make an if statement saying that if the current score is higher than their high score then replace the old high score with the current score (i do this by retrieving the NSUSERDELFAULTS which is the current high score) . If it's not, it displays the old high score (which I can do by simply making an else counterpart to the if statement and displaying the old high score without retrieving the new NSUSERDEFAULTS data). I dont want it to write the user defaults every time they make a new score. I want it to write it every time they make a score higher than they've ever made.
You're on the right track. I think the thread had run its course, so rather than continue batting it back and forth, I'll just share what I think is a good strategy for this.
Code:
// Read the high score from the user defaults at app startup.
high_score = [NSUserDefaults blah blah]; // read from user defaults
// Run the game
if ( game_score > high_score )
{
// this is a new high score, update the internal variable...
high_score = game_score;
// ...and write out the new high score to user defaults
[NSUserDefaults blah blah]; // write to user defaults
}
You're on the right track. I think the thread had run its course, so rather than continue batting it back and forth, I'll just share what I think is a good strategy for this.
Code:
// Read the high score from the user defaults at app startup.
high_score = [NSUserDefaults blah blah]; // read from user defaults
// Run the game
if ( game_score > high_score )
{
// this is a new high score, update the internal variable...
high_score = game_score;
// ...and write out the new high score to user defaults
[NSUserDefaults blah blah]; // write to user defaults
}
It's really no more complicated than that.
I just messed around with it and finally got it. Thanks for the help anyway!
Ok, I have a similar 'problem'. I've got OpenFeint integrated with my app, and I was wondering if it's possible to retrieve data from the leader-boards to unlock an achievement; so, for example, when the user has 10,000 points overall and at least 1,000 in each category. Should I just store the numbers in NSUserdefaults, or can I access the leader-boards?
Thanks.
Ok, I have a similar 'problem'. I've got OpenFeint integrated with my app, and I was wondering if it's possible to retrieve data from the leader-boards to unlock an achievement; so, for example, when the user has 10,000 points overall and at least 1,000 in each category. Should I just store the numbers in NSUserdefaults, or can I access the leader-boards?
Thanks.
Both work. I believe you can access the OF leaderboard values ... we used to do that in the past. But we track it ourselves. I'm not on my Mac now, so I can't see what routines we used to get them ... but you should be able to find it on the OF site.