SanMu
SanMu

Reputation: 765

Retrieve GKTurnBasedMatch objects from GKTurnBasedMatchMakerViewController - Swift 4

I am trying to implement a turn-based multiplayer game (2-player strategy board game). I have managed to authenticate the local player and present the GKTurnBasedMatchmakerViewController (the standard interface).

I am a bit puzzled by how to retrieve the GKTurnBasedMatch object that the player selects (if available). This method of the GKTurnBasedMatchmakerViewControllerDelegate protocol seemed quite sensible for this purpose, but apparently it has been deprecated.

func turnBasedMatchmakerViewController(_ viewController: GKTurnBasedMatchmakerViewController, didFind match: GKTurnBasedMatch)

Any hints would be much appreciated!

Upvotes: 1

Views: 99

Answers (1)

Tim
Tim

Reputation: 1

I agree, the delegate function for achieving this was useful.

My solution for this is to register a listener in the delegate that presents the matchmaker view controller, let's call this mainVC. MainVC should conform to GKLocalPlayerListener protocol.

// In mainVC
GKLocalPlayer.localPlayer().registerListener(self)

Then when the player selects a match in the standard interface, the following function is called, which you must implement in mainVC:

// in mainVC
func player(player: GKPlayer, receivedTurnEventForMatch match: GKTurnBasedMatch, didBecomeActive: Bool) {
  // Dismiss the turnBasedMatchmakerViewController standard interface
  // Load your match using the match object passed above
}

In this function, dismiss the standard interface that matched the player and then load your game using a segue or otherwise.

The receivedTurnEventForMatch function is the standard way to update your match with events that have happened. It is called when:

  • The current turn has a time-out associated with it and the turn is about to expire.
  • Player accepts an invite from another player.
  • Turn was passed to another player. In this case, didBecomeActive is false.
  • Match data is saved by another player.
  • Player receives a reminder.

Upvotes: 0

Related Questions