1.18 handle player status changes in multiplayer games
Problem
You want to detect when a player disconnects in multiplayer mode.
Solution
Implementation and ProcessingGkmatchdelegateClassMatch: Player: didchangestate:Host messages.
Discussion
In multiplayer games, it is important for a player to know the status of other players. The status here indicates a connection or disconnection.
Assume that you have compiled a racing game and used matchmaking. After the two players connected, they started the first lap of the championship. Suddenly, player #2 is disconnected. In this case, you must notify the player of the message #1 and end the match so that player #1 can wait for other invitations.
Implement and handle changes to player statusGkmatchdelegateClassMatch: Player: didchangestate:Host messages. The parameter Player contains the ID of the player whose ID is changed.Didchangestate:Parameters (GkplayerconnectionstateType), will contain one of the following values:
Gkplayerstateunknown
The player status is unknown. In a racing game, you may want to temporarily keep the player's car position unchanged.
Gkplayerstateconnected
Player connection. You may want to display a message (for example, "Player 2 is connected. Let's go !") To local players.
Gkplayerstatedisconnected
Players are disconnected. This status determines whether to stop the game, return to the main menu to wait for another match, and temporarily pause the game.
You can useExpectedplayercountThe instance method knows that it takes more than one player to start the game. For example, if we start a game that requires two players and one of them is disconnectedExpectedplayercountThe return value is 1, indicating that another player is required to start the game again. In the following code, assume that we are in a match between two people, and if one of them is disconnected, we will stop the match.
/* The Player state changed
(Eg. connected or disconnected )*/
-(Void) Match :( gkmatch *) Match
Player :( nsstring *) playerid
Didchangestate :( gkplayerconnectionstate) State {
Switch (state ){
Case gkplayerstatedisconnected :{
If ([Match expectedplayercount]> 0 ){
[Match Disconnect];
}
Break;
}
}
}