Now that our leaderboard is ready, we need to actually log our scores to it.
Locate the scene:Game(event)
function which responds to a game being over.
To be able to submit high scores, we have to be connected to the network; otherwise, we should just leave the game:
scene:addEventListener('Game', scene) function scene:Game( event ) if social.loaded then else storyboard.gotoScene('splash') end end
When a game ends, collect the scene's score variable and submit it to the game network:
if social.loaded then gameNetwork.request('setHighScore', { localPlayerScore = {category = 'overall_allTime', value = self.ScoreCurrent}; } ) else storyboard.gotoScene('splash')
Then add a listener so that the game can carry on once the scores are submitted and the leaderboard has responded that it has received them:
local function displayScores( event ) end scene:addEventListener('Game', scene) function scene:Game( event ) if social.loaded then gameNetwork.request('setHighScore', { localPlayerScore = {category = 'overall_allTime', value = self.ScoreCurrent}; listener = displayScores; } ) else
In the listener for the score submission, we'll fall back on a built-in leaderboard display to show the results of the submitted score and how it ranks:
local function displayScores( event ) gameNetwork.show('leaderboards', { leaderboard = { category = 'overall_allTime', timeScope = 'AllTime'; }; } ) end
This request will also get a listener, a simple closure that will send the game back to the splash screen once the user dismisses the leaderboard display:
gameNetwork.show('leaderboards', { leaderboard = { category = 'overall_allTime', timeScope = 'AllTime'; }; listener = function( event ) storyboard.gotoScene('splash') end; } )
We used the results of our Game Center initialization call from the last step to determine whether scores could be sent to a leaderboard, then forwarded them if the leaderboard was available. Finally, we used Game Center's own built-in facilities to show the current status of the leaderboard before returning to the splash screen.
In addition to the functions we've used here that ask the network itself to display its info, the gameNetwork
package also includes requests that allow you to scan leaderboards yourself, collect data about other players, format your own lists of achievements, and so on. These allow you to take over some of the coding work in exchange for much more control in making the displayed leaderboards and achievements fit the appearance of your game.
18.224.63.87