0

The Game Center turn-based matchmaking interface allows a user to create a match with a number of players and fill some of those places with invites to friends and leave the others for auto-matching.

I am creating our own in-game match creation interface which works fine, so far, except when it comes to creating a match with both invited and auto-match players.

GKMatchmaker has the addPlayersToMatch method where I believe you can add auto-match players once the match exists, but GKTurnBasedMatch has no equivalent method.

The following is the code I am using, which works just fine. If anyone knows how to add a number of auto-match players it would be much appreciated!

- (GKMatchRequest *) buildMatchRequestWithFriends: (NSArray *) friendsList NumberOfPlayers: (NSInteger) numberOfPlayers
{
    NSLog(@"TurnByTurnHelper.buildMatchRequestWithFriends");
    GKMatchRequest *request = [[GKMatchRequest alloc] init];
    NSArray *playersToInvite = [NSArray arrayWithArray:friendsList];
    request.playersToInvite = playersToInvite;
    request.defaultNumberOfPlayers = numberOfPlayers + 1;
    return request;
}

- (void) requestMatchWithFriends:(NSArray *) friendsList NumberOfPlayers: (NSInteger) numberOfPlayers{

    if (!_delegate)
    {
        NSLog(@"Error: Expected but did not find delegate");
        return;
    }

    GKMatchRequest *request = [self buildMatchRequestWithFriends:friendsList NumberOfPlayers: numberOfPlayers];

    [GKTurnBasedMatch findMatchForRequest: request withCompletionHandler:^(GKTurnBasedMatch *match, NSError *error)
     {
         if (match){
             NSLog(@"findMatchForRequest: Success!");

             // Add match to matches

         } else {
             NSLog(@"error: %@", error);

         }
     }];
}
Chris Nelson
  • 466
  • 5
  • 4

1 Answers1

0

Well, with a bit of searching around and testing I have discovered that the Apple Developer documentation is incomplete and/or misleading. The following statement is made in the playersToInvite property section of GKMatchRequest:

"If the value of the property is non-nil, when you use the request to create a match, Game Center invites those players to the match. No automatching is done and the GKMatchRequest maxPlayers and minPlayers properties are ignored."

This is true for GKMatchmaker but NOT for GKTurnBasedMatch! For GKTurnBasedMatch, if you provide an array of playerIds in the playersToInvite property, the maxPlayers and minPlayers properties are NOT ignored and Game Center fills up the extra spaces with Random players.

The following code does the trick:

- (GKMatchRequest *) buildMatchRequestWithFriends: (NSArray *) friendsList NumberOfPlayers: (NSInteger) numberOfPlayers
{
    GKMatchRequest *request = [[GKMatchRequest alloc] init];
    NSArray *playersToInvite = [NSArray arrayWithArray:friendsList];

    if([friendsList count]<=0){
        request.minPlayers = 2;
        request.maxPlayers = numberOfPlayers;
        request.playersToInvite = nil;
    } else {
        request.minPlayers = 2;
        request.maxPlayers = numberOfPlayers;
        request.playersToInvite = playersToInvite;
    }

    return request;
}

And the moral of that story is: sometimes it's better not to RTFM!

Chris Nelson
  • 466
  • 5
  • 4
  • I'm playing around with gamecenter now and this comment just saved me hours of work I think. Gamecenter is so weird. – Negora May 14 '20 at 05:56