How to set a host player for a peer to peer match in GameKit?

191 Views Asked by At

I have a GKMatch game with two players where I send data from one player to another with the match.send().

As I understand in a peer to peer match, without a server, one of the players would act as a host, from whose device some of the game logic would be determined.

What I cannot figure out is how to set a player to be host for a game, as I cannot see any helping functions from GameKit other than chooseBestHostingPlayer(), but that return is optional so what would be a guaranteed way to determine host?

Also, after host is determined would I still use the same sendMethod such as match.send(), to update game state, then just check to make sure only send it if the player is host?

Any clarity on this topic would be highly appriciated, thank you.

1

There are 1 best solutions below

0
hotdogsoup.nl On

chooseBestHostingPlayer() is indeed just a helper to find the player with the best network connection. You don't have to use this function.

You can use your own way of deciding who is the "host" or "server". For instance: by throwing dice

  1. Before the game starts, each client generates a random number and sends it to the other players.
  2. The one with the highest number becomes the host.
  3. Every client will know if they are the host or not because they all have the same list of random numbers.
  4. After this "negotiation", the host client can then send a command to start the game.

An example of this can be found here: https://www.kodeco.com/2485-game-center-tutorial-how-to-make-a-simple-multiplayer-game-with-sprite-kit-part-2-2 (it's old Objc code but the mechanism is explained).

  1. In case the host gets disconnected, each client will receive a disconnected message, and they can check the list of numbers again to determine who is now the new host.