Apple Game Frameworks and Technologies: currentPlayer = nil

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)

I’m not quite to the bottom of things yet, but I have observed something that, I think, throws a kink in the book’s logic.

When I set up my tester account, apple assigned a name of “Player_4939364543”, I didn’t really like that, so I worked to change it to “App Tester 2” on my simulator. When I get into the addPlayer() function, I’ve received “App Tester 2” from the loadGameCenterGame() function, but already have “Player_4939364543” loaded.

As you can see from the debugging variables, both the existing players[0] and the player I’m adding have a player ID of “A:_d1bd1278a67fdab60ac1de3fc6089d18”, but have different names. I would take that to mean that the playerId (alone) uniquely identifies the player, and that trying to look the player up by name is misguided.

I’m not positive, but suspect that this will be a factor in tracking down this behavior.

I hate to keep dragging folks through troubleshooting my typos, but in this case, I’m pretty sure that this is a real-world scenario that the code in the book should handle correctly. (people are going to be able to rename their players (which apparently gives them a different name locally (??? - really foggy on this so far)))

Just a bit more information: Apparently GameKit, itself has the same opinion (that it’s the other player’s turn from both players)

I suspect that “forfeiting” from one of these might solve the deadlock, but then I’m not sure I could recreate the situation.

By changing getPlayer(withName playerName: String -> GameCenterPlayer? to getPlayer(withId playerId: String) -> GameCenterPlayer? (and corresponding logic change)

(and the == static func on GameCenterPlayer to only compare las.playerId == res.playerId)

I have things working now. It still seems a bit “wonky”, but maybe because I’ve not done much with GameCenter on “real” games. It seems odd that I have to go the the “Find Match” screen (and that there are not notifications of turns handing off), but I can see play switching between players, and proper tracking of who won/lost, the “score” being passed over to the other player, etc. So I guess I have it working now.

I do think that it’s an issue attempting to match on the player name though (after all of this, don’t really feel like going back and switching it back to try it out (maybe I’ll feel different after a night’s sleep))