Skip navigation

GKMatch is weird. You don’t ever instantiate GKMatch directly. Instead you use GKMatchMaker as a type of “factory” to instantiate GKMatch objects.

There are basically 2 ways to get a GKMatch up and running.

1) BOOTY CALLS: I call out people I want to have sex with have a match with BY NAME.

2) RANDOM ENCOUNTER: All players initiated THEIR OWN random partner search, and GameCenter “hooked them up” with a random partner.

First we’ll cover booty calls, then random encounters.

1. Booty call

BOOTY CALL works as follows. ONE OF THE PLAYERS (the “horny dude”) INVITES ALL THE OTHER PLAYERS BY NAME. He can invite as many players as he wishes (mm hmm). ALL PLAYERS WHO WANT TO ACCEPT INVITES BY NAME __MUST__ HAVE REGISTERED TO RECEIVE “BOOTY CALLS” BY SETTING [GKMatchmaker sharedMatchmaker].inviteHandler. We’ll call those players “the booty”.

// ALL PLAYERS OFFERING UP THEIR BOOTIES FOR TAPPING MUST
// SET THEIR INVITEHANDLER:
[GKMatchmaker sharedMatchmaker].inviteHandler = ^( GKInvite *acceptedInvite, NSArray *playersToInvite )
{
  // THIS RUNS FOR PLAYER WHO GOT A BYNAME INVITE ONLY
  printf( "You got an invitation to join a match started by `%s`\n", [acceptedInvite.inviter UTF8String] );
  
  // ACCEPTS THE MATCH:
  [ [GKMatchmaker sharedMatchmaker] matchForInvite:acceptedInvite completionHandler:^( GKMatch *theMatch, NSError *nsError ) {
    if( nsError ) {
      error( nsError, "matchForInvite:acceptedInvite" ) ; // prints the error (using my `error` function)
      return ;
    }

    // KEEP A COPY OF THE GKMATCH OBJECT.
    Game->match = theMatch ; /////////////// YOU MUST DO THIS.
    // IF YOU DON'T "RETAIN" THE MATCH OBJECT THEN IT GOES AWAY AND THE MATCH NEVER STARTS.
      
    // ATTACH YOUR GKMatchDelegate.  __YOU MUST DO THIS__
    // otherwise you have no chance in hell of making a GKMatch work.
    // I instantiated myGameMatchDelegate OUTSIDE this function.
    // instantiating it here gives an ARC error. 
    theMatch.delegate = myGameMatchDelegate ; // myGameMatchDelegate implements GKMatchDelegate protocol

    // Now after this, your GKMatchDelegate match:player:didChangeState: event will trigger
    // automatically.  You can continue to do network setup there.  From that point forward
    // the machines are "having sex" (network connected).
 } ] ;
} ;

The “horny dude” who initiates the booty call does so like this:

GKMatchRequest *request = [[GKMatchRequest alloc] init] ; 
request.minPlayers = 2 ; // INCLUDES YOU.  "The minimum number of players must be at least 2."
request.maxPlayers = 2 ;
request.defaultNumberOfPlayers = 2 ;
request.playerAttributes = 0 ; // NO SPECIAL ATTRIBS
request.playerGroup = 0 ;

// Only for when you have another user to play with BYNAME.
request.playersToInvite = [[NSArray alloc] initWithObjects:@"G:1928347891235", nil] ; //G:1928347891235 is
// the ID of the player I want to booty call.

// A block to be called when a response from an invited BY NAME player is returned to your game.
request.inviteeResponseHandler = ^( NSString *playerID, GKInviteeResponse response )
{
  printf( "\n*** `%s` RESPONDED TO YOUR INVITE WITH `%s`\n", 
  [playerID UTF8String], GKInviteeResponseName[response] ) ;
} ;

request.inviteMessage = @"COME PLAY WITH ME" ; // This appears in a game center notification notice.
  
// This is like a GKMatch constructor for the caller.
// It usually completes almost IMMEDIATELY.
[ [GKMatchmaker sharedMatchmaker] findMatchForRequest:request
   withCompletionHandler:^( GKMatch *newMatch, NSError *nsError )
  {
    // THIS RUNS FOR THE INVITER IMMEDIATELY AFTER SENDING ANY findMatchForRequest
    if( nsError ) {
      error( nsError, "[MM findMatchForRequest] failed" ) ;
      return ;
    }
      
    // This is the chance to finish setting up the GKMatch object.
    newMatch.delegate = myGameDelegate ; // YOU HAVE TO SET THE DELEGATE.
    Game->match = newMatch ; // YOU MUST DO THIS.
    // IF YOU DON'T RETAIN THE MATCH OBJECT THEN IT GOES AWAY AND THE MATCH NEVER STARTS.
  }
] ;

So after the booty call goes out, OTHER PLAYERS RECEIVE THE INVITATIONS AND ACCEPT IT (Note: requires VIA GC OVERLAY).

2. RANDOM ENCOUNTERS

BOTH PLAYERS ARE LOOKING FOR “RANDOM ENCOUNTERS”. They put on makeup/shave as appropriate. They then EACH INVOKE findMatchForRequest, BUT WITHOUT SPECIFYING OTHER PLAYER NAMES TO HOOK UP WITH. In other words, they’re looking to hook up with just anybody. GAMECENTER WILL “HOOK THEM UP” WITH RANDOM PARTNERS (TO HAVE SEX WITH).

The code that both players call then, to say to GameCenter they are DTF with just about anyone, is very similar to the code used “make a booty call” above. You just don’t fill out the playerIDs field (leave it nil).

GKMatchRequest *request = [[GKMatchRequest alloc] init] ; 
request.minPlayers = 2 ; // INCLUDES YOU.  "The minimum number of players must be at least 2."
request.maxPlayers = 2 ;
request.defaultNumberOfPlayers = 2 ;
request.playerAttributes = 0 ; // NO SPECIAL ATTRIBS
request.playerGroup = 0 ;

request.playersToInvite = nil ; // NIL THAT
  
// This is like a GKMatch constructor for the caller.
// It completes IMMEDIATELY.
[ [GKMatchmaker sharedMatchmaker] findMatchForRequest:request
   withCompletionHandler:^( GKMatch *newMatch, NSError *nsError )
  {
    // THIS RUNS FOR THE INVITER IMMEDIATELY AFTER SENDING ANY findMatchForRequest
    if( nsError ) {
      error( nsError, "[MM findMatchForRequest] failed" ) ;
      return ;
    }
      
    // This is the chance to finish setting up the GKMatch object.
    newMatch.delegate = myGameDelegate ; // YOU HAVE TO SET THE DELEGATE.
    Game->match = newMatch ; // YOU MUST DO THIS.
    // IF YOU DON'T RETAIN THE MATCH OBJECT THEN IT GOES AWAY AND THE MATCH NEVER STARTS.
  }
] ;
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: