Dunfey · Hotel WWDC as data, est. 1983
Front desk everything
Years
Topics

2020 Graphics & Games

WWDC20 · 25 min · Graphics & Games

Tap into Game Center: Leaderboards, Achievements, and Multiplayer

Level up your Game Center integration and enable players to compare scores on leaderboards, earn valuable achievements, and engage with other players. Organize special events like weekly championships, daily showdowns, or 1-hour competitions using recurring leaderboards. Create up to 100 unique achievements for your game. And we’ll show you how to set up real-time or turn-based multiplayer matches for your Game Center players. If you want to learn more about Game Center’s interface, Dashboard, and player profiles, check out “Tap into Game Center: Dashboard, Access Point, and Profile.” And for more about preparing your game’s interface for these new capabilities, see “Design for Game Center.”

Watch at developer.apple.com ↗

Transcript all transcripts

Code shown on screen · 8 snippets

Submitting a score swift · at 4:05 ↗
// Use the class method to submit score to one or more leaderboards at once
GKLeaderboard.submitScore(self.points, context: 0, player: GKLocalPlayer.local,
    leaderboardIDs: ["my.leaderboard.id"]) { error in
}
Submitting a score - recurring leaderboard ID swift · at 4:30 ↗
// Use the class method to submit score to one or more leaderboards at once
GKLeaderboard.submitScore(self.points, context: 0, player: GKLocalPlayer.local,
    leaderboardIDs: ["my.recurring.leaderboard.id"]) { error in
}
Submitting to a specific occurrence of a recurring leaderboard swift · at 4:48 ↗
// Submitting to a specific occurrence of a recurring leaderboard
GKLeaderboard.loadLeaderboards(IDs:["my.recurring.leaderboard.id"]) { (fetchedLBs, error) in
    if let lb = fetchedLBs?.first {
       lb.submitScore(self.points, context: 0, player: GKLocalPlayer.local) { error in
       }
    }
}
Launching in-game UI swift · at 5:33 ↗
// Launching in-game UI

// Display a list of leaderboards
let vc = GKGameCenterViewController(
             state: .leaderboards)
vc.gameCenterDelegate = self
present(vc, animated: true, completion: nil)


// Or directly display scores for a specific leaderboard
let vc = GKGameCenterViewController(
             leaderboardID: "YOUR_ASC_LEADERBOARD_ID",
             playerScope: .global,
             timeScope: .allTime)
vc.gameCenterDelegate = self
present(vc, animated: true, completion: nil)
Accessing previous occurrence swift · at 7:14 ↗
// Accessing previous occurrence

// Load current occurrence of a recurring leaderboard
GKLeaderboard.loadLeaderboards(IDs:["my.recurring.leaderboard.id"]) { (fetchedLBs, error) in
    if let current = fetchedLBs?.first {
       // Load previous occurrence using the current occurrence
       current.loadPreviousOccurrence { (prevOccurrence, error) in
           // Do something with the previous occurrence
       }
    }
}
Reporting achievement progress swift · at 14:34 ↗
if let achievement = GKAchievement(identifier: identifier) {
    achievement.percentComplete = percentComplete
    GKAchievement.report([achievement]) { error in
        if let error = error {
            print("Error in reporting achievements: \(error)")
        }
    }
}
Displaying Game Center achievements swift · at 16:05 ↗
// Showing the Game Center achievements page

let viewController = GKGameCenterViewController(state: .achievements)
viewController.gameCenterDelegate = self
present(viewController, animated: true)
Check if personalized communication is restricted swift · at 23:50 ↗
// Check if personalized communication is restricted
if GKLocalPlayer.local.personalizedCommunicationRestricted {
    // Disable UI for Voice chat
}