Invite players programmatically into a match

I am using Swift 4.


I am able to authenticate the localplayer and find a match using the Game Center match viewcontroller. I was also able to link the iOS simulator (player1) to my real iPhone 8 device (player2) into a match for test purposes.


However, after the localplayer is authenticated, I would like to obtain a list of all my game center "friends" myself and then create my own viewController interface to invite/connect players into the game. I was able to call "GKLocalPlayer.local.loadChallengableFriends( )" to obtain the friends, but I am not sure how to properly invite them to a match one by one programmatically.


Googling indicates GKMatchmaker inviteHandler, GKInvite and "player DidAccept" are involved but I do not know how to put this all together.


Any pointers, hints or sample code would be greatly appreciated.


In the meantime, I will continue to Google around

Accepted Reply

ok, thanks for your input. I really want to create my own viewcontroller so my interface looks like it is part of my game motif instead of some default Game CEnter interface. However, we will see how it goes 🙂 . There is not a lot of examples using swift 4 so I am trying to read everything and put the pieces of the puzzle together bit by bit.

Replies

There are many tutorials that will walk you through this. Search for them.


It's been quite some time (pre ARC) but this code worked long ago:



- (void)player:(GKPlayer *)player didAcceptInvite:(GKInvite *)invite{
    GKMatchmakerViewController *mmvc = [[[GKMatchmakerViewController alloc] initWithInvite:invite] autorelease];
    mmvc.matchmakerDelegate = self;
    [self presentViewController:mmvc animated:YES completion:nil];
}


-(void) sendOutAnInvitationTo:(NSString *)action{
  GKMatchRequest *request = [[[GKMatchRequest alloc] init] autorelease];
  request.minPlayers = 2;
  request.maxPlayers = 4;
  GKMatchmakerViewController *mmvc = [[[GKMatchmakerViewController alloc] initWithMatchRequest:request] autorelease];
  if([action isEqualToString:@"add a player"])[mmvc addPlayersToMatch:myMatch];
  mmvc.matchmakerDelegate = self;
        [self presentViewController:mmvc animated:YES completion:nil];
}


- (void)matchmakerViewControllerWasCancelled:(GKMatchmakerViewController *)viewController{
        [self dismissViewControllerAnimated:YES completion:nil];
}

- (void)matchmakerViewController:(GKMatchmakerViewController *)viewController didFailWithError:(NSError *)error{
        [self dismissViewControllerAnimated:YES completion:nil];
}

- (void)matchmakerViewController:(GKMatchmakerViewController *)viewController didFindMatch:(GKMatch *)match{
    [self dismissViewControllerAnimated:YES completion:nil];
    match.delegate = self;
  if (match.expectedPlayerCount == 0){
  [self initiateAMatch:match];
  [self startMultiplayerPlay:match];
  }
}


-(void) initiateAMatch:(GKMatch *)match{
  useGameCenter=YES;
  bluetoothStatus=1;
  AVAudioSession *audioSession = [AVAudioSession sharedInstance];
  NSError *error= nil;
  [audioSession setCategory:AVAudioSessionCategoryPlayAndRecord withOptions:AVAudioSessionCategoryOptionDefaultToSpeaker error:&error];
  [audioSession setActive: YES error: NULL];
  self.allChannel=[match voiceChatWithName:@"allPlayers"];

  [allChannel start];
  allChannel.volume = 1.0;
  allChannel.active=YES;
  voiceChatIsActive=YES;
  self.myMatch = match; // Use a retaining property to retain the match.
}



- (void)match:(GKMatch *)match player:(NSString *)playerID didChangeState:(GKPlayerConnectionState)state{
  switch (state){            
        case GKPlayerStateUnknown:
            // handle a new player connection.
            // NSLog(@"here with number =  %i",match.expectedPlayerCount);
            break;
        case GKPlayerStateConnected:
            // handle a new player connection.
            // NSLog(@"here with number =  %i",match.expectedPlayerCount);
            if (match.expectedPlayerCount == 0){
               [self initiateAMatch:match];  //problem if switched from an earlier game??
               [self startMultiplayerPlay:match]; 
            }
            break;
        case GKPlayerStateDisconnected:
            if([match.players count]==0){  // 2016 was playerids
                   UIAlertView *playerList;
                   playerList = [[UIAlertView alloc]
                        initWithTitle:nil
                        message:@"Game has eneded.  You are no longer connected to any other player."
                        delegate: self        
                        cancelButtonTitle:@"Ok"
                        otherButtonTitles:nil];
                 [playerList show];
                 [playerList release];
            }
            [self startMultiplayerPlay:match];//I think this will work....
            break;
   }
}

-(void) startMultiplayerPlay:(GKMatch *)match{
////     good to go......
}

PBK,


Thanks for the code, but I have all of this code already working.


I would like to find a way to Invite players to participate in a match without presenting the Game Center matchmaker view controller and having to click an "Invite Players" button. I would like to design my own view controller interface to add players to a game instead of using the default Game Center matchmaker view controller.


I am assuming I would still need to create the match using the GKMatchRequest (as below) but I am hoping I do not have to display the matchmaker view controller which is provided.


        let request = GKMatchRequest.init()
        request.minPlayers = minPlayers
        request.maxPlayers = maxPlayers


I have googled and googled but have not found anything


Do you know how to do this?

I know how to do what I did in the app Go Connect. I don't know if that is what you want or not.

I am not familiar with Go Connect.


I just want to find a way to invite players into a match without displaying the matchmaker view controller provided by Game Center

I think a missed reading the "Real-time matches" section in teh game cnter porgramming guide :>P

It seems like it is talking about this topic. I will give it a read and see what happens, No idea how I missed that section

Download Go Connect and see if that is what you want.


I once duplicated the invite functionality through Cloudkit to avoid using GameCenter but decided it wasn't worth it.


What is wrong with the Game Center View Controllers?

>>What is wrong with the Game Center View Controllers?

I want to be able to invite my friends to the match and if they cannot join then be able to invite random players into the match at that point to fill the available slots. I do not think Game Center view controller provides this type of thing since I call the findMatchWithMinPlayers method which then brings up the Game Center viewcontroller with all random players.


I discovered I can provide a "matchRequest.recipients" array indicating specific players to invite (like my friends), but I am not sure whether the Game Center UI view controller will default to inviting a random player automatically if one of my friends declines the invitation. Do you know how Game Center UI view controller handles a decline of a player listed in the "recipient" array?

>Do you know how Game Center UI view controller handles a decline of a player listed in the "recipient" array?


I do not recall.


After 'rolling my own in CloudKit' in order to avoid using 'Game Center' I decided that was a mistake and reverted to the Game Center approach. I'd recommend your doing the same. They thought alot about the 'invite' concept and came up with a solution that works for most scenarios. I suspect you will find that any particular 'edge case' that you are most focused on will be, in actual practice, a rarity.


IMHO.

ok, thanks for your input. I really want to create my own viewcontroller so my interface looks like it is part of my game motif instead of some default Game CEnter interface. However, we will see how it goes 🙂 . There is not a lot of examples using swift 4 so I am trying to read everything and put the pieces of the puzzle together bit by bit.

Again - check out Go Connect and see which view controllers you don't like and why they might be necessary.

And somewhere up there you asked about teh collision - the collision is very likely when the game crashes for any one of a bunch of reasons - game over, network problem. In that case both players will try a reconnect making a collision possible.

I downloaded th GoConnect "Lifestyle" app from the AppStore and it asked me for me email address and a subDomain. I have no idea what a subDomain is so I did not continue.


I did not see any "GoConnect" app dealing with games and such

Go Connect is an app on the Apple App Store. There is a space between Go and Connect.

Searching for "Go Connect" in the App Store returns the following results for me:


  • Connect by goPanache (Booking Beauty Professional)
  • GOconnect (Lifestyle)
  • GoToConnect (Business)
  • Proximiti GoCONNECT (Business)
  • Word Search - Crossword
  • GO!CONNECT! (Casual)
  • 4FrontGo
  • Go-SIM
  • etc..


There is nothin which I can see caleld Go Connect dealing with games

Sorry. Go Game Connect. https://apps.apple.com/au/app/go-game-connect/id1385361527