GKTurnBasedMatch

The GKTurnBasedMatch class allows your game to implement turn-based matches between sets of players on Game Center. A turn-based match uses a store-and-forward approach to share data between the participants. When a player participating in the match performs actions that advance the state of the match, your game describes the new state of the match and decides which player acts next. The next player to act is notified by a push notification. Later, when the next player launches your game, you download the match data from Game Center and continue the match. Players take turns acting (based on whatever internal logic your game implements) until the match ends. One advantage of turn-based matches is that a player may participate in multiple matches simultaneously.

Your game never directly creates GKTurnBasedMatch objects, because a match always represents an existing match that is stored by Game Center. Instead, match objects are created for your game by Game Kit. A match object may hold a newly created match (for a match that has not started yet) or a match that is already under way. Here are ways your game acquires match objects:

The matchData property holds binary data that describes the current state of the match. The format of the data contained in this object is something you design when you create your game; Game Kit only manages storing and forwarding of this data.

When a player views a particular match, your game should take the match object and call its loadMatchDataWithCompletionHandler: method to retrieve the match data. After the match data is downloaded from Game Center, your game parses the data and displays the game’s user interface. A player may want to view the match even when it is another player’s turn to act; your game should expect this and allow the player to view the state of the match without allowing them to act. When the current player views the match, your game should both show them the state of the match and provide a user interface to allow the player to take a turn. The current player is expected to perform actions that change the state of the match. These actions are based on the internal logic of your game’s rules. The player continues to act until he or she takes an action that requires another person in the match to act in order to advance the match. For example, in a game like Chess, moving a piece on the board and passing control to the other participant happen simultaneously. A more complex game might allow the current player to take multiple actions before the match reaches a state in which control is ceded to another player.

Once the player takes an action that requires another player to act, your game should determine which player needs to act next. Then, it should encode the state of the match into an NSData object and call the match object’s endTurnWithNextParticipants:turnTimeout:matchData:completionHandler: method to set the next player to act. The next player is automatically informed that the match is waiting on his or her actions in order to continue the match.

As the match progresses, players may leave the match. For example, this situation might occur if your game determines that this player has been eliminated from the match. Or your game might offer a user interface to allow players to resign from matches. If the local player resigns from the match and is also the match’s current player, your game must call the match object’s participantQuitInTurnWithOutcome:nextParticipant:matchData:completionHandler: method. As it does when the player takes a turn, your game passes in the state of the match and the next player to act. Your game also provides a match outcome — essentially, a numerical value that indicates the reason why that player left the match. If your game allows players to resign from matches when it is not that player’s turn, your game calls the match object’s participantQuitOutOfTurnWithOutcome:withCompletionHandler: method to allow the player to leave the match.

Eventually, a match ends. When your game logic dictates that a match should end, your game calls the match object’s endMatchInTurnWithMatchData:completionHandler: method. Your game must ensure that all participants in the match have a valid match outcome before ending the match.

Even after a match ends, its data is still stored on Game Center; this allows players to view previous victories and defeats. A player may choose to delete the match using the standard user interface, but your game can also programmatically remove a match by calling the match object’s removeWithCompletionHandler: method. In all cases, the decision to delete a match should be made by a player; the removeWithCompletionHandler: method exists to allow your game to present a custom user interface for viewing and working with matches.

Parameters

completionHandler

A block to be called after the matches are retrieved from the server.

The block receives the following parameters:

matches

An array of GKTurnBased Match objects containing the match objects for matches that the local player is playing in, or nil if there are no matches to load. If an error occurred, this value may be non-nil. In this case, the array holds whatever match data could be retrieved from Game Center before the error occurred.

error

If an error occurred, this error object describes the error. If the operation was completed successfully, the value is nil.

Discussion

When this method is called, it creates a new background task to handle the request. The method then returns control to your game. Later, when the task is complete, Game Kit calls your completion handler. The completion handler is always called on the main thread.

Parameters

If the operation completed successfully, this parameter holds the match. If an error occurred, the value is nil.

error

If an error occurred, this error object describes the error. If the operation was completed successfully, the value is nil.

Discussion

When this method is called, it creates a new background task to handle the request. The method then returns control to your game. Later, when the task is complete, Game Kit calls your completion handler. The completion handler is always called on the main thread.

Parameters

A match request that specifies the properties that the new match must fulfill.

completionHandler

A block to be called after the match is successfully created.

The block receives the following parameters:

match

A newly initialized match object that contains a list of players for the match. If an error occurred, this value is nil.

error

If an error occurred, this error object describes the error. If the operation was completed successfully, the value is nil.

Discussion

When this method is called, it creates a background task to handle the request. The method then returns control to your game. Later, when the task is complete, Game Kit calls your completion handler. The completion handler is always called on the main thread.

This method may either create a new match or it may place the player into an existing match that needs a new player to advance the match further. Regardless of how the player is placed in the match, the local player is always the current participant in the returned match. Your game should immediately display the match in its user interface and allow the player to take a turn.

To be consistent with older servers and versions of iOS, the minimum number of players specified by the GKMatchRequest object is set to be equal to the maximum number of players when looking for a match.

Parameters

A newly initialized match object that contains a list of players for the match. If an error occurred, this value is nil.

error

If an error occurred, this error object describes the error. If the operation was completed successfully, the value is nil.

Discussion

When this method is called, it creates a new background task to handle the request. The method then returns control to your game. Later, when the task is complete, Game Kit calls your completion handler. The completion handler is always called on the main thread.

Parameters

If an error occurred, this error object describes the error. If the operation was completed successfully, the value is nil.

Discussion

When this method is called, it creates a new background task to handle the request. The method then returns control to your game. Later, when the task is complete, Game Kit calls your completion handler. The completion handler is always called on the main thread.

Parameters

A newly initialized match object that contains a list of players for the match. If an error occurred, this value is nil.

error

If an error occurred, this error object describes the error. If the operation was completed successfully, the value is nil.

Discussion

When this method is called, it creates a new background task to handle the request. The method then returns control to your game. Later, when the task is complete, Game Kit calls your completion handler. The completion handler is always called on the main thread.

Declaration

Discussion

This string is not intended to be displayed to players. Your game should use this string whenever it needs to identify a specific match. For example, if you want your game to store additional information on a device or in iCloud, it might store it in a database using the match ID as a key.

Availability

Declaration

Discussion

The message property is displayed by the standard user interface; this allows your game to use the message to inform players of the current state of the match.

Important

This property can be changed only by an instance of your game associated with the current player. If an instance of your game associated with another player in the match attempts to write to this property, an exception is thrown.

Declaration

Discussion

The elements of this array are GKTurnBasedParticipant objects representing each participant in the match. Your game uses these objects to retrieve more information about the participants in the match. Your game also uses one of the objects in this array as a parameter whenever it calls a method that sets a different participant to act in the match.

The size of the array and the order in which the participants appear in the array are set when the match is first created, and never changes. When a match is first created, some participants may not hold actual players yet. Game Center searches for a player to fill that spot in the match only after your game sets that participant as the current player.

Declaration

Discussion

Although Game Center knows who is participating in the match and who is expected to act next, it does not know anything about your game’s internal logic. Your game provides the match data and all the programming logic required to interpret it. This data should include the current state of the game and provide any necessary details about what actions the current player is expected to take. It can also be helpful for your game to record information about recent moves made by other players. The game can then replay those moves visually for the player to show exactly how the match reached the state it is in now.

Your game never directly updates the match state associated with this property. Instead, when the data is updated to reflect the actions of the current player, your game serializes the updated state into memory and calls one of the match’s instance methods that transmit the updated state to Game Center.

The value of this property is nil until after your game calls the loadMatchDataWithCompletionHandler: method and the load task is complete. After this task completes, the matchData property holds the data that the last player to act transmitted to Game Center.

Parameters

A block to be called after the match data is retrieved from the server.

The block receives the following parameters:

matchData

The data stored on Game Center that reflects the current state of the match. If an error occurred, this value is nil.

error

If an error occurred, this error object describes the error. If the operation was completed successfully, the value is nil.

Discussion

When this method is called, it creates a new background task to handle the request. The method then returns control to your game. Later, when the task is complete, Game Kit calls your completion handler. The completion handler is always called on the main thread.

Parameters

A serialized blob of data reflecting the game-specific state for the match. Do not pass nil as an argument.

completionHandler

A block to be called after the data is uploaded to Game Center.

The block receives the following parameters:

error

If an error occurred, this error object describes the error. If the operation was completed successfully, the value is nil.

Discussion

This method updates the match data stored on Game Center. Call this method when the current player takes an action that advances the state of the match but does not end the player’s turn. For example, if your game has a fog-of-war mechanic, you might call this method when the player revealed new information on the map.

When this method is called, it creates a new background task to handle the request. The method then returns control to your game. Later, when the task is complete, Game Kit calls your completion handler. The completion handler is always called on the main thread.

Parameters

An array of GKTurnBasedParticipant objects reflecting the order in which the players should act next. Each object in the array must be one of the objects stored in the match’s participants property.

timeout

The length of time the next player has to complete their turn.

matchData

A serialized blob of data reflecting the game-specific state for the match. Do not pass nil as an argument.

completionHandler

A block to be called after the data is uploaded to Game Center.

The block receives the following parameters:

error

If an error occurred, this error object describes the error. If the operation was completed successfully, the value is nil.

Discussion

If the next player to act does not take their turn in the specified interval, the next player in the array receives a notification to act. This process continues until a player takes a turn or the last player in the list is notified.

When this method is called, it creates a new background task to handle the request. The method then returns control to your game. Later, when the task is complete, Game Kit calls your completion handler. The completion handler is always called on the main thread.

Parameters

The next player in the match who needs to take an action. It must be one of the object’s stored in the match’s participants property.

matchData

A serialized blob of data reflecting the game-specific state for the match. Do not pass nil as an argument.

completionHandler

A block to be called after the data is uploaded to Game Center.

The block receives the following parameters:

error

If an error occurred, this error object describes the error. If the operation was completed successfully, the value is nil.

Discussion

When this method is called, it creates a new background task to handle the request. The method then returns control to your game. Later, when the task is complete, Game Kit calls your completion handler. The completion handler is always called on the main thread.

Parameters

matchOutcome

The end outcome of the current player in the match. Do not pass nil as an argument.

nextParticipants

An array of GKTurnBasedParticipant objects that contains participant objects reflecting the order in which the players should act next. Each object in the array must be one of the objects stored in the match’s participants property.

timeout

The length of time the next player has to complete their turn.

matchData

A serialized blob of data reflecting the game-specific state for the match.

completionHandler

A block to be called after the data is uploaded to the server.

The block receives the following parameters:

error

If an error occurred, this error object describes the error. If the operation was completed successfully, the value is nil.

Discussion

Your game calls this method on an instance of your game that is processing the current player’s turn, but that player has left the match. For example, the player may have willingly resigned from the match or that player may have been eliminated by the other players (based on your game’s internal logic).

If the next player to act does not take their turn in the specified interval, the next player in the array receives a notification to act. This process continues until a player takes a turn or the last player in the list is notified.

When this method is called, it creates a new background task to handle the request. The method then returns control to your game. Later, when the task is complete, Game Kit calls your completion handler. The completion handler is always called on the main thread.

Parameters

The end outcome of the current player in the match. Do not pass nil as an argument.

nextParticipant

The next player in the match who needs to take an action. It must be one of the object’s stored in the match’s participants property.

matchData

A serialized blob of data reflecting the game-specific state for the match.

completionHandler

A block to be called after the data is uploaded to the server.

The block receives the following parameters:

error

If an error occurred, this error object describes the error. If the operation was completed successfully, the value is nil.

Discussion

Your game calls this method on an instance of your game that is processing the current player’s turn, but that player has left the match. For example, the player may have willingly resigned from the match or that player may have been eliminated by the other players (based on your game’s internal logic).

When this method is called, it creates a new background task to handle the request. The method then returns control to your game. Later, when the task is complete, Game Kit calls your completion handler. The completion handler is always called on the main thread.

Parameters

The end outcome of the current player in the match. Do not pass nil as an argument.

completionHandler

A block to be called after the player’s status is updated on Game Center.

The block receives the following parameters:

error

If an error occurred, this error object describes the error. If the operation was completed successfully, the value is nil.

Discussion

If the local player decided they wanted to resign from the match but is not the current participant in the match, your game calls this method.

When this method is called, it creates a new background task to handle the request. The method then returns control to your game. Later, when the task is complete, Game Kit calls your completion handler. The completion handler is always called on the main thread.

Parameters

A serialized blob of data reflecting the current state for the match. Do not pass nil as an argument.

scores

An array of GKScore objects containing the final scores for every participant in the match.

achievements

An array of GKAchievement objects containing the achievements acquired by each participant in the match.

completionHandler

A block to be called after the scores are saved to the server.

The block receives the following parameters:

error

If an error occurred, this error object describes the error. If the operation was completed successfully, the value is nil.

Discussion

When this method is called, it creates a new background task to handle the request. The method then returns control to your game. Later, when the task is complete, Game Kit calls your completion handler. The completion handler is always called on the main thread.

This method ends the current match and submits scores and achievements for all of the participants. The scores can submitted to multiple leaderboards. The GKTurnBasedMatchOutcome status must be set for each participant before calling this method.

Parameters

A serialized blob of data reflecting the end state for the match. Do not pass nil as an argument.

completionHandler

A block to be called after the match is successfully ended.

The block receives the following parameters:

error

If an error occurred, this error object describes the error. If the operation was completed successfully, the value is nil.

Discussion

Calling this method ends the match for all players. This method may only be called by the current participant. Before your game calls this method, the matchOutcome property on each participant object stored in the participants property must have been set to a value other than GKTurnBasedMatchOutcomeNone.

When this method is called, it creates a new background task to handle the request. The method then returns control to your game. Later, when the task is complete, Game Kit calls your completion handler. The completion handler is always called on the main thread.

Declaration

Parameters

A block to be called after the match has been removed from Game Center.

The block receives the following parameters:

error

If an error occurred, this error object describes the error. If the operation was completed successfully, the value is nil.

Discussion

Even after a player’s participation in a match ends, the data associated with the match continues to be stored on Game Center. Storing the data on Game Center allows the player to continue to watch the match’s progress, or even see the final state of the match when it ends. However, players may also want to delete matches that they have finished playing. If you choose not to use the standard matchmaker user interface, your game should offer the ability to delete a finished match from Game Center. When a player chooses to delete a match from Game Center, call this method. It is a programming error to call this method on a match that has the local player as an active participant.

When this method is called, it creates a new background task to handle the request. The method then returns control to your game. Later, when the task is complete, Game Kit calls your completion handler. The completion handler is always called on the main thread.

When the task completes, the match is no longer visible to the local player whose device made the call. Other players involved in the match still see the match.

Parameters

An array of GKTurnBasedExchange objects that contains the resolved exchanges.

completionHandler

A block to be called after the scores are saved to the server.

The block receives the following parameters:

error

If an error occurred, this error object describes the error. If the operation was completed successfully, the value is nil.

Discussion

When this method is called, it creates a new background task to handle the request. The method then returns control to your game. Later, when the task is complete, Game Kit calls your completion handler. The completion handler is always called on the main thread.

When this method is called, the data sent is marked as being resolved. This means that the data has been merged into the match data. If other players currently have the game running, they will receive a handleTurnEventForMatch call to indicate that matchData has changed. All completed exchanges must be resolved before ending a turn. If the completed exchanges have not been resolved, then calling endTurn, participantQuitInTurnWithOutCome, or endMatchInTurn will result in an error.

Parameters

An array of GKTurnBasedParticipant objects containing the participants who are to receive the reminder.

data

The data to be exchanged between players.

key

The location of the alert message string in the Localizable.strings file for the current localization.

arguments

An array of NSString objects to be substituted using the format string.

timeout

The length of time the next player has to complete their turn.

completionHandler

A block to be called after the exchange request has been sent.

The block receives the following parameters:

exchange

The updated exchange to be passed.

error

If an error occurred, this error object describes the error. If the operation was completed successfully, the value is nil.

Discussion

When this method is called, it creates a new background task to handle the request. The method then returns control to your game. Later, when the task is complete, Game Kit calls your completion handler. The completion handler is always called on the main thread.

Allows an exchange request to be sent to one or more participants. Each recipient receives a push notification that uses the supplied localizable message. An error is returned if any of the participants are inactive.

Parameters

An array of GKTurnBasedParticipant objects containing the participants who are to receive the reminder.

key

The location of the alert message string in the Localizable.strings file for the current localization.

arguments

An array of objects to be substituted using the format string.

completionHandler

A block to be called after the reminder has been sent.

The block receives the following parameters:

error

If an error occurred, this error object describes the error. If the operation was completed successfully, the value is nil.

Discussion

When this method is called, it creates a new background task to handle the request. The method then returns control to your game. Later, when the task is complete, Game Kit calls your completion handler. The completion handler is always called on the main thread.

When this method is called, each recipient receives a push notification using the supplied localizable message. This allows the game to send reminders that a turn or exchange request needs action. The receiver will have a turn event generated for the current game.

Declaration

Parameters

key

The location of the alert message string in the Localizable.strings file for the current localization.

arguments

An array of NSString objects to be substituted using the format string.

Discussion

Only the current player is able to set this message. The message sent is localized on the receiver side if the game is installed and on the sender side otherwise. The localized message identified through the key parameter can be formatted with the %@ and %n$@ specifiers. These specifiers use the objects designated in the arguments array.

The localized message is evaluated locally from these keys and is sent as well so that devices that do not have the game installed will see the message in the sender’s localization.