Saturday, December 1, 2007

Networking Step 1: SignIn to LIVE

Ok, so to play a network game XNA uses Microsofts XBox LIVE service which is now supported on both PC and XBox 360. So we going to need to get our game to give the option of signing into the LIVE network.

Lets start off by adding the Gamer Services game component which is required before networking API calls can be completed.

So lets go and add this to our Game.cs class in the constructor just before we setup the ScreenManager:
Components.Add(new GamerServicesComponent(this));
Position the cursor after the word GamerServicesComponent and then press "CTRL" + "." and you should be prompted to add the GamerServices (see image below)

Ok, so the best place to sign-in would probably be on when selecting XBox Live from the menu... so lets go back there and add the following: (this is in the LiveOptionsMenusScreen.cs)

Edit the OnSelectEntry to have the following:

/// <summary>
/// Responds to user menu selections.
/// </summary>
protected override void OnSelectEntry(int entryIndex)
{
switch (entryIndex)
{
case 0:
// XBox LIVE

// Check to see if we have an account thats signed into LIVE
bool isSignedInToLive = false;
if (Gamer.SignedInGamers.Count > 0)
{
foreach (SignedInGamer signedInGamer in Gamer.SignedInGamers)
{
if (signedInGamer.IsSignedInToLive)
{
isSignedInToLive = true;
break;
}
}
}

// If not signed in lets display the Sign-in Guide
if (!isSignedInToLive)
{
if (!Guide.IsVisible)
{
Guide.ShowSignIn(1, true);
}
}

break;

case 1:
// Split Screen

break;

case 2:
// System Link

break;

case 3:
// Back
// Go back to the main menu.
ExitScreen();
break;
}
}

So basically what we doing here is checking to see if a LIVE account is signed in... if not display the Sign In window (which is created and handled by XNA).

So now run the game and go to Multiplayer -> XBox Live... if its your first "Games for Windows" game you playing or if you have Auto Sign-in set to disabled, then you'll be asked to SignIn and be presented with something similar to this:
Otherwise if you have signed in before it should automatically sign you in when the game starts (it will try sign you in from the moment "Components.Add(new GamerServicesComponent(this));" is called)
If successful you should see a toaster popup saying you have signed in after a few seconds of loading the game... similar to this:
Once Signed in you can now use the Xbox button on your 360 joypad or the HOME key on your keyboard to access your GamerTag Profile and messages etc..

2 comments:

Anonymous said...

Hi thanks for a great tutorial but could you help me? I've been working though it and i keep coming up against errors that say:
"Error 1 'GameStateManagement.Screens.LiveOptionsMenuScreen.OnSelectEntry(int)': no suitable method found to override"
and:
"Error 2 'GameStateManagement.Screens.LiveOptionsMenuScreen.OnCancel()': no suitable method found to override LiveOptionsMenuScreen.cs 117 33 GameStateManagementWindows" would you be able to help? Thanks again gavobrien@gmail.com

Edward said...

I have a feeling you may be using the new GameStateManagement sample for XNA 2, at the time of writing my article Microsoft had only released the GameStateManagement sample for XNA 1.1

Try using the sample from here:
http://creators.xna.com/Headlines/developmentaspx/archive/2007/01/01/Game-State-Management-Sample.aspx

And select XNA GSE 1.0 Refresh version.

I'll have a look at upgrading to v2 in the near future.