How To: Manage Player Movement Between Lobby and Gameplay Modes
Describes the properties and events associated with a multiplayer session that allow a game to move between lobby and game play modes. It identifies the game's current mode.
Managing Player Movement Between Modes
To manage player movement between lobby and gameplay modes
Hook up the session state change events.
The NetworkSession.GameStarted event indicates that all players have moved from the lobby to gameplay. The NetworkSession.GameEnded event occurs when the players move from gameplay back to the lobby. NetworkSession.SessionEnded occurs when the session is over, and all players move from the lobby of a session back to find another available session.
session.GameStarted += new EventHandler<GameStartedEventArgs>(session_GameStarted); session.GameEnded += new EventHandler<GameEndedEventArgs>(session_GameEnded); session.SessionEnded += new EventHandler<NetworkSessionEndedEventArgs>(session_SessionEnded);
Use NetworkGamer.IsReady to signal that a player is ready to move from the lobby to the game.
// Signal I'm ready to play! if (IsButtonPressed(GamePadButton.A)) { foreach (LocalNetworkGamer gamer in session.LocalGamers) gamer.IsReady = true; }
On the host gaming machine, check NetworkSession.IsEveryoneReady to see if all players are ready.
If this is true, call NetworkSession.StartGame to change the game state from the lobby to gameplay mode.
// The host checks if everyone is ready, and moves to game play if true. if (session.IsHost) { if (session.IsEveryoneReady) { session.StartGame(); foreach (SignedInGamer signedInGamer in SignedInGamer.SignedInGamers) { signedInGamer.Presence.PresenceMode = GamerPresenceMode.InCombat; } } }
In the NetworkSession.GameStarted event handler, make preparations for the game to begin.
In this example, the game object states are reset for the new game.
void session_GameStarted(object sender, GameStartedEventArgs e) { //// Reset everything when we are starting a new game. NetworkSession session = (NetworkSession)sender; for (int i = 0; i < session.AllGamers.Count; i++) { Tank tank = session.AllGamers[i].Tag as Tank; tank.Reset(i); } }
In the NetworkSession.GameEnded event handler, make preparations to return the players to the lobby, such as recording the high score.
void session_GameEnded(object sender, GameEndedEventArgs e) { ReturnToLobby(); }
In the NetworkSession.SessionEnded event handler, return to the game title screen.
Note that before the session ends, the session can move between lobby and gameplay states as many times as the player wants. To retrieve the session state at any time, you can check the NetworkSession.SessionState property.
void session_SessionEnded(object sender, NetworkSessionEndedEventArgs e) { ReturnToTitleScreen(); }
See Also
Concepts
Tasks
How To: Create a Network Session
How To: Find and Join a Network Session
How To: Manage Players Joining and Leaving the Game