Native Game Center Tutorial

Here’s a quick tutorial (before I rush off to work haha) for the basics until Ansca posts something more detailed. Keep in mind I am assuming you have enabled Game Center in ITC and have put up leader boards and achievements.

  1. Initialize Game Center
  
local gameNetwork = require("gameNetwork")  
  
--Callback  
local function finishAuth(event)  
 gamenetworkEnabled = event.data  
end  
  
function authorize()  
 gameNetwork.init("gamecenter", finishAuth)  
end  
  
  1. Post a High Score, where board is the leaderboard id, and score is the player’s score
  
function updateHighScore(board, score)  
 if gamenetworkEnabled == true then  
 gameNetwork.request("setHighScore", { localPlayerScore={ value=tonumber(score), category=board }})  
 end  
end  
  
  1. Unlock an Achievement, where identifier is the achievement id. PAY CLOSE ATTENTION, identifier is spelled wrong (like it is in the daily build notes) in the api.
  
function unlockAchievement(identifier)  
 if gamenetworkEnabled == true then  
 gameNetwork.request("unlockAchievement", { identifer=identifier, percentComplete=100, showsCompletionBanner=true })  
 end  
end  
  1. Basic Leaderboard List (suitable for a button)
function getLeaderboards()  
 if gamenetworkEnabled == true then  
 gameNetwork.show("leaderboards")  
 else  
 authorize()  
 end  
end  
  1. Basic Achievements List (suitable for a button)
function getAchievements()  
 if gamenetworkEnabled == true then  
 gameNetwork.show("achievements")  
 else  
 authorize()  
 end  
end  
  1. Basic Friend Request (suitable for a button)
function getFriends(text)  
 if gamenetworkEnabled == true then  
 gameNetwork.show("friendRequest", { message=text })  
 else  
 authorize()  
 end  
end  
  
function getFriendsBasic()  
 if gamenetworkEnabled == true then  
 gameNetwork.show("friendRequest")  
 else  
 authorize()  
 end  
end  

I hope this tides you all over until official APIs. Yes, there’s plenty of stuff not listed here (such as advanced fetching and level specific leader boards. If no one gets around to it, I’ll post those later.

Good Luck!

(Keep in mind you need build 725 or higher) [import]uid: 36054 topic_id: 20394 reply_id: 320394[/import]

No one will ever see that I made a typo. Edit FTW :smiley: [import]uid: 36054 topic_id: 20394 reply_id: 79726[/import]

@blaster - Much appreciated! Sure your mini-tuto here together the BIG Ansca blog that probably is coming out very soon (maybe today?! :slight_smile: ) will HELP A LOT!

Cheers,
Rodrigo. [import]uid: 89165 topic_id: 20394 reply_id: 79731[/import]

thanks a lot, i will give it a try very soon!

:smiley: [import]uid: 90610 topic_id: 20394 reply_id: 79724[/import]

what about using Game Center and OpenFeint together?

both has same name gameNetwork

gameNetwork.init( "openfeint", of\_product\_key, of\_product\_secret, "My Game", of\_app\_id ) [import]uid: 96683 topic_id: 20394 reply_id: 79735[/import]

This is a fantastic contribution! [import]uid: 52491 topic_id: 20394 reply_id: 79741[/import]

Have you had any luck getting gameNetwork.request( “loadAchievements”, syncCallback ) to work? I get an invocation to syncCallback but the event parameter is an table empty of anything other than the provider type. There is no .data member as the release notes would indicate. [import]uid: 117383 topic_id: 20394 reply_id: 79774[/import]

@blasterv, thank you so much for the tutorial. I’m bookmarking this thread for me to look at when I implement GC.

Thanks again!
Naomi [import]uid: 67217 topic_id: 20394 reply_id: 79788[/import]

Sample code and documentation is still forthcoming. But a few quick replies/points.

  • All Game Center APIs have listeners now. Particularly for init and request, you should always have a listener to handle potential network errors.

  • Yes, I misspelled ‘identifier’ in a few places. Sorry about that. Thanks for catching that. I will fix that in the next mainline push I do for Game Center. Please note that your code will break when I fix this.

  • You cannot use Game Center and OpenFeint in the same run. Once you call init, you are stuck with that network until the app exits.

  • There is a new build.settings option (components) that let’s you control whether OpenFeint is included or not.

  • For gameNetwork.request( “loadAchievements”, syncCallback ), I think Apple only returns the user’s completed achievements. If you haven’t completed any, you get nil back. Can you verify that?

[import]uid: 7563 topic_id: 20394 reply_id: 79878[/import]

You can get by without a listener for requests as long as you’re not interested in being graceful (i.e. just have a button that doesn’t provide the user with feedback if no connection is there). You’d think Apple would not allow this in practice but even popular apps such as Angry Birds do not provide the user with Game Center errors. The button will just not work, hahaha.

You should definitely have one for init though, if you don’t, you can get into a situation where your “request” buttons work but are totally blank with no information on them. Ouch.

@iplayalot Unless you really love OpenFeint, you shouldn’t be using it on iOS. Apple will not promote games that use third party gaming networks. So you’re only hurting yourself by including it. That’s why we’ve all been pushing for native support for so long. With that said if you want OpenFeint + GC, you can do it the old way with the OFGameCenter.plist file and OpenFeint will pass along stuff to GC. That sucks though.

With that said I still use OpenFeint on Android, and my GameNetwork code looks something like this:

function updateHighScore(board, score)  
 if gamenetworkEnabled == true then  
 if isAndroid == false then  
 gamenetwork.request("setHighScore", { localPlayerScore={ value=tonumber(score), category=board } })  
 else  
 gamenetwork.request("setHighScore", { leaderboardID=board, score=score })  
 end  
 end  
end  

P.S. Yes I check for false, I think differently :slight_smile: [import]uid: 36054 topic_id: 20394 reply_id: 79886[/import]

For network failures like setHighScore, you’ll want to handle the error. In that case, you want to save the high score to disk and try reporting it again in the future. That way the user won’t permanently lose their high score. [import]uid: 7563 topic_id: 20394 reply_id: 79888[/import]

For loadAchievements I was seeing a nil response for data even when I had an unlocked achievement. They were showing as unlocked in Game Center at any rate. If there are any other tests I can do for you please let me know. [import]uid: 117383 topic_id: 20394 reply_id: 79892[/import]

@ewing You’re right. When I dealt with OpenFeint I just decided to re-report everything every so often. I could probably be more efficient now :slight_smile: However, in looking at Apple’s documentation it looks like if you set your minimum OS to 5.0, you don’t even have to worry about dealing with reporting again as Apple does it for you.

[import]uid: 36054 topic_id: 20394 reply_id: 79893[/import]

@ewing Feel bad about double posting (hahaha), but I’m curious about something with the friendRequest.

If I do this:

 gameNetwork.show("friendRequest", { message="hello" }) 

I will get a friend request pop-up with the text “hello” set by default.

If I do this:

 gameNetwork.show("friendRequest") 

I will get a friend request pop-up with the text “John Smith would like to be your friend” set by default. (John Smith being the player’s name)

Is there any way to include your own message (such as in the first example), but still include the player’s name (like in the second example) ?
[import]uid: 36054 topic_id: 20394 reply_id: 79895[/import]

Can you use the ‘alias’ property from the loadLocalPlayer table to get the name and construct your own string for the message?
[import]uid: 7563 topic_id: 20394 reply_id: 79900[/import]

@ewing Oh damn. I totally didn’t see that one in the daily notes. Thank you. I’ll test it later, but I’m sure it’ll work :slight_smile: [import]uid: 36054 topic_id: 20394 reply_id: 79901[/import]

I’m working on trying to add this and I’m missing something, but it’s got to be on the iTunes Connect side since I’m getting a message that:

This game is not recognized by Game Center

in an alert box.

So my live game (version 1.2) does not support game center.
I’m building an Ad Hoc 1.3. I’ve set up version 1.3 in iTunes connect, created a leader board, gone into the version and enabled Game Center (for 1.2, the button to enable seems to be non-clickable)

Any one know what I missed? Do I have to get game center turned on for my existing version? Do I need to upload and reject 1.3 to prime something?

How can I test this?

[import]uid: 19626 topic_id: 20394 reply_id: 79923[/import]

@robmiracle Are you using a Sandbox Game Center account for testing? [import]uid: 36054 topic_id: 20394 reply_id: 79924[/import]

I dunno, I’m logged into GC. I’m not seeing anything that says sandbox.

I have several accounts from my failed attempts to get OpenFeint to work.

I don’t see a place to say “this is a sandbox” account.
[import]uid: 19626 topic_id: 20394 reply_id: 79925[/import]

@robmiracle
http://developer.apple.com/library/ios/#documentation/NetworkingInternet/Conceptual/GameKit_Guide/GameCenterOverview/GameCenterOverview.html

Read the bottom. Enjoy :slight_smile: [import]uid: 36054 topic_id: 20394 reply_id: 79926[/import]