This tutorial covers how to use a highscore table with OpenFeint in your iPhone apps.
Before continuing, please ensure you’ve completed part 1 of the tutorial. (This part assumes you have already got the basics set up, which is what we did in part 1 – if you continue without doing this it will not work and will get very messy very fast!)
I had initially hoped to cover achievements and high scores in a single post, but this will now be a 3 part mini series of tutorials as I want to try and keep things as easy and simple as possible for everyone so no one feels overwhelmed.
Let’s get started on high scores!
STEP ONE: Head on over to OpenFeint where you will already have an account from part one, and sign in; then make sure you have the correct app selected from the drop down bar (top left) and use the left hand side navigation menu to click on Features then Leaderboards under that.
STEP TWO: Set up a leaderboard for your application if you haven’t already. This is easy to do and you don’t need to change any settings, just give it a name – in my case, I’ve called it “Highscores”. Minimize the page but don’t close it yet, we’ll want that ID in a moment.
STEP THREE: I use director to manage my scenes/screens and most of you will too as it’s pretty inefficient to make a game without doing so; however if you for some reasons don’t use it you’ll want to do this slightly differently, but I’ll cover that in the moment – whether you are using director or not, copy and paste this code into your gameover.lua (or whatever you have called your final scene/screen) or, in the case of using only a main.lua place the code at the end of that file.
if string.sub(system.pathForFile("Icon.png", system.ResourceDirectory),1,6)=='/Users' then
-- Leave this OUT if you are NOT using director as it will already be in your file from part 1
*I can see the huge gap between “if” and “string.sub” for some reason it wont go away despite the fact everything appears to be formatted properly. Sorry, I know it’s hideous
As you can see from the comment above, you wont use this if you aren’t using director as it’s already in your main.lua file in this case from part 1 of the tutorial.
You will notice that I have named this onDeviceGO (“GO” being Game Over) – this is simply for clarity. If you aren’t using director you will want to change this simply to “onDevice” for the rest of the tutorial. If you are using director, leave it as it is.
*Notice how I don’t tell you to require openfeint in your game over screen/scene? This is because we called it in part 1 and calling it again causes issues. Yes, I’m once again stressing that you better have completed part 1!
STEP FOUR: Time to set the highscore, at last! Copy and paste this code into your gameover.lua (or equivalent) or at the end of your main.lua if you aren’t using director;
lbID = "123456"
numScore = score.getScore()
function setHighScore( lbID, points )
if onDeviceGO then
openfeint.setHighScore( 535014, numScore)
timerGO = timer.performWithDelay(2000, setHighScore, 1)
-- Set the highscore on OpenFeint
OK – let’s cover what parts of this code you might want to change;
- The timer sets the highscore two seconds after the player gets a game over; if you want to change this to 3 seconds you’d replace “2000″ with “3000″ or 1 second “1000″ and so on and so forth. 2 seconds felt right for my particular game.
- I’ve named my timer timerGO – this was totally unnecessary and to this day I have no idea why I did it. Using a normal, unnamed timer (timer.performWithDelay) would be just fine.
- The first line, lbID must be changed. Copy and paste your ID from your minimized OpenFeint window into this spot; leave the quotes in.
- numScore should equal your score. If your score is simply named “playerscore” then use that, mine is score.getScore() as I’m using a handy little score thingy (yeah, I haven’t got the language down yet, sorry!) and that’s how I get my score.
Alright, so that’s all pretty straight forward; if you are on the simulator it will simply print “highscore called”, or whatever you’d like to change this to. If you are on the device and you get a high score, you’ll see a little message up top just like with all OpenFeint events.
If you are not using director and this is all in one big file, I would suggest that if your game uses a timer, ie, after 60 seconds the game ends, then set your timer for 61 or 62 seconds. If you are not using a timer then add the event to whatever triggers the end of the game. If this confuses you at all, do not hesitate to email, FB or tweet at me - I know it can be confusing and I am totally happy to help you out, by which I mean talk you through it or fix your code myself; I genuinely don’t want to leave anyone upset or frustrated that they can’t make this thing work. (Same goes for those using director, if you can’t make it work for some reason let me know – it wont be a problem and we’ll get it working for you too.)
A FINAL VITAL STEP: You need to add an OpenFeint account to “Test Users” in your OpenFeint Dev account. (Go Settings, Test Users). It wont let you use your account if it’s the same as your login for your Dev account, I don’t know why – if this happens to you when trying to add a new account just create a second email if you don’t already have one and use that.
And that’s it!
This should work flawlessly; note that only one high score per user will be recorded on the board, if they beat it they overwrite it, if they don’t it wont show up. This is a good thing as it stops one user (presumably you if you spend a lot of time testing) from filling up the leader board.
As usual and as I’ve stressed above, get in touch – even if you think your question is “stupid” I still want to hear from you; I had a huge number of what I considered to be stupid questions when I was learning this stuff and it stopped me from asking things I should have. I wont get fed up with and I wont think you’re silly for not understanding; just please, please – let me know if you need a hand.
If you haven’t already shown some FB or Twitter love you can do so using the buttons at the top right.
Part 3 will be coming in the next few days and will cover unlocking achievements, sorry I couldn’t fit it into part 2 as planned!
PS – I had a comment on one of my posts yesterday and have previously had a few emails as well in which people seem to be under the impression I am in fact male. While I have nothing against your fine gender I’d still like to make it clear that at the present time I do not have a penis – I simply have an interest in coding and being overall geeky, nerdy and awesome. Thanks!