Report Score to Leaderboard

Before you jump into the code that reports scores to leaderboards, you may want to review the GameData.swift file. In this file, you’ll see there’s a wins property. This is the property you’ll use to track the player’s number of wins. If you switch to the GameOverScene.swift file, you’ll see the didMove(to:) method increments this value by one every time the player wins a game. So, when you report the number of wins to the leaderboard, you’ll actually be passing in the value saved in this property.

All right, let’s get to it.

Switch to the GameKitHelper.swift file. First, add a new static property to hold the Leaderboard ID string, placing it at the top of the class (for easy access):

 // Leaderboard IDs
 static​ ​let​ leaderBoardIDMostWins = ​"net.justwritecode.hogdice.wins"

Be sure to use the ID you set up earlier, for example, {your bundle id}.hogdice.wins.

The next step is to create the method for reporting the score. Below the authenticateLocalPlayer() method, add the following code:

 // Report Score
 func​ ​reportScore​(score: ​Int​, forLeaderboardID leaderboardID: ​String​,
  errorHandler: ((​Error​?)->​Void​)? = ​nil​) {
 guard​ ​GKLocalPlayer​.local.isAuthenticated ​else​ { ​return​ }
 
 if​ ​#available(iOS 14, *)​ {
 GKLeaderboard​.​submitScore​(score, context: 0,
  player: ​GKLocalPlayer​.local,
  leaderboardIDs: [leaderboardID],
  completionHandler: errorHandler ?? {
  error ​in
 print​(​"error: ​​(​​String​(describing: error)​)​​"​)
  })
  } ​else​ {
 let​ gkScore = ​GKScore​(leaderboardIdentifier: leaderboardID)
  gkScore.value = ​Int64​(score)
 GKScore​.​report​([gkScore], withCompletionHandler: errorHandler)
  }
 }

Recently, Apple made some modifications as to how GameKit reports scores to leaderboards. Before iOS 14, GameKit used the GKScore class for reporting. However, this class is now deprecated and was replaced with a new method in the GKLeaderboard class. If you plan to support older devices, which Hog Dice does, you may want to consider using an API availability check (#available) shown in the reportScore(score:forLeaderboardID:errorHandler:) method.

Finally, you need to call this method from the game over scene. Switch to the GameOverScene.swift file, and at the end of the didMove(to:) method, replace the TODO comment with the following code:

 // Report Score
 GameKitHelper​.shared.​reportScore​(score: ​GameData​.shared.wins,
  forLeaderboardID: ​GameKitHelper​.leaderBoardIDMostWins)

Here, you’re passing in the total wins and the value stored in the static leaderBoardIDMostWins property.

Build and run the project, then play a quick game to 25. To make it easier for you to win, play a local game rather than a solo match. This way, you can play against yourself; just make sure Player One wins. Alternatively, you could cheat and tap the dice on the lobby scene, which forces a win. This simulator-only code was added to the existing project for testing purposes only. If you’re curious what it looks like, search for #if targetEnvironment(simulator) inside the project.

When the game ends, click the Leaderboards button, and you’ll see your first win listed:

images/AddingLeaderboardsAndAchievements/build-gc-leaderboard-win.png

(Note that if you were playing the game beforehand, your wins were being recorded, so you may see more than one win listed here.)

You’re well on your way to finishing out this chapter. There’s only one more thing to do: add the code to report achievements.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset