GameCenter Integration Leaderboards and Achievements

This article uses JMA’s gaming software to demonstrate these features. To download the games, please click here.

MonoGame integrates with the Game Center using the Guide and SignedInGamer classes. Here are the steps to add GameCenter to your game:

First, you a testing device and not the simulator. Make sure that your device isn’t jail broken.

On your home page, go to Manage Users > Test User and make two accounts. You must have two accounts to post a score, before any data shows in the leaderboard.

App Details > Manage Game Center > Enable

Complete the form as shown below:

image

It can take up to 24 hours for the game center to work with your game, so if you don’t see any scores then wait. Please note the Leaderboard ID, which is sent to Apple in the code.

When you save, go back to achievements. Add an achievement as shown below:

image

Note the Achievement ID.

When you submit the app, it needs to have the game center info set-up.

 image

Testing will fail if the game has developer rejected status. It must says upload received, in review or ready for sale.

Once you have created this data, you can add the code. Here is my code:

using System;
using Microsoft.Xna.Framework.GamerServices;
using SpaceRacer.Common;

public class GameCenterManager
{
    private string _categoryOrLeaderBoardID;
    private SignedInGamer _gamer;
    private long _score = 0;
    Level _level;
    
    public GameCenterManager (string categoryOrLeaderBoardID, long Score, Level level)
    {
        _categoryOrLeaderBoardID = categoryOrLeaderBoardID;
        _gamer = GetSignedInGamer ();
        _score = Score;
        _level = level;
    }
    
    private bool IsSignedIntoGameCenter ()
    {
        if ((Gamer.SignedInGamers.Count > 0) && (Gamer.SignedInGamers [0].IsSignedInToLive)) {
            return true;
        }
        return false;
    }
        
    private SignedInGamer GetSignedInGamer ()
    {
        if (IsSignedIntoGameCenter ()) {
            return Gamer.SignedInGamers [0];
        }
        return null;
    }
        
    public void SaveScoreToIosGameCenter ()
    {        
        if (IsSignedIntoGameCenter ()) {
            if (_gamer != null) {
                _gamer.UpdateScore (_categoryOrLeaderBoardID, _score);
            }
        }
    }
    
    /// <summary>
    /// Awards the achievement, based on score
    /// </summary>
    public void AwardAchievements ()
    {
        if(_score >= 10000){
            _gamer.AwardAchievement ("com.jmawebtechnologies.spaceracermt.scoretenthousand");
        }
        
        if(_level.LevelNumber >= 5){
            _gamer.AwardAchievement("com.jmawebtechnologies.spaceracermt.levelfive");    
        }
    }
}

The award achievement argument is the achievement ID. In this case, I award achievements based on score and completed level.

To use the code, just instantiate the GameCenterManager class and call these methods:

 

GameCenterManager gcm = new 
                    GameCenterManager("com.jmawebtechnologies.spaceracermyleaderboard", score * player.ScoreMultiply, currentLevel);
                gcm.SaveScoreToIosGameCenter();
                gcm.AwardAchievements();
Posted: 7/30/2012 9:19:05 AM