Updating the leaderboard from the code

It is simple to send a new score to the leaderboard from the code. Follow these steps to send the number of coins collected to the leaderboard every time a game ends:

  1. In Xcode, open GameSCNScene.swift.
  2. Add an import statement at the top so we can use the GameKit framework in this file:
            import GameKit 
  3. Add a new function in the GameSCNScene class named updateLeaderboard, as shown here:
     func updateLeaderboard() { if GKLocalPlayer.localPlayer().isAuthenticated { // Create a new score object, with our leaderboard: let highScore = GKScore(leaderboardIdentifier: "highscore") // Set the score value to our coin score: highScore.value = Int64(self.score) // Report the score (wrap the score in an array) GKScore.report([highScore], ...

Get Swift Game Development - Third Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.