mikecargal

mikecargal

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)

First Post!

mikecargal

mikecargal

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)))

Where Next?

Popular Pragmatic Bookshelf topics Top

johnp
Hi Brian, Looks like the api for tinydb has changed a little. Noticed while working on chapter 7 that the .purge() call to the db throws...
New
ianwillie
Hello Brian, I have some problems with running the code in your book. I like the style of the book very much and I have learnt a lot as...
New
yulkin
your book suggests to use Image.toByteData() to convert image to bytes, however I get the following error: "the getter ‘toByteData’ isn’t...
New
herminiotorres
Hi! I know not the intentions behind this narrative when called, on page XI: mount() |&gt; handle_event() |&gt; render() but the correc...
New
AleksandrKudashkin
On the page xv there is an instruction to run bin/setup from the main folder. I downloaded the source code today (12/03/21) and can’t see...
New
cro
I am working on the “Your Turn” for chapter one and building out the restart button talked about on page 27. It recommends looking into ...
New
rmurray10127
Title: Intuitive Python: docker run… denied error (page 2) Attempted to run the docker command in both CLI and Powershell PS C:\Users\r...
New
Chrichton
Dear Sophie. I tried to do the “Authorization” exercise and have two questions: When trying to plug in an email-service, I found the ...
New
jonmac
The allprojects block listed on page 245 produces the following error when syncing gradle: “org.gradle.api.GradleScriptException: A prob...
New
mcpierce
@mfazio23 I’ve applied the changes from Chapter 5 of the book and everything builds correctly and runs. But, when I try to start a game,...
New

Other popular topics Top

malloryerik
Any thoughts on Svelte? Svelte is a radical new approach to building user interfaces. Whereas traditional frameworks like React and Vue...
New
PragmaticBookshelf
Design and develop sophisticated 2D games that are as much fun to make as they are to play. From particle effects and pathfinding to soci...
New
DevotionGeo
I know that -t flag is used along with -i flag for getting an interactive shell. But I cannot digest what the man page for docker run com...
New
PragmaticBookshelf
Rust is an exciting new programming language combining the power of C with memory safety, fearless concurrency, and productivity boosters...
New
AstonJ
Thanks to @foxtrottwist’s and @Tomas’s posts in this thread: Poll: Which code editor do you use? I bought Onivim! :nerd_face: https://on...
New
Maartz
Hi folks, I don’t know if I saw this here but, here’s a new programming language, called Roc Reminds me a bit of Elm and thus Haskell. ...
New
mafinar
This is going to be a long an frequently posted thread. While talking to a friend of mine who has taken data structure and algorithm cou...
New
PragmaticBookshelf
Author Spotlight: Tammy Coron @Paradox927 Gaming, and writing games in particular, is about passion, vision, experience, and immersio...
New
AstonJ
If you’re getting errors like this: psql: error: connection to server on socket “/tmp/.s.PGSQL.5432” failed: No such file or directory ...
New
Fl4m3Ph03n1x
Background Lately I am in a quest to find a good quality TTS ai generation tool to run locally in order to create audio for some videos I...
New

Sub Categories: