Advertise Mobile SDKs Books Events Forum News Social Networking Support Us
Follow @iphonedevsdk on Twitter

Interface 2, Advanced iOS
Mockup & Code Gen
($9.99)

Make your own iPhone apps
and run them live!
(free)

Pic Frame Dynamo: Photo Editing
($0.99)

Abiliator
($1.99)

Want your application or service advertised on iPhone Dev SDK?

Go Back   iPhone Dev SDK Forum > iPhone SDK Development Forums > iPhone SDK Game Development

Reply
 
LinkBack Thread Tools Display Modes
Old 04-13-2010, 08:32 PM   #1 (permalink)
Registered Member
 
Whitehk's Avatar
 
Join Date: Feb 2010
Posts: 119
Whitehk is on a distinguished road
Default Save High Scores?

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?
Whitehk is offline   Reply With Quote
Old 04-13-2010, 09:57 PM   #2 (permalink)
Pro. Game Developer
iPhone Dev SDK Supporter
 
Join Date: Feb 2009
Location: żLa Islas Hermosas?
Posts: 2,176
Kalimba is on a distinguished road
Default

Quote:
Originally Posted by Whitehk View Post
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?
__________________
~~ Word Flurry ~~ App Store / Website / Facebook
Kalimba is offline   Reply With Quote
Old 04-14-2010, 04:33 PM   #3 (permalink)
Registered Member
 
Whitehk's Avatar
 
Join Date: Feb 2010
Posts: 119
Whitehk is on a distinguished road
Default

Quote:
Originally Posted by Kalimba View Post
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.
Whitehk is offline   Reply With Quote
Old 04-14-2010, 04:50 PM   #4 (permalink)
Pro. Game Developer
iPhone Dev SDK Supporter
 
Join Date: Feb 2009
Location: żLa Islas Hermosas?
Posts: 2,176
Kalimba is on a distinguished road
Default

Quote:
Originally Posted by Whitehk View Post
...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?
__________________
~~ Word Flurry ~~ App Store / Website / Facebook
Kalimba is offline   Reply With Quote
Old 04-14-2010, 05:04 PM   #5 (permalink)
Registered Member
 
Whitehk's Avatar
 
Join Date: Feb 2010
Posts: 119
Whitehk is on a distinguished road
Default

Quote:
Originally Posted by Kalimba View Post
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?
Whitehk is offline   Reply With Quote
Old 04-14-2010, 05:11 PM   #6 (permalink)
Pro. Game Developer
iPhone Dev SDK Supporter
 
Join Date: Feb 2009
Location: żLa Islas Hermosas?
Posts: 2,176
Kalimba is on a distinguished road
Default

Quote:
Originally Posted by Whitehk View Post
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.
__________________
~~ Word Flurry ~~ App Store / Website / Facebook
Kalimba is offline   Reply With Quote
Old 04-14-2010, 05:57 PM   #7 (permalink)
Registered Member
 
Whitehk's Avatar
 
Join Date: Feb 2010
Posts: 119
Whitehk is on a distinguished road
Default

Quote:
Originally Posted by Kalimba View Post
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.
Whitehk is offline   Reply With Quote
Old 04-14-2010, 06:31 PM   #8 (permalink)
Pro. Game Developer
iPhone Dev SDK Supporter
 
Join Date: Feb 2009
Location: żLa Islas Hermosas?
Posts: 2,176
Kalimba is on a distinguished road
Default

Quote:
Originally Posted by Whitehk View Post
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.
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.
__________________
~~ Word Flurry ~~ App Store / Website / Facebook
Kalimba is offline   Reply With Quote
Old 04-14-2010, 06:55 PM   #9 (permalink)
Registered Member
 
Whitehk's Avatar
 
Join Date: Feb 2010
Posts: 119
Whitehk is on a distinguished road
Default

Quote:
Originally Posted by Kalimba View Post
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.
Whitehk is offline   Reply With Quote
Old 04-14-2010, 07:35 PM   #10 (permalink)
Pro. Game Developer
iPhone Dev SDK Supporter
 
Join Date: Feb 2009
Location: żLa Islas Hermosas?
Posts: 2,176
Kalimba is on a distinguished road
Default

Quote:
Originally Posted by Whitehk View Post
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)
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 View Post
...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?
__________________
~~ Word Flurry ~~ App Store / Website / Facebook
Kalimba is offline   Reply With Quote
Old 04-14-2010, 08:49 PM   #11 (permalink)
Registered Member
 
Whitehk's Avatar
 
Join Date: Feb 2010
Posts: 119
Whitehk is on a distinguished road
Default

Quote:
Originally Posted by Kalimba View Post
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.
Whitehk is offline   Reply With Quote
Old 04-14-2010, 09:30 PM   #12 (permalink)
Registered Member
 
mobileben's Avatar
 
Join Date: Jul 2009
Location: Zgrunturos
Posts: 161
mobileben is on a distinguished road
Default

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.
__________________
Have a Poketastic time with QuitIt!
Arcade Basketball at it's best! Hoops Madness!
VS action on the iPad with Hoops Madness VS!
Sweet trailer here!
mobileben is offline   Reply With Quote
Old 04-14-2010, 10:21 PM   #13 (permalink)
Pro. Game Developer
iPhone Dev SDK Supporter
 
Join Date: Feb 2009
Location: żLa Islas Hermosas?
Posts: 2,176
Kalimba is on a distinguished road
Default

Quote:
Originally Posted by Whitehk View Post
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
}
It's really no more complicated than that.
__________________
~~ Word Flurry ~~ App Store / Website / Facebook
Kalimba is offline   Reply With Quote
Old 04-17-2010, 04:57 PM   #14 (permalink)
Registered Member
 
Whitehk's Avatar
 
Join Date: Feb 2010
Posts: 119
Whitehk is on a distinguished road
Default

Quote:
Originally Posted by Kalimba View Post
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!
Whitehk is offline   Reply With Quote
Old 04-19-2010, 04:10 AM   #15 (permalink)
Registered Member
 
Join Date: Mar 2010
Posts: 43
Nabopolassar is on a distinguished road
Default

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.
Nabopolassar is offline   Reply With Quote
Old 04-20-2010, 07:36 PM   #16 (permalink)
Registered Member
 
mobileben's Avatar
 
Join Date: Jul 2009
Location: Zgrunturos
Posts: 161
mobileben is on a distinguished road
Default

Quote:
Originally Posted by Nabopolassar View Post
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.
__________________
Have a Poketastic time with QuitIt!
Arcade Basketball at it's best! Hoops Madness!
VS action on the iPad with Hoops Madness VS!
Sweet trailer here!
mobileben is offline   Reply With Quote
Old 04-22-2010, 01:14 PM   #17 (permalink)
Registered Member
 
Join Date: Mar 2010
Posts: 43
Nabopolassar is on a distinguished road
Default

Thank you!
Nabopolassar is offline   Reply With Quote
Old 04-23-2010, 10:24 AM   #18 (permalink)
Pro. Game Developer
iPhone Dev SDK Supporter
 
Join Date: Feb 2009
Location: żLa Islas Hermosas?
Posts: 2,176
Kalimba is on a distinguished road
Default

Quote:
Originally Posted by Skin Care View Post
Usually implicit in the concept of saving information is the ability to retrieve it, and perhaps this is already obvious to you.
I have to LOL at how "original" this message is -- before deleting it and banning this spammer...
__________________
~~ Word Flurry ~~ App Store / Website / Facebook
Kalimba is offline   Reply With Quote
Old 04-23-2010, 05:19 PM   #19 (permalink)
Registered Member
 
Join Date: Feb 2009
Posts: 178
dustbyter is on a distinguished road
Default

If you dont want to use NSUserDefaults, you can always use the same set up as already discussed and persist the high-score in a sqlite database table.

Again the only difference here would be the repository to store the data and the set up should be same.
dustbyter is offline   Reply With Quote
Reply

Bookmarks

Tags
high score, high scores, nsuserdefault, nsuserdefaults, save data

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



» Advertisements
» Online Users: 407
8 members and 399 guests
iOS.Lover, JackReidy, jeroenkeij, Leslie80, Sami Gh, Wikiboo, Yosh_K
Most users ever online was 1,387, 04-10-2012 at 04:21 AM.
» Stats
Members: 175,671
Threads: 94,121
Posts: 402,903
Top Poster: BrianSlick (7,990)
Welcome to our newest member, JackReidy
Powered by vBadvanced CMPS v3.1.0

All times are GMT -5. The time now is 04:45 AM.
Powered by vBulletin® Version 3.8.0
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.3.0