Search code examples
iosxcodeswift3sprite-kitleaderboard

Swift 3 Game Center Leaderboard does not save the value of the score


I have a question, help me understand the topic. Honestly, I've spent several days studying the topic, but it's not working yet.

There is a simple game on SpriteKit, Swift 3.

I'm trying to implement Leaderboard Game Center.

iTunes Connect has already set up, the leaderboard has already created, the test user has already created.

When I start the game, the user logs in as they should. The button to display the board created and I can open my leaderboard in the Game Center.

The problem is that the value of the points scored in the leaderboard is not updated.

My game scores are stored in variable "score" In gamescene.swift, I have two functions for saving and overriding:

func saveHighscore(gameScore: Int) {
        if GKLocalPlayer.localPlayer().isAuthenticated {
        print("\n Success! Sending highscore of \(score) to leaderboard")
        let scoreReporter = GKScore(leaderboardIdentifier: “MY_ID_THERE”)
        scoreReporter.value = Int64(gameScore)
        let scoreArray: [GKScore] = [scoreReporter]
        GKScore.report(scoreArray, withCompletionHandler: {error -> Void in
            if error != nil {
                print("An error has occured: \(String(describing: error))")
            }
        })
    }
}


 func overrideHighestScore(gameScore: Int) {
    UserDefaults.standard.integer(forKey: "highestScore")
    if gameScore > UserDefaults.standard.integer(forKey: "highestScore") {
        UserDefaults.standard.set(gameScore, forKey: "highestScore")
        UserDefaults.standard.synchronize()
        saveHighscore(gameScore: score)
        print(score.hashValue)
    }
}

I call both functions when I press the button

saveHighscore (gameScore: score)

overrideHighestScore (gameScore: score)

The console displays output correctly, for example, when collecting five points the output will be a message called:

Success! Sending the highscore of 5 to the leaderboard

but the value of the variable score in the game center is not sent and there are zero values achieved at the first access to the board

I really hope for your help.

Sincerely, Eugene.


Solution

  • I hope you have properly configured leaderboard at https://itunesconnect.apple.com

    Please refer below screen


    enter image description here


    Also refer below code to save score on leaderboard.

    func saveScoreOnGameCenter()
    {
        let leaderboardID = 111
        let sScore = GKScore(leaderboardIdentifier: leaderboardID)
        sScore.value = Int64(10)
    
        GKScore.reportScores([sScore], withCompletionHandler: { (error: NSError?) -> Void in
            if error != nil {
                print(error!.localizedDescription)
            } else {
                print("Score submitted")
    
            }
        })
    }
    

    Hope this helps you to figure out problem.