Invites Sample
This sample shows you how to support invites in a networked XNA Framework game.Sample Overview
This sample builds on the Peer-to-Peer sample, adding support for invites.
To test invites, you will need two machines (either Xbox 360 or PC) with two XNA Creators Club subscriptions. This is because invites work only with LIVE PlayerMatch sessions, unlike other networking features that can be tested locally using system link. If you don't have two subscriptions, the easiest way to try this out is to get a friend to help, or ask for assistance on the creators.xna.com forums.
To join a session through an invite, run this sample on two machines, using LIVE profiles that are friends. Press A to create a session on the first machine. There are three ways the second machine can join this session:
- Using regular matchmaking, the second player can press B to search and join the session that was created by the first.
- The first profile (which is hosting the session) can go to its friends list, select the friend on the second machine, and choose the Invite to Game option. An invite notification appears on the second machine. If the second profile presses the Guide button and accepts this invite message, that player automatically joins the session. This can be thought of as a "pull mode invite" because the first profile sends a message to pull the second into the session.
- The player on the second machine can press the Guide button, go to the friends list, select the first profile (who is hosting the session), and choose the Join Session In Progress option. This is a "push mode invite" because the second player pushes himself or herself into the session without any direct involvement from the first.
Although pull and push mode invites appear very different to the user, they are implemented in the same way. This sample supports both scenarios using the same InviteAcceptedEventHandler method.
Sample Controls
This sample uses the following keyboard and gamepad controls.
| Action | Keyboard control | Gamepad control |
|---|---|---|
| Create a session | A | A |
| Join a session | B | B |
| Move the tank | UP ARROW, DOWN ARROW, LEFT ARROW, RIGHT ARROW | Left thumb stick |
| Aim the turret | A, D, W, S | Right thumb stick |
| Exit the sample | ESC or ALT+F4 | BACK |
How the Sample Works
This sample builds on the Peer-to-Peer sample. You should become familiar with it. The changes required to support invites are as follows:
- Changed the session type (passed to NetworkSession.Create and NetworkSession.Find) from SystemLink to PlayerMatch, because invites work only with LIVE PlayerMatch sessions.
-
Added one line to the
InvitesGameconstructor, hooking the NetworkSession.InviteAccepted event. By subscribing to this event, we indicate that our game supports invites. This will automatically enable the Invite to Game and Join Session In Progress options in the Guide friends screen. -
Added the
InviteAcceptedEventHandlermethod. This leaves the current session (if any), then calls NetworkSession.JoinInvited. It is not necessary to prompt the user before doing this, because the Guide will have taken care of the necessary confirmations before the invite was delivered to you.
Cross-Title Invites
If both profiles are already playing the same game when the invite is sent, it can be delivered immediately. But invites also work if your friend is playing a different game from you, or if your friend is in the Dashboard and not playing any game at all. You don't need to write any special code to enable cross-title invites. They are handled automatically by the system software. There are several possibilities for how this can work:
- On Xbox 360, if the target game is already installed, the system automatically reboots into it, then delivers the invite event as soon as it starts up.
- If the invite is for an Xbox Creators game (one that is in development or peer review), and the target game is not installed, the user gets a message saying the invite could not be delivered because the game was not found.
- For Xbox LIVE Indie Games on the other hand (ones that have made it through peer review and are being played by end users), when the target game is not installed, the user gets a message asking whether he or she wants to download it. The user is taken to the appropriate Xbox LIVE Marketplace page. Then, after the download finishes, the new game launches automatically and delivers the invite. This is a great way to encourage viral distribution. Make a fun multiplayer game that supports invites. Convince one person to give it a go. That person can spread it to all his or her friends simply by sending invites!
- On Windows, there is no good way to automatically find and launch the correct game even if it is installed locally. The framework just brings up a message saying "please run game XXX." If you launch the correct game within a few minutes, the invite will be delivered when it starts up.