Nov 252010

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.

local onDeviceGO=true
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)
print("highscore called")
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!

Peach Pellen :)

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! :P

Like this post? Subscribe to the monthly newsletter!


  16 Responses to “Adding OpenFeint To Your iPhone App – Part 2”

  1. I get this error along with others

  2. I’m having trouble putting my high score into the leaderboard I get this error saying:
    attempt to index global ‘score_txt’ (a nil value)

    and my score looks like this

    local score_txt =display.newText("Score: ",0,0,nil,22)
        score_txt.x = 70; score_txt.y = 460
        score_txt.text = "Score: ".. score

    How would I replace numScore = score.getScore() to my score.

  3. Hello! Thanks for the good article about OF implementation.
    Btw. Is there a way to show more than only the 25 top scores from Corona?
    i know with their obj-c sdk you can use OFScoreEnumerator, but is there anything similar in corona?
    thx, etienne

  4. That really helps. Thanks for your quick response. Thats awesome!
    Greetings from Germany

  5. No stress at all Siddhi,

    You are correct, OF manages it all, you just follow the tutorial and you’ll have global online leaderboards.

    For the rest of it; users don’t have to use OF, they can use it as a guest or not at all, but pretty much everyone who has ever bought more than one or two games knows what OF is.

    Hope that helps :)


  6. Hey Peach,
    ok. I think Ive catched it. All the things (leaderboard,achievements..) will be stored on the OF-platform. But this brings me to my next question: Must a gamer have to be registered on OF to see the highscore or is it possible without being registered as a gamer to see achevements and leaderboard? If a user buys my app from the AppleStore does all this stuff work without ever having heared about OF?
    I hope my issues dont stress you to much

  7. Sorry for another request.
    I dont really catch the concept behind OF. From games point of View: lets say you download a game on your device (iPad/pod) You play the game as the first games, so you reach a highscore (the initial one). Now OF brings up the leaderboard and shows the score. My question: is this score saved on the device or on the OF-server?
    If its on the server:
    if another player buys my game and tops the score, as I can see this, a little function on the OF-server (however it will be called) compares the players result with the entry on the server and overwrite it if its higher than the older one.

    If the score is saved on the device I have no idea how the highscore-compare-thing will work.
    Thanks for a little help on that issue

  8. Hey Siddhi,

    Actually, OpenFeint highscore boards show the top 25 scores – so not just the top 1 or 10. Does that help?

    Peach :)

  9. its really helpful. Thx a lot. I wonder if there is a way to show not only the best player but show the top ten, like old school highscore tables. Is this a restriction in OpenFeint or is there a way to do this?
    all the best

  10. [...] Part 2: Adding OpenFeint Leaderboards to your App [...]

  11. [...] Before continuing, please ensure you have completed Part 1 of the OpenFeint Tutorial and that you have added yourself as a test user, as described in Part 2. [...]

  12. Heh, no worries at all :P

    It actually happens ALL the time; I suppose because people automatically assume if you write code you’re a guy, which is fair enough given the fact that men out number women in that field about a trillion to one ;)

    You were one of the few who actually realized by yourself ^-^;

    Peach :)

  13. Haha thats me! Sorry about that!

    Great post!

 Leave a Reply



You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

Notify me of followup comments via e-mail. You can also subscribe without commenting.