Le Mot Juiced
Le Mot Juiced

Reputation: 3849

How to actually start a GKTurnBasedMatch so that I can send an exchange?

I'm trying to send an exchange to another player in a GKTurnBasedMatch. When I send it, my completion handler gets a big error message, with these descriptions:

The requested operation could not be completed because the session is in an invalid state.
Game is not active, session state is Matching

I want the match to start when there are just two players, but to allow a total of 16 players. So naturally I'm setting maxPlayers = 16 and minPlayers = 2. I'd thought that would automatically start the match once two players were seated, but it's not so.

I've tried to do this once the match has two players, :

    if match.participants?.count == 2 {
      match.status = GKTurnBasedMatchStatus.open
    }

But then I'm told that status is read-only. I can't manually set it.

Now, with a regular GKMatch, I officially start the match by calling:

GKMatchmaker.shared().finishMatchmaking(for: match)

But there doesn't seem to be a similar thingy for GKTurnBasedMatch.

How do I actually get the match started, so I can send an exchange between the two players?

Upvotes: 1

Views: 73

Answers (1)

Le Mot Juiced
Le Mot Juiced

Reputation: 3849

Man, the documentation on GKTurnBasedMatch is sparse.

Here's the thing: you don't actually start a GKTurnBasedMatch explicitly. You only end it explicilty.

During gameplay, you just pass the turn from one active player to another active player. The game technically starts as soon as the first player is seated.

The problem I was having is that I had passed the turn when there was no other active player. So I accidentally told Game Center to assign the turn to an empty seat. So when the error message told me

Game is not active, session state is Matching

It meant that the current seat was empty and still looking for a player to fill it. The game apparently goes out of active status any time the current player's status is Matching (which denotes an empty seat).

Why on earth this would affect sending exchanges, whose whole purpose is to enable communication between any players, no matter who the current player is, is beyond me. But there you go.

The solution: make sure that the current turn is held by an actual person, and is not an empty seat. Then you can send exchanges. Otherwise you can't.

Upvotes: 1

Related Questions