Using Gamer Services in your XNA Game

I started to play around with the Gamer Services this morning and have started to collect a small amount of code that may help others get started. The first is just a simple way to start a session and allow the Gamer to sign in.


To start just fire up a new Windows XNA Game from the Game Studio system, once done add the following line to the Games Constructor.


[code language="C#"]


Components.Add(new GamerServicesComponent(this));


[/code]


The above statement is all you need to intergrate the gamer Services into your application. Now to work on the sign in part.


You will find that when you first put this code in and run the application nothing will happen. To activate the in game guide you can either press the ‘Home’ key on the keyboard, or if you have a controller connected you can press the ‘Guide Button’. Doing this should bring up the Live sign in Guide. When you do sign in for the first time you may need to go through an update process, but this is relativly simple and does not take to long.


This is all well and good, but now we want to handle the sign in ans sign out of a player. For this simple task we are going to have to add two events to the code base right underneath the code we just added.


[code language="C#"]


SignedInGamer.SignedIn += new EventHandler<SignedInEventArgs>(SignedInGamer_SignedIn);
SignedInGamer.SignedOut += new EventHandler<SignedOutEventArgs>(SignedInGamer_SignedOut);


[/code]


When you are typing these in you will find that one of the great built in functions that ship with Visual Studio will assist you in creating not only the events but also the procedures caled by these event calls.


You should have the following two procedures created in your code base.


[code language="C#"]       


void SignedInGamer_SignedIn(object sender, SignedInEventArgs e)
        {
            throw new Exception("The method or operation is not implemented.");
        }


void SignedInGamer_SignedOut(object sender, SignedOutEventArgs e)
        {
            throw new Exception("The method or operation is not implemented.");
        }


[/code]


 


What I would like to do when a Gamer Signs in or out is to change the Windows Forms Title. I know this will not afect the Xbox 360 code, but at least you will be able to see what we are doing with this example. To change the Windows title change the two procedures to look like the following.


[code language="C#"]


void SignedInGamer_SignedOut(object sender, SignedOutEventArgs e)
        {
            this.Window.Title = "No Gamers Signed In";
        }


void SignedInGamer_SignedIn(object sender, SignedInEventArgs e)
        {
            this.Window.Title = "Gamer Signed In - " + e.Gamer.Gamertag.ToString();
        }


[/code]


If you run the application now you should find that when you activate the Guide (“Home” or “Guide Buttons”) you should be presented with an option to sign in to the Gamer Services. When you do the Windows Title will change to show your Gamer Tag as Signed in, and when you sign out (Pressing the Guide Buttons again and signing out) you should see the Title change to show that no gamers are signed in.


I do hope that this helps someone get started using the Gamer Services, over the next few weeks I am planning on doing a bit of work with them and will post what I find.


Cross Post from www.virtualrealm.com.au

One thought on “Using Gamer Services in your XNA Game”

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>