This blog is no longer updated. Feel free to copy any useful information to other blogs or wikis. Thanks.


Exception handling and GamerServicesComponent

November 27th, 2009 | 2 comments

As mentioned in the comments for my last post on exception handling on Xbox, you can run into some issues with that code with the GamerServicesComponent. If your first game initialized it, you can’t initialize a second one. So how can we solve this issue without having to get rid of the nice message box in the exception game?

First our main game needs a static field/property for the GamerServicesComponent it will create:

public static GamerServicesComponent GamerServices { get; private set; }

Next, we make sure that we create and initialize the GamerServices by hand in the Initialize method. I do this by hand rather than adding to the Components collection and letting it do the work to avoid an issue where the component is created by not initialized. If I create the component and some other component throws an exception before the GamerServicesComponent can initialize, we run into issues. So I do this instead:

protected override void Initialize()
{
	base.Initialize();

	GamerServices = new GamerServicesComponent(this);
	Components.Add(GamerServices);

	// this is important since we're in Initialize but after base.Initialize()
	GamerServices.Initialize();
}

Then we can update our exception game to utilize this static property in its Initialize method when determining how to handle the GamerServicesComponent:

protected override void Initialize()
{
	if (Game1.GamerServices == null)
		Components.Add(new GamerServicesComponent(this));

	base.Initialize();

	if (Game1.GamerServices != null)
		Components.Add(Game1.GamerServices);
}

This works only because of a bug in the XNA Game class (and who said all bugs were bad? ;) ). Basically any game component added in the Initialize method but after the call to base.Initialize() will never be automatically initialized by the game. This can be bad, but here it works to our advantage.

Our initialize method will first see if the first game created a gamer services component. If not, we add one before calling base.Initialize() that way our new game will automatically initialize it. Otherwise if the game did create one, we add that same instance to our new game after the call to base.Initialize() thus avoiding the game initializing it a second time.

By making these tweaks, you can easily use the GamerServicesComponent in both your game and exception displaying game for maximum user friendliness.

  1. SteveProXNA
    December 18th, 2009 at 04:58
    Quote | #1

    Hi Nick, I have been following this series on Catching Exceptions on Xbox 360 and A More Robust Exception System. Both code samples work great however, when I add the code above to the Game1 and ExceptionGame files, I now get a black screen without the MessageBox. Is it possible to email you the 2x game files directly to see if I’ve missed something?

    Also, I think this is a typo above: Game1.Add(new GamerServicesComponent(this)); should be Components.Add(new GamerServicesComponent(this)); no?

    Thanks,
    Steven.

  2. SteveProXNA
    December 18th, 2009 at 04:58
    Quote | #2

    Hi Nick, I have been following this series on Catching Exceptions on Xbox 360 and A More Robust Exception System. Both code samples work great however, when I add the code above to the Game1 and ExceptionGame files, I now get a black screen without the MessageBox. Is it possible to email you the 2x game files directly to see if I’ve missed something?

    Also, I think this is a typo above: Game1.Add(new GamerServicesComponent(this)); should be Components.Add(new GamerServicesComponent(this)); no?

    Thanks,
    Steven.

Comments are closed.