Adding Game Center

Hello, for those of you who have published games made in Codea, how did you implement game center?

Search for ‘game center’, a few users have put up tutorials and a fix has been given for the latest runtime. I haven’t done it myself yet so if you have questions hopefully someone else will answer.

When you export your project the code to use game centre is included but commented out, if you read the source you should be able to work out how to call the various functions from your Lua code

@Luatee Ok

@TechDojo Im probely missing something since when I use the iOS simulator, I get a codea error “attempt to index global ‘gamecenter’ (a nil value)” I had an if statement in one of my setup functions that checks if game center is enabled.

That get’s defined in “GameCenterAddon.m” as part of the “gamecenterLibs” table that’s passed to the “didCreateLuaState()” function.

You need to uncomment the line

    // [self.viewController registerAddon:[GameCenterAddon sharedInstance]];

In “AppDelegate.mm” so that the initialisation function is called.

Have a look at my Addon’s for beginners thread (http://codea.io/talk/discussion/6171/implementing-addon-s-for-beginners)

@TechDojo Thanks so much! So does this mean that I have to do all of my lua Game Center programing in Xcode since this would error out in Codea?

Btw, would you know how to call lua functions from AppDelegate.mm? Your other post talks about that, right?

@Goatboy76 - not quite, think of your game centre code as being split into two - firstly there’s the actual act of talking to and uploading data to game centre, this is done in xCode and generally is the same for each game - so much so you could package it up into a library and use it over and over again. Then there is the actual integration with your game that is very specific to how your game is coded / how your scoring works - this bit you do in your lua code by calling the various functions in the gamecentre table.

Now the important thing to remember is that when you are running your code inside codea you can’t actually deal with the “real” game centre so a set of fake methods are provide to “simulate” what would happen (in an ideal world - assuming everything works behind the scenes). Note - if these aren’t provided by Codea then you’ll need to create them, just as you would if creating your own addons.

When you export your project ti xcode you replace the simulated functions by their real C equivalents (normally you’d just comment out the simulated ones - hence the reason I group them together in a single tab) so that when your final exported code is run then the functionality is there.

As for your second question - you don’t call Lua from AppDelegate.mm (or any other Obj-C code), it’s the other way round and you do that by “registering” the functions - using a list that “maps” the lua function name to the specified C function. I’ll try and answer it in a bit more detail on the other thread.

@TechDojo So I need to call my PauseGame function if your playing when you leave the app. So assumed that I would have the objective-c function ‘applicationDidEnterBackground’ call my lua function when it ran.

But now since I have to have the lua code call the objective-c code, I’m confused as how to do this since the objective-c code is the only thing that knows when your application enters the background.

Would you happen to know anything about an apps Codea project data being reset whenever you close the app?

@Goatboy76 The Game Center addon will pause codea automatically if you call showLeaderboards or showAchievment. Is this what you meant?

@jrohanian No, its not but thats good to know. Im trying to have methods in my lua code called when the app goes into the background.

Pretty sure it pauses automatically, must be built into the Codea runetime. You can essentially export to xcode project and basic functionality like that works out of the box. Only have to do tweeks to get add-ons to work.

@Crumble When the app runs in the background, the game doesn’t stop playing automatically, I need to tell it to pause when it enters the background. I tested it.

@Goatboy76 - What you’re talking about is not related to Game Centre but is instead related to the system events that get triggered by iOS when stuff happens.

You’re correct that the methods in AppDelegate.mm are the ones that get called - this is due to the parent class that AppDelegate extends. However I don’t know of any easy way to send messages TO Lua from C. The only thing I can think of at the moment is to have some kind of global status flag in the objective-c code that is set by the applicationWill… applicationDid… functions, you could then write an addon that returned this status and you could poll this function once per frame (in draw() ) and then set your internal app state accordingly - it’s a bit hacky but it should work.

NOTE - I have asked for this repeatedly in the past - I don’t think it should be a big issue for Simeon to call a function like touched(), or orientation() or keyboard() to allow us to respond to these events - IMHO as more and more Codea apps run on the iPhone then this will become more important as your supposed to handle cases where your app is interrupted due to the user getting a phone call or when they press the home button.

@TechDojo I am getting off topic. Thats a bit farther past my skill level right now.

@Goatboy76, @TechDojo I’m fairly certain there is a way to call a Lua function from C

@JakAttak, I think the problem is to do with having access to the Lua state and the context within which to call the function, as you’d have to run the Lua engine to be able to find the bytecode representation of the function as in C this would make no sense, how ever I’d be more than happy to see an example if it’s possible.

@Goatboy76, no worries, just keep practicing and you’ll get there, we all had to start somewhere:)

@TechDojo, have a look at this page: http://www.lua.org/pil/25.2.html

From what i saw, should be no problem incorporating that into an addon

@JakAttak - we’ll that’s my next project after analytics & iAd / IAP support then!

So I added these functions to my project and hooked them up.

function Game:SubmitNewGameCenterHighscore(score)
    
    if gamecenter.enabled then
        gamecenter.submitScore(score)       
    end
    print("submitted new highscore")
end

function Game:ShowGameCenterLeaderBoard()
   
    if gamecenter.enabled then
        gamecenter.showLeaderboard()
    end
end

But when I call ShowGameCenterLeaderBoard, its says “attempt to call showLeaderboard, a nil value”. gamecenter.enabled calls fine. I added a default leaderboard to itunes connect and I assume its made properly. Does anything about this look wrong?

Did you follow all of the above instructions on this and the other thread - have you uncommented the register function? To be honest I haven’t actually used Game Center but I’m sure someone on the forum has.