Chris Gomez

Development topics for the indie programmer

Start With the Game State Management Sample

Most presentations on XNA, including my own, start at File > New Project.  There are tons of great ones around the web.  Right now I would suggest Bill Reiss’ Intro to XNA tutorial, which is on Part 5 as I write this post.

The tutorial may seem focused on Windows Phone, but the technique of drawing a sprite, handling input, and playing sounds are key to all games, so it’s a good start and I’ll try to continue filling in the gaps for the questions I am hearing at my presentations.

If you have tried looking at my Circus sample that I use in my XNA talks, you’ll see I started with File > New Project, but this isn’t REALLY how you will want to structure your game.  Use this wizard when you are getting started with XNA and just playing around.  But when you decide you want to start looking at something with more features of a full game, start with the Game State Management Sample.

The Game State Management Sample drops you off at essentially the same place, with a game loop and a CornflowerBlue game screen that you simply start adding your code to, but it has all the basic structure of a game that you’ll want to at least consider when you’re thinking of publishing.

Where do I get it?

You can find the Game State Management Sample on the App Hub website, along with hundreds of other fantastic code samples and tutorials that will help you get up to speed with game programming in general (not just XNA).  The techniques are all here, and it’s awesome work by a team dedicated to bringing game development to the masses.

But when I get there, which version do I want?

At this point, there’s little reason to use anything other than XNA Game Studio 4.0.  And there’s little reason not to use a version optimized for Mango (Windows Phone 7.5).  So I’m going to get GSMSample_4_0_Mango.zip.  What is nice is that with the Windows Phone 7.1 SDK release of the tools, you get Visual Basic support now, so if you want that, get GSMSample_4_0_Mango.VB.zip.

When you do this, you are going to get source code, not a Visual Studio template.  It might have been a nicer Visual Studio template and maybe that’s a good project for us to get working either through NuGet or the Visual Studio gallery.  But for now let’s see what we get.

Unblock the zip file

My suggestion here is to first find the zip file you just downloaded and “unblock” it (if necessary).  Visual Studio will annoy you with messages that the projects aren’t from a trustworthy source, and it’s far easier to unblock the zip file before you extract it than to extract it and have to go try and unblock everything manually.

So right-click on the file and pick Properties.  You may see an Unblock button on the General tab.  Click it and you’re done with that.

image

Now extract it to your favorite working folder and let’s open up the contents.

You have three separate solutions in the GameStateManagementSample folder.  I’m going to open the Windows solution just by double-clicking on it.

image

The first thing you’ll notice is you get three projects.  The sample has the familiar content project (at the top in the screenshot below) and the “game” project (at the bottom in the screenshot below).  The mystery middle project is just a class library (or a dll project).

image

Just to whet your appetite, this sample has some great code you can just use or build upon to manage gather input, changing “screens”, and showing menus.  For now, let’s just build it and run.

Menus, Gameplay, and Input Tracking Ready

You get a Main Menu with the Play Game option highlighted.  Since I’m presuming you ran this on your PC, you can use the up and down arrows to highlight different menu options.  Then you can hit the Enter key or the space bar to select one.  If you have a controller plugged in, you can also use the thumbsticks or directional pad to highlight different menu options, and then use the A button or the Start button to select.

image

You should recognize that this screen system is implemented as a stack of screens. The background of light blue circles is actually a screen by itself, implemented as BackgroundScreen in the code.  The menus simply draw over top of it.  The system supports transitions between the screens that you’ll see when you select different options.

Eventually, you’ll hit Play Game.  You’ll see a faux loading screen to simulate that this sample supports a loading transition while you do some initialization (perhaps load specific content for a level) and then a game loop begins, with some simple fake “gameplay” ready to be replaced with your game.

image

As you dive into the Game State Management Sample, you’ll see code that detects controllers and their state, but also code to detect keyboard presses and touches and gestures on Windows Phone.  The stack of screens approach has worked pretty well for a great number of games.  Many games on Xbox Live Indie Games and Windows Phone use this sample as a starting point.

blog comments powered by Disqus