Friday, October 19, 2012

Tutorial 21 - Integrating Game Center (Part 3)

21.1 On the Shoulders of Giants...

Juan Belón (@juaxix over on the Codea Forums) has already done a lot of the hard work required to get Game Center up and running on your Codea App. If all you need is to submit scores to a single leaderboard then just follow Juan's instructions at this post. Juan has also provided the ability to play and stop mp3's from a Codea App.

We want to be able to submit scores to multiple leaderboards (Easy, Medium and Hard) and incorporate achievements so we need to add to the code that Juan has generously contributed. To this end we will fork his Game Center class on GitHub.

We will start by updating the Codea Runtime Objective C code.

21.2 Modifications to LuaState

In Xcode, make sure that you are showing the Project Navigator (top left button on the tool bar). Open up the Frameworks group, then Codea -> Backend. In Backend, click on the LuaState.m file. Within this file you will see a method called - (void)create{ }. To this method add the following:

//  Game Center Functions:

//  Play Music

21.3 Modifications to OSCommands

In the same directory as LuaState you will see OSCommands.h and OSCommands.m, click on the header file OSCommands.h and after int alert(struct lua_State *L) add the following:

//  Game Center:
    int aGameCenter_start(struct lua_State *state);
    int aGameCenter_isGameCenterAvailable(struct lua_State *state);
    int aGameCenter_showEasyLeaderboard(struct lua_State *state);
    int aGameCenter_showMediumLeaderboard(struct lua_State *state);
    int aGameCenter_showHardLeaderboard(struct lua_State *state);
    int aGameCenter_reportEasyScore(struct lua_State *state);
    int aGameCenter_reportMediumScore(struct lua_State *state);
    int aGameCenter_reportHardScore(struct lua_State *state);
    int aGameCenter_reportAchievementIdentifier(struct lua_State *state);
    int aGameCenter_showAchievements(struct lua_State *state);
    int aGameCenter_resetAchievements(struct lua_State *state);
  //  Play Music
    int playMusic(struct lua_State *L);
    int stopMusic(struct lua_State *L);

Then in the implementation file, OSCommands.m add the following:

static aGameCenter_Codea *CodeaGameCenter;

int aGameCenter_start(struct lua_State *state){
    NSLog(@"Starting Game Center");
    if (CodeaGameCenter==nil){
        CodeaGameCenter = [[[aGameCenter_Codea alloc] init] autorelease];

    [CodeaGameCenter start];
    return 0;

int aGameCenter_isGameCenterAvailable(struct lua_State *state){
    return [CodeaGameCenter isGameCenterAvailable];

int aGameCenter_showLeaderboard(struct lua_State *state) {
    [CodeaGameCenter showLeaderboard];
    return 0;

int aGameCenter_reportEasyScore(struct lua_State *state) {
    [CodeaGameCenter reportEasyScore:lua_tonumber(state,1)];
    return 0;

int aGameCenter_reportMediumScore(struct lua_State *state) {
    [CodeaGameCenter reportMediumScore:lua_tonumber(state,1)];
    return 0;

int aGameCenter_reportHardScore(struct lua_State *state) {
    [CodeaGameCenter reportHardScore:lua_tonumber(state,1)];
    return 0;

int aGameCenter_reportAchievementIdentifier(struct lua_State *state) {
    [CodeaGameCenter reportAchievementIdentifier:lua_tonumber(state,1)];
    return 0;

int aGameCenter_showAchievements(struct lua_State *state) {
    [CodeaGameCenter showAchievements];
    return 0;

int aGameCenter_resetAchievements(struct lua_State *state) {
    [CodeaGameCenter resetAchievements];
    return 0;

int playMusic(struct lua_State *L){
    [CodeaGameCenter playMusic:lua_tonumber(L,1)];
    return 0;

int stopMusic(struct lua_State *L){
    [CodeaGameCenter stopMusic];
    return 0;

21.4 Modifications to the aGameCenter_Codea Class

The final change in Xcode is to add the updated version of the aGameCenter_Codea class. You can download the forked version of Juan's code here. Add these two files to the Supporting Files group in the runtime.

21.5 Modifications to your Codea App

Once you start adding the Game Center functionality you wont be able to compile and run your App in Codea anymore (since these functions are only defined in the runtime). Consequently, debug all of your code apart from the Game Center specific parts before you make the following modifications. 

After you are in Xcode and using the runtime, you can still update your Lua code but you need to force the runtime to reload the Lua files into the documents directory. You can do this by changing the version number in the Lua info.plist which can be found in Project.codea. 

This number just needs to be different (not necessarily larger) to what it was the last time you compiled, to force an upload. Alternatively, if you get sick of doing this each run-test cycle (like we did), you can go into the CodifyAppDelegate.m file, find the - (BOOL) migrateProjectAtPath:(NSString*)path toPath:(NSString*)destPath method and comment out the following code as shown below.
// NSString* oldVersion = [self versionForProjectAtPath:destPath];
// NSString* newVersion = [self versionForProjectAtPath:path];

// if ([oldVersion isEqualToString:newVersion]) 

//    {
//        return YES;
//    }

Should you need to add sprites, you can save these directly into the dropbox folders in the runtime but you may need to do a clean build (select Product from the menu bar and then Clean) before Xcode will see them.

As part of the Game Center update of MineSweeper (v1.5) we took the opportunity to tweak the code based on feedback from the Codea Forum. In particular:
  1. There is a new MineSweeper logo on the Menu screen courtesy of @derhannes (you can see their web site at:
  2. The falling mines on the Menu Screen now start dropping from above the screen which makes the animation look smoother. Thanks to @Fred for this suggestion.
  3. The textBox used to enter the players name, if a new high score is achieved, now handles a changing orientation (e.g. Portrait to Landscape). Thanks to @West for finding this bug.

Once you have added all the code above, the actual Game Center implementation in Lua is very simple. In the Setup() function in Main add the following code:

-- Initialise Game Center if it is available (requires a device running iOS 4.1
-- or later and the App needs to be running within the Codea run time).
    if aGameCenter_isGameCenterAvailable() then
        print("Game Center Started.")
        print("Game Center not available")

Then you just need to report your scores and achievements as they happen. For example in MineSweeper when the game is won we do the following achievement checks:

-- Check if any Achievements were completed once game
-- has been won.
        if gameDifficulty == stateEasy then
            if readLocalData("easyWinner") == nil then
                saveLocalData("easyWinner", "YES")
                print("Easy Winner Achievement.")
        if gameDifficulty == stateMedium then
            if readLocalData("mediumWinner") == nil then
                saveLocalData("mediumWinner", "YES")
                print("Medium Winner Achievement.")
        if gameDifficulty == stateHard then
            if readLocalData("hardWinner") == nil then
                saveLocalData("hardWinner", "YES")
                print("Hard Winner Achievement.")
        if readLocalData("gamesPlayed") == nil then
            gamesPlayed = 0
            gamesPlayed = readLocalData("gamesPlayed")
        gamesPlayed = gamesPlayed + 1
        saveLocalData("gamesPlayed", gamesPlayed)
        if gamesPlayed == 10 then
            if readLocalData("decathlon") == nil then
                saveLocalData("decathlon", "YES")
                print("Decathlon Achievement.")
        if gamesPlayed == 100 then
            if readLocalData("centurion") == nil then
                saveLocalData("centurion", "YES")
                print("Centurion Achievement.")

To save high scores onto the relevant leader board we updated the saveHighScore function as shown below.

function saveHighScore(d)
    -- Build the high score data into a string which is saved
    -- using saveLocalData(key, value). Also save gameTime as
    -- the score on Game Center for the appropriate leader board
    -- (easy, medium or hard).
    -- n = playerName
    -- t = gameTime
    -- d = [current date] not used in this version
    playerName = textBox.text
    print("New High Score by: "..playerName)
    local hsDataString = string.format("return {\"%s\", %d}", playerName, gameTime)
    if gameDifficulty == stateEasy then
        saveLocalData("easyHighScore", hsDataString)
    elseif gameDifficulty == stateMedium then
        saveLocalData("mediumHighScore", hsDataString)
    elseif gameDifficulty == stateHard then
        saveLocalData("hardHighScore", hsDataString)
    highScoreSaved = true