According to the documentation, I should implement the GKTurnBasedEventListener
protocol's player(_:receivedTurnEventFor:didBecomeActive:)
method to receive turn-based event information.
Although it's documented as GKTurnBasedEventListener
, Apple now recommends that we implement the GKLocalPlayerListener
protocol instead of several separate protocols including GKTurnBasedEventListener
.
From the docs:
Adopt the GKLocalPlayerListener protocol to handle a variety of Game Center events instead of the individual GKChallengeListener, GKInviteEventListener, GKSavedGameListener, and GKTurnBasedEventListener protocols. Then implement the player(_:receivedTurnEventFor:didBecomeActive:) and other GKTurnBasedEventListener protocol methods to handle turn-based events that occur throughout a match.
So, I've implemented GKLocalPlayerListener
.
At first, I was using the default Game Center interface -- and player(_:receivedTurnEventFor:didBecomeActive:)
was called as expected (when the user started a new match, for example).
However, now that I'm using my own custom interface, the method is no longer called, ever. It's not called on the Simulator, and it's not called on device.
So, here's what I'm doing:
- Set up the
GKLocalPlayerListener
protocol:
class GameCenterMatches: UIViewController, GKLocalPlayerListener {
...
}
- Add the
player(_:receivedTurnEventFor:didBecomeActive:)
method to the class:
func player(_ player: GKPlayer, receivedTurnEventFor match: GKTurnBasedMatch, didBecomeActive: Bool) {
...
}
- Immediately after authenticating with Game Center, register the class with the local player (
GameCenterAuth.player
isGKLocalPlayer.local
, andSceneDelegate.gameCenterMatches
isGameCenterMatches()
):
GameCenterAuth.player.register(SceneDelegate.gameCenterMatches)
As an example of something I believe should trigger player(_:receivedTurnEventFor:didBecomeActive:)
, here's how I'm creating new matches:
let request = GKMatchRequest()
//Configure the request
GKTurnBasedMatch.find(for: request) { match, error in
...
}
Question: Why isn't player(_:receivedTurnEventFor:didBecomeActive:)
being called for my custom interface events, and how can I make it work?