Title: Apple Game Frameworks and Technologies: currentPlayer = nil (page 412)
Given my recent track record on my problem posts, I would not blame anyone for giving me a day or so to discover what I did wrong.
In this case. in the canTakeTurn()
func called from GameScene.setupRemoteGame()
, the match.currentParticipant?.player
is nil. Both instances are getting the same nil result, so both instances think it’s the other player’s turn. So far, I’m not seeing a way to reset things, but I would think that, if it’s possible that the match.currentParticipant?.player
can be nil, then there should be some code to handle that state. (BTW, the match.currentParticipant
is not nil. Here’s a bit of print output attempting to debug this:
Attempting to load the lobby scene.
player( <GKLocalPlayer: 0x600001aa6100>(alias:App Tester 2 name:App Tester 2 status:(null) friendBiDirectional:0 friendPlayedWith:0 friendPlayedNearby:0 acceptedGameInviteFromThisFriend:0 initiatedGameInviteToThisFriend:0 automatchedTogether:0)
receivedTurnEventFor:<GKTurnBasedMatch 0x600003a716e0 - matchID:2dc64251-a2ed-4d8b-a3cf-6e43530492eb bundleID:com.mikecargal.hogdice status:GKTurnBasedMatchStatusMatching message:'' creationDate:2021-01-26 15:43:22 +0000 currentParticipant:<GKTurnBasedParticipant 0x6000036be7b0 - status:Matching matchOutcome:None lastTurnDate:(null) timeoutDate:(null)> participants:<GKTurnBasedParticipant 0x6000036be9d0 - status:Active matchOutcome:None lastTurnDate:2021-01-26 15:45:48 +0000 timeoutDate:(null)>,<GKTurnBasedParticipant 0x6000036be7b0 - status:Matching matchOutcome:None lastTurnDate:(null) timeoutDate:(null)> matchData.length:151 matchDataMaximumSize:65536 exchanges:(null) state:4>
didBecomeActive:true
-- currentplayer: nil
Attempting to load the game scene using Game Center Match data.
Attempting to load the game scene.
for context this is where most of this output comes from:
func player(_ player: GKPlayer,
receivedTurnEventFor match: GKTurnBasedMatch,
didBecomeActive: Bool)
{
print("player( \(player)\n receivedTurnEventFor:\(match)\ndidBecomeActive:\(didBecomeActive)")
print(" -- currentplayer: \(match.currentParticipant?.player)")
matchmakerViewController?.dismiss(animated: true, completion: nil)
NotificationCenter.default.post(name: .receivedTurnEvent, object: match)
}
func canTakeTurn() -> Bool {
guard let match = currentMatch else { return false }
let currentParticipant = match.currentParticipant
let currentPlayer = currentParticipant?.player
print("match = \(match)\ncurrentPlayer = \(currentPlayer)\nlocalPlayer = \(GKLocalPlayer.local)")
return match.currentParticipant?.player == GKLocalPlayer.local
}
Going to take the challenge of trying to sort out how to handle this state, but thought I’d pass along…
BTW… an observation: In Chapter 17, the instructions generally go along the lines of “replace // TODO: …” with … That works OK on the first pass, but makes going back through to double check your work a bit challenging.
observation 2: The instructions to respond to certain events by posting a notification and then responding to that notification over in another class, could use a bit of elaboration as to why that’s the thing to do. It’s not clear if it’s just demonstrating another way of getting something done in another class, or if there’s some sort of reason why the “real work” needs to happen on the thread that the Observer is running on (just a wild guess as to what might be behind that)