How To: Add Support for Game Invitations
The XNA Framework player match multiplayer network session type enables players to invite friends to play a game online over LIVE. Games of this type are able to host other players when the session is joinable, when there are open slots, and when game invitations are enabled.
Invitations can be sent from the Xbox LIVE friends list or through the Guide.ShowGameInvite method. Notification of accepted invitations is sent using the NetworkSession.InviteAccepted event.
Gamers can accept game invitations from the message center or while viewing the friends list. In addition, a gamer can request to join an existing game session without an invitation by selecting Join Session In Progress while viewing the friends list.
Note
When someone accepts an invitation for Xbox LIVE Community Games, but the game is not installed already on his or her computer, the invited player will be taken to the game offer. The offer enables the invited player to download the game and accept the invitation to start the game.
For creator games still waiting to pass peer review, the game invitation will be sent over the Xbox LIVE service to the other player. If the game is not installed the invited player will be prompted with a message indicating that he or she can get the game from the game creator.
To register for the InviteAccepted event
For your game to accept game invitations, register it as an event handler for the NetworkSession.InviteAccepted event when your game starts up. This event registration can be done either in the game constructor or in the Game.Initialize method.
NetworkSession.InviteAccepted += new EventHandler<InviteAcceptedEventArgs>(NetworkSession_InviteAccepted);
To respond to the InviteAccepted event
Two actions might result in the delivery of the NetworkSession.InviteAccepted event: either a player can view the friends list and accept an invitation, or a player without a game invitation can go to the friends list and select Join Session In Progress to request to join a friend's session.
If the gamer accepts an invitation or selects the Join Session In Progress option, an event notification will be sent to the game. When the game processes this notification, it is then sent to the registered event handler.
void NetworkSession_InviteAccepted(object sender, InviteAcceptedEventArgs e) { if (session != null) { session.Dispose(); session = null; } // Join the session from this invite session = NetworkSession.JoinInvited(maxLocalGamers); }
Note
If an invite is pending because the game automatically started in response to a cross-title invite, the method to handle the game invitation event will be called as soon as you subscribe to the event. Because of this, it is important to initialize everything upon which the event handler depends before it is registered.
To display the game invitation user interface
Whenever the game is able to host other gamers, the Send Game Invite option button will automatically be displayed in the Xbox LIVE friends list. Because a gamer can send invitations at any point simply by pressing the Guide button and navigating to the friends list, it is usually not neccessary to use the Guide.ShowGameInvite method. As a result, adding a game logic call Guide.ShowGameInvite at a particular instance in a game as demonstrated here is optional, but this action may provide a more polished user experience.
if (IsButtonPressed(GamePadButton.Y) && !Guide.IsVisible)
{
Guide.ShowGameInvite(PlayerIndex.One, null);
}
See Also
Reference
NetworkSession.AllowJoinInProgress Property
Guide.ShowGameInvite Method
NetworkSession.InviteAccepted Event
NetworkSession.JoinInvited Method
NetworkSession.BeginJoinInvited Method
NetworkSession.EndJoinInvited Method