Share via


Inviting Players to Join Your Game

The XNA Framework player match multiplayer network session type enables players to invite friends to play an online game on Xbox LIVE. Games of this type can host online players when the session is joinable, when there are open slots, and when game invitations are enabled.

Gamers can send game invitations from the Xbox LIVE friends list or by using the Guide.ShowGameInvite method. Game invitations can also be sent to members of a gamer's LIVE Party by using LocalNetworkGamer.SendPartyInvites. For more information about LIVE Parties, see Inviting a Group to Join Your Game (Xbox 360).

The NetworkSession.InviteAccepted event receives the notification of an accepted game invitation. For this reason, titles must register for the InviteAccepted event in order to send game invitations.

Gamers can accept game invitations from the message center or from the friends list. A gamer can also ask to join an existing game session without an invitation either from the game lobby or from the friends list by selecting Join Session In Progress. Either action causes an InviteAccepted event to be sent to the title.

Important

If your title does not register for the InviteAccepted event, its sessions always show as nonjoinable.

To register for the InviteAccepted event

  • Register an event handler for the NetworkSession.InviteAccepted event when your game starts.

    You can use the game constructor or the Game.Initialize method to register this event.

    NetworkSession.InviteAccepted += 
        new EventHandler<InviteAcceptedEventArgs>(
            NetworkSession_InviteAccepted);
    

Once the InviteAccepted event is registered, the registered event handler is called when the event is received. This happens either when a game invitation is accepted or when a player sends a request to join a session in progress.

To respond to the InviteAccepted event

  • In the handler registered for InviteAccepted, check if the invitation is for a preexisting local session by examining IsCurrentSession. If it is not for the current local session, call NetworkSession.JoinInvited to allow the gamer to join the remote session. If another local gamer has already joined the session, call NetworkSession.AddLocalGamer to add additional gamers to the session.

    void NetworkSession_InviteAccepted(object sender, 
        InviteAcceptedEventArgs e)
    {
        // if this is from a session that other local gamers are already in
        if (e.IsCurrentSession) 
        {
            // and that sessions is still running...
            if (session != null)                
                session.AddLocalGamer(e.Gamer); // add the additional gamer
        }
        else // else the invite is to a different session
        {
            if (session != null)
            {
                session.Dispose();
                session = null;
            }
            session = NetworkSession.JoinInvited(maxLocalGamers);
        } 
    }
    

Note

If an invitation is pending because the game started in response to a cross-title invite, the method to handle the game invitation event is 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.

When a game is able to host other gamers, the Send Game Invite option button automatically appears in the Xbox LIVE friends list. Because a gamer can send invitations at any point in the game by pressing the Guide button and then navigating to the friends list, typically you do not need to write additional code to display the game invitation screen.

There may be situations, however, when you would like to bring up the game invitation user interface (UI) yourself as a result of player actions in the game. In this case, you can use the Guide.ShowGameInvite method in your game's code to bring up the UI directly.

To display the game invitation user interface

  • Call Guide.ShowGameInvite to show the game invitation UI.

    You can do this from any point in your code, but be sure that the title already is registered to receive the InviteAccepted event as described above.

    if (IsButtonPressed(GamePadButton.Y) && !Guide.IsVisible)
    {
        Guide.ShowGameInvite(PlayerIndex.One, null);
    }
    

Invitations to Uninstalled Games

When a player accepts an invitation for an Xbox LIVE Indie Game that is not installed on his or her console, the invited player is taken to the game offer. The offer enables the invited player to download the game and accept the invitation to start the game.

For App Hub games still waiting to pass peer review and for which no game offer exists, a game invitation is still sent over the Xbox LIVE service to the other player. If the game is not installed, the invited player receives a message indicating the game is available from the game developer.

See Also

Tasks

Inviting a Group to Join Your Game (Xbox 360)

Reference

NetworkSession.AllowJoinInProgress Property
Guide.ShowGameInvite Method
NetworkSession.InviteAccepted Event
NetworkSession.JoinInvited Method
NetworkSession.BeginJoinInvited Method
NetworkSession.EndJoinInvited Method