I'm trying to implement Game Center in a SpriteKit game I am working on.
Leaderboards started working easily, but all the tutorials I could find online are either written in objc or are from 2015 or earlier I am using Swift 5 and Xcode 11.5. The achievements are set up on App Store Connect and everything works properly, except the achievements do not load and aren't getting marked as completed. Most of my Game Center code is in the GameScene.swift file, but the Game Center page is shown with the GameViewController file.
Here is my main Game Center code.
// MARK: Game Center
func authenticateLocalPlayer() {
let localPlayer: GKLocalPlayer = GKLocalPlayer.local
localPlayer.authenticateHandler = {(ViewController, error) -> Void in
if((ViewController) != nil) {
// 1. Show login if player is not logged in
GVC.present(ViewController!, animated: true, completion: nil)
} else if (localPlayer.isAuthenticated) {
// 2. Player is already authenticated & logged in, load game center
self.gcEnabled = true
// Get the default leaderboard ID
localPlayer.loadDefaultLeaderboardIdentifier(completionHandler: { (leaderboardIdentifer, error) in
if error != nil { print(error as Any)
} else { self.gcDefaultLeaderBoard = leaderboardIdentifer! }
})
} else {
// 3. Game center is not enabled on the users device
self.gcEnabled = false
print("Local player could not be authenticated!")
print(error as Any)
}
}
}
func addPoints(pts: Int64) {
// Add a point.
score += pts
// Submit score to Game Center leaderboard
let bestScoreInt = GKScore(leaderboardIdentifier: LEADERBOARD_ID)
bestScoreInt.value = score
GKScore.report([bestScoreInt]) { (error) in
if error != nil {
print(error!.localizedDescription)
} else {
print("Best Score submitted to the leaderboard!")
}
}
}
func reportAchievements() {
print(achievements)
guard gcEnabled else {
print("Game Center Not Enabled")
return
}
GKAchievement.report(achievements) {
error in guard error == nil else {
print("""
GKAchievement Error:
\(error as Any)
""")
return
}
}
}
var gcAchievements:[String:GKAchievement]? = nil
func loadAchievements() {
var allAchievements = [GKAchievement]()
GKAchievement.loadAchievements(completionHandler: { (allAchievements, error: Error?) -> Void in
if error != nil {
print("Game Center: could not load achievements, error: \(String(describing: error))")
} else {
for anAchievement in allAchievements! {
if let oneAchievement = anAchievement as? GKAchievement {
self.gcAchievements?[oneAchievement.identifier] = oneAchievement
}
}
}
})
}
This is called from elsewhere in GameScene.swift as:
achievements = []
achievements.append(AchievementName)
print(achievements)
print(AchievementName.isCompleted)
if A_leaveNosund.percentComplete == 0 {
print("Rewarding for achievement completion...")
A_leaveNosund.percentComplete = 100
reportAchievements()
addPoints(pts: 1)
}
(AchievementName represents my achievement's name, and is declared in the same class, earlier in the file).
Does anyone know what needs to be modified to make achievements load and get marked as completed in Swift 5?
Any help would be appreciated.
Thanks,
ajldev