NullPlague
NullPlague

Reputation: 7

Can't add additional players with [GKMatchmaker addPlayersToMatch] on iPhone/IOS 5

I have a Game Center app. I successfully connect two clients and can send messages etc. I am now trying to add a 3rd/4th client with [GKMatchmaker addPlayersToMatch] like so...

- (void) findAdditionalPlayer
{
    GKMatchRequest *request = [[[GKMatchRequest alloc] init] autorelease];
    request.minPlayers = 2;  // minPlayers = 3 doesn't work either
    request.maxPlayers = 4;

    [[GKMatchmaker sharedMatchmaker] addPlayersToMatch:match matchRequest:request completionHandler:^(NSError *error)
    {
        if (error)
        {
            // Process the error.
            NSLog(@"Could not find additional player - %@", [error localizedDescription]);
        }
        else
        {
            NSLog(@"Find additional player expecting = %d", match.expectedPlayerCount);
        }
    }];
}

If one client (the voted server) calls findAdditionalPlayer I never connect (the other client is using GKMatchmakerViewController). Oddly, if both the connected clients call findAddtionalPlayer, then my completion block executes (the match.expectedPlayerCount == 2), but my 3rd client never connects.

Should just one game client call this function above? The documentation doesn't really specify.

Does anyone have an example using addPlayersToMatch that works?

Upvotes: 1

Views: 944

Answers (1)

rizjoj
rizjoj

Reputation: 197

In my experience, for a 2 player game, addPlayersToMatch should be executed by BOTH players for them to reconnect to the game (and communicate back and forth via Game Center).

It makes sense that your two clients can connect if they both call findAdditionalPlayer since both are calling addPlayersToMatch.

If you already have a game with 2 players (say A and B) and you want a third player (say C) to join, you'll have to:

In Player A (invite C):

GKMatchRequest *request = [[GKMatchRequest alloc] init];
request.minPlayers = 3;
request.maxPlayers = 4;
request.playersToInvite = [NSArray arrayWithObject:playerC_id];
[[GKMatchmaker sharedMatchmaker] addPlayersToMatch:myMatch matchRequest:request completionHandler:nil];

In Player B (invite C):

GKMatchRequest *request = [[GKMatchRequest alloc] init];
request.minPlayers = 3;
request.maxPlayers = 4;
request.playersToInvite = [NSArray arrayWithObject:playerC_id];
[[GKMatchmaker sharedMatchmaker] addPlayersToMatch:myMatch matchRequest:request completionHandler:nil];

In Player C (invite A and B):

GKMatchRequest *request = [[GKMatchRequest alloc] init];
request.minPlayers = 3;
request.maxPlayers = 4;
request.playersToInvite = [NSArray arrayWithObjects:playerA_id, playerB_id, nil];
[[GKMatchmaker sharedMatchmaker] addPlayersToMatch:myMatch matchRequest:request completionHandler:nil];

So the mechanism to rejoin or add new players to a match seems to be:

  1. When a player detects a remote player has disconnected (or an entirely new player is added), create a new match request object, include only the disconnected/new player's id in this new match request's playersToInvite array and execute addPlayersToMatch with it.
  2. When the disconnected player resumes, create a new match request object, include all the remote players' ids (you may have to store them in an array in advance or get them from your GKMatch's playerIDs property) in its match request's playersToInvite array and execute addPlayersToMatch with it.

In other words, each existing player much add the new player to their match objects AND the new player much add all existing players to its match object.

For a 4 player game (players A, B, C and D where player D is the newest addition): Players A, B and C each execute their addPlayersToMatch call with a match request object whose playerToInvite consists of only D's playerID. While player D executes its addPlayersToMatch call with a match request object whose playerToInvite array contains A, B as well as C's playerIDs.

Upvotes: 1

Related Questions