views:

44

answers:

1

I want to make a simple highscore system for my game. No posting online, just storing your best scores on the device, maybe being able to share them on Twitter or something like that.

My table is like this:

CREATE TABLE highscores ( name VARCHAR(10), score INT )

and then I want to find an easy way to retrieve the name, score and order it by score descending, returning the top 5.

SELECT name, score FROM highscores ORDER BY score DESC LIMIT 0, 5

I tried to do this, following this SQLite tutorial: http://dblog.com.au/iphone-development-tutorials/iphone-sdk-tutorial-reading-data-from-a-sqlite-database/

I created the database, set it up, tried to make my own wrapper, but I got the following problems:

  • It worked loading the score, but loading the name, which I tried to convert to a NSString* like they do in the tutorial, wouldn't work, it just returned 0 even though I had set the name to "Johannes"
  • If I navigated away from the scores page, and then back to the menu, and clicked on the scores page again the app crashed.

Any ideas? I'm really stressed I can't find ANYTHING online about this. :[

+2  A: 

Dude... Overkill.

Just save this data in an object or array, to save and restore the object betweens sessions use NSUserDeafults.

To order the data. Use Predicates. Failing that just sort it your self. http://developer.apple.com/mac/library/documentation/Cocoa/Conceptual/Predicates/Articles/pUsing.html

Seriously don't waste anytime getting your app live. I like to say "fail quickly", which means figure out what doesn't work and run with what works and make that awesome. Nothing worse than spending days/weeks on an app that might only make a few bucks.

Hope this is a help.

John Ballinger
Can't you post some example code of how to do what you just said?
Johannes Jensen
NSUserDefaults to save info. Is simple as this.[[NSUserDefaults standardUserDefaults] setObject:YOUR_HIGH_SCORE_ARRAY_DICTIONARY forKey:@"hiscores"];read more about it here. http://www.kurl.ws/xKYou could even forget using Predicates and write a simple loop to sort the scores manually.
John Ballinger
Oh thanks a lot! NSUserDefaults is like a life saver. :D Reminds me of Flash's SharedObject...sort of. Well, as easy to use. :D
Johannes Jensen
Yeah totally. I am working with shared objects right now in Flash. But keep your stuff super simple if you can, "fail quickly" and figure out after launching your product what is working well and not. Glad you are just going to use NSUserDefaults. :)
John Ballinger