Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

trouble negotiating a 2 player match in game kit

I am getting some behavior I can't decode from GameKit.

sometimes the player who has done the inviting gets stuck in a 'waiting...' loop, and is unable to close the deal on his invitation.

I believe it has to do with multitasking and the invite handler... it seems that if the invitee's app starts from scratch, then the invitation can be properly accepted. But the mechanism of this not so transparent to me.

Any clues as to what might be missing? I've become blinded by the documentation.

like image 529
Tim Summers Avatar asked Mar 31 '11 22:03

Tim Summers


1 Answers

Sometimes when a match between two players starts, it's possible that one player doesn't have the connected state yet. You should check if more players are expected to connect before actually starting the game. If it's over 0, instead of starting the game, wait for the player to connect, and only start the game when that player is connected.

So the code would look something like this inside your the method where you are setting up the game:

if (currentMatch.expectedPlayerCount) {
    waiting = YES;
}

And you would implement this delegate method:

- (void)match:(GKMatch *)match player:(NSString *)playerID didChangeState:(GKPlayerConnectionState)state
{
    if (state == GKPlayerStateConnected) {
        if (waiting) {
            waiting = NO;
            // start the game now
        }
    } else if (state == GKPlayerStateDisconnected) {
        // handle disconnect
    }
}
like image 83
Filip Radelic Avatar answered Oct 30 '22 17:10

Filip Radelic