Invites in XNA Game Studio 3.0

Originally posted to Shawn Hargreaves Blog on MSDN, Thursday, September 18, 2008

One of my favorite Xbox LIVE features is how you can go into your friends list, see what game people are playing, then join their current network session with just a couple of button presses. Or you can send an invite to a friend, asking them to come join your session.

If you made a networked game with the 2.0 XNA Framework, you probably noticed this wasn't enabled. I'm happy to report we fixed that for our 3.0 release.

The code you have to write to support invites is as minimal as we could make it. First, you subscribe to the InviteAccepted event, typically in your game constructor:

    NetworkSession.InviteAccepted += OnInviteAccepted;

This event handler should quit the current session (if any) and then join the new one:

    void OnInviteAccepted(object sender, InviteAcceptedEventArgs args)
    {
// Quit the current session if (networkSession != null)
{ networkSession.Dispose();
networkSession = null;
}
// Join the new session
networkSession = NetworkSession.JoinInvited(maxLocalGamers); }

That's it! The act of hooking up the InviteAccepted event will enable the "Invite to Game" and "Join Session In Progress" options in the Guide.

This code path is the same regardless of whether one player sends an invite to another, then the second player accepts it, or whether the second player chooses "Join Session In Progress" from their friends list, without the first player being directly involved. You can think of these two scenarios as pull vs. push, but your game handles them both using the same InviteAccepted event.

There is no need to confirm with the user before processing the event, because the Guide will have already taken care of that before it was delivered to you.

The really cool thing (and the part that caused the most tearing-out-of-hair while I was implementing it :-) is what happens when your friend is playing a different game to you, or if they are in the Dashboard and not playing any game at all.

On Xbox, if the target game is already installed, we will automatically reboot into it, then deliver 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, we just display a message saying the invite could not be delivered because the game was not found.

For Xbox Community 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 we bring up a message asking if they want to download it. We take them to the appropriate Marketplace page, wait for the download to finish, then automatically launch the new game and deliver 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, and they can spread it to all their friends simply by sending invites!

On Windows, there is no good way for us to automatically find and launch the correct game even if it is installed locally, so we just bring 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.

Blog index   -   Back to my homepage