Creating a class to handle Game center and Play services for Solar2D / Corona SDK

/ #Solar2D


Structuring code in large projects can be hard, but it's really important and it can be rewarding in the long run.

I'm not going to talk about how the whole structure of a project should be. This guide will only show you how to set up a helper class to interact with Game center and Play services.

I like to put helper classes inside a folder called "classes". Inside this folder, create a new file called "gamecenter.lua". This will contain settings and helper functions.

The basics

--
-- Import
local json = require("json")


--
-- Create class and set variables
local gamecenter = {}
gamecenter.gpgs = nil
gamecenter.gameCenter = nil

local platform = system.getInfo("platform")
local env = system.getInfo("environment")


--
-- Return
return gamecenter

First, we just import json. This will be used to print error messages to the console. Next, we init the class (gamecenter) and assign to variables to it. We also need to know which platform and environment the game is running on. On the bottom of the file, we just return the class so it's usable throughout the game.

Next, we're going to need a way to initialize Game center og Play services depending on which platform you're on. So below the variables where we get the platform and environment, add these functions.

--
-- Play services
gamecenter.gpgsInitListener = function(event)

    if not event.isError then
        if event.name == "login" then
            print( json.prettify(event) )
        end
    end
end


--
-- Apple Game Center init
gamecenter.gcInitListener = function(event)

    if not event.isError then
        if event.data then
            print( json.prettify(event) )
        end
    end
end


--
-- Init
function gamecenter:init()
    
    -- Android / Play services
    if (platform == "android" and env ~= "simulator") then
        gamecenter.gpgs = require("plugin.gpgs.v2")
        gamecenter.gpgs.login({ userInitiated = true, listener = gamecenter.gpgsInitListener })

    -- iOS / Game center
    elseif (platform == "ios" and env ~= "simulator") then
        gamecenter.gameCenter = require("gameNetwork")
        gamecenter.gameCenter.init("gamecenter", gamecenter.gcInitListener)
    end
end

The first function is the listener for Play services. When the user has been logged in, this function will be called. The next function is the listener for Game center. Both of these will print a json dump of the event if it's not an error.

The last function (init) is where we require the correct plugin based on platform and environment. We also connect to Game center and set the listener or login to Play service.

This is the most basic version of this helper class. In the next part I'll show you how to implement functions for showing a leaderboard.

Showing a leaderboard

Leaderboards are an essential part of most games. Here is the code you need to make it possible to show a leaderboard. First add these to variables together with "gpgs" and "gameCenter".

gamecenter.android_leaderBoardId = "LEADERBOARDID"
gamcecenter.ios_leaderBoardId = "LEADERBOARDID"

And then add this function below the "init" function.

--
-- Open leaderboard
function gamecenter:openLeaderboard()
    if gamecenter.gpgs then
        gamecenter.gpgs.leaderboards.show( {
            leaderboardId = gamecenter.android_leaderBoardId
        } )
    elseif gamecenter.gameCenter then
        gamecenter.gameCenter.show( "leaderboards", {
            leaderboard = {
                category = gamecenter.ios_leaderBoardId
            }
        })
    end
end

If you know Corona SDK / Solar2D, this code should be familiar to you. If you don't, this is just a simple function where we first check if gamecenter.gpgs has a truthy value or if the gamecenter.gameCenter is truthy. Based on this we know if we should open the leaderboard for iOS or for Android.

Submitting a score to the leaderboard

There's no point of having a leaderboard if you can't submit scores to them. Here's a function for doing this. This can be placed below the openLeaderboard function.

--
-- Submit score
function gamecenter:submitScore(score)

    if gamecenter.gpgs then
        gamecenter.gpgs.leaderboards.submit({
            leaderboardId = gamecenter.android_leaderBoardId,
            score = score,
            listener = function() print("The score is submitted") end
        })

    elseif gamecenter.gameCenter then
        gamecenter.gameCenter.request("setHighScore", {
            localPlayerScore = {
                category = gamecenter.ios_leaderBoardId,
                value = score
            },
            listener = function() print("The score is submitted") end
        })
    end
end

Again, this is a really basic function when it comes to Corona SDK. We create a function which receives a parameter (score). Inside the function we check if it's iOS or Android. And then just call either the submit function or a request function based on this. When the score is submitted, we'll print that to the log.

That's it!

That's basically it. Now you got a reusable file you can use in all of your Corona SDK / Solar2D projects. Adding achievements is almost identically to the leaderboard process.

This way of making small "plugins" for your games will definitely help you keeping the code base as clean as possible. If you've got any questions about the code or if you have a tip on how to improve this, please don't hesitate to leave a comment below!

Comments

No comments yet...

Add comment

Info

Please log in to comment!

Newsletter

Subscribe to my weekly newsletter. One time per week I will send you a short summary of the tutorials I have posted in the past week.