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

Mockup & CodeGen, iPhone & iPad
($9.99)

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

Manu
($0.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 03-12-2010, 01:42 AM   #1 (permalink)
Registered Member
 
Join Date: Feb 2010
Posts: 7
Default Storing name and score information to NSUserDefaults

Hi there,

I'm trying to create my first game and I'm having a little trouble figuring out how to save the name and score information.
Basically I have 5 possible name-score information like this:

1. John 220
2. John 117
3. Mary 93
4. -- --
5. -- --

to be stored and neither the name or score is unique, so I can't use NSDictionary.

What is the best way to go about this?

Thanks!
mansi5 is offline   Reply With Quote
Old 03-19-2010, 11:46 AM   #2 (permalink)
Indie Dev
 
hm50's Avatar
 
Join Date: May 2009
Location: South Bend, Indiana
Posts: 162
Default

Code:
NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults];
NSString *yourScore = [prefs stringForKey:@"score"];
						
NSLog(@"Your score is: %@",yourScore);

//To set a default
NSString *newScore = @"1234";
[prefs setObject: newScore forKey:@"score"];
NSUserDefaults will accept BOOLs,Strings, FLOATs, and INTs I believe.

I just showed using string values, but in your case you may want to use a INT type for each score. Search NSUserDefaults for more info.
__________________
iPhone 3G

Support Indie Devs!! (that goes for newbs too!)

Apps:
Spell Blocks
See Read Say
iStatus
myVIP
myMVP
hm50 is offline   Reply With Quote
Old 03-21-2010, 02:24 PM   #3 (permalink)
Registered Member
 
ataranlen's Avatar
 
Join Date: Jun 2009
Location: Flower Mound, Texas
Age: 22
Posts: 66
Default

if you're wanting to do a high score table, you want to use nested if statements, in conjunction with NSUserdefaults.

example:
Code:
Int_HighScore_0 = [prefs integerForKey:@"HighScore_0"];
Int_HighScore_1 = [prefs integerForKey:@"HighScore_1"];
Int_HighScore_2 = [prefs integerForKey:@"HighScore_2"];
Int_HighScore_3 = [prefs integerForKey:@"HighScore_3"];
Int_HighScore_4 = [prefs integerForKey:@"HighScore_4"];
if (Int_Score > Int_HighScore_4) {
	if (Int_Score > Int_HighScore_3) {
		if (Int_Score > Int_HighScore_2) {
			if (Int_Score > Int_HighScore_1) {
				if (Int_Score > Int_HighScore_0) {
					[prefs setInteger:Int_Score forKey:@"HighScore_0"];	
					[prefs setInteger:Int_HighScore_0 forKey:@"HighScore_1"];
					[prefs setInteger:Int_HighScore_1 forKey:@"HighScore_2"];
					[prefs setInteger:Int_HighScore_2 forKey:@"HighScore_3"];
					[prefs setInteger:Int_HighScore_3 forKey:@"HighScore_4"];
					[prefs setObject:Lbl_Menu_HSE_Name.text forKey:@"HighScore_Name_0"];
					[prefs setObject:String_HighScore_Name_0 forKey:@"HighScore_Name_1"];
					[prefs setObject:String_HighScore_Name_1 forKey:@"HighScore_Name_2"];
					[prefs setObject:String_HighScore_Name_2 forKey:@"HighScore_Name_3"];
					[prefs setObject:String_HighScore_Name_3 forKey:@"HighScore_Name_4"];

				} else {
					[prefs setInteger:Int_Score forKey:@"HighScore_1"];	
					[prefs setInteger:Int_HighScore_1 forKey:@"HighScore_2"];
					[prefs setInteger:Int_HighScore_2 forKey:@"HighScore_3"];
					[prefs setInteger:Int_HighScore_3 forKey:@"HighScore_4"];
					[prefs setObject:Lbl_Menu_HSE_Name.text forKey:@"HighScore_Name_1"];
					[prefs setObject:String_HighScore_Name_1 forKey:@"HighScore_Name_2"];
					[prefs setObject:String_HighScore_Name_2 forKey:@"HighScore_Name_3"];
					[prefs setObject:String_HighScore_Name_3 forKey:@"HighScore_Name_4"];
				}
			} else {
				[prefs setInteger:Int_Score forKey:@"HighScore_2"];	
				[prefs setInteger:Int_HighScore_2 forKey:@"HighScore_3"];
				[prefs setInteger:Int_HighScore_3 forKey:@"HighScore_4"];
				[prefs setObject:Lbl_Menu_HSE_Name.text forKey:@"HighScore_Name_2"];
				[prefs setObject:String_HighScore_Name_2 forKey:@"HighScore_Name_3"];
				[prefs setObject:String_HighScore_Name_3 forKey:@"HighScore_Name_4"];
			}
		} else {
			[prefs setInteger:Int_Score forKey:@"HighScore_3"];	
			[prefs setInteger:Int_HighScore_3 forKey:@"HighScore_4"];
			[prefs setObject:Lbl_Menu_HSE_Name.text forKey:@"HighScore_Name_3"];
			[prefs setObject:String_HighScore_Name_3 forKey:@"HighScore_Name_4"];
		}
} else {
	[prefs setInteger:Int_Score forKey:@"HighScore_4"];	
	[prefs setObject:Lbl_Menu_HSE_Name.text forKey:@"HighScore_Name_4"];
}
I know my example is kinda sloppy, but it works if you're wanting to have an in game only High score table.
__________________
Nathan Stoltenberg
Lead Programmer - Fractal Fox Studios
Mobile Applications Consultant - The Principal Consulting

Apps I've coded:
Greeble, Cat Juggling, and Garden 2 Go

1CJd8W6JiGKEgKHYp4PvWXWsnemHd8uqr7

Last edited by ataranlen; 03-21-2010 at 02:40 PM.
ataranlen is offline   Reply With Quote
Reply

Bookmarks

Tags
nsuserdefaults, score

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: 263
16 members and 247 guests
14DEV, @sandris, ADY, ArtieFufkin10, bookesp, ckgni, Dani77, DarkAn, HemiMG, iDifferent, jakerocheleau, JasonR, prchn4christ, Rudy, Speed
Most users ever online was 1,187, 10-11-2011 at 08:09 AM.
» Stats
Members: 158,885
Threads: 89,230
Posts: 380,767
Top Poster: BrianSlick (7,129)
Welcome to our newest member, bookesp
Powered by vBadvanced CMPS v3.1.0

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