Using GamerServicesDispatcher to fix up exception handling
Since my last update to my exception handling code that showed how to handle using GamerServices in the exception game after the first game uses it, I’ve found out a much, much better way to handle this scenario. The key here: don’t use the GamerServicesComponent.
The GamerServicesComponent is a very minimal wrapper on top of the GamerServicesDispatcher. If we avoid using the GamerServicesComponent in both games, we’ll be fine. I’m personally moving away from using the component entirely, but you can keep using it in the real game and just use the dispatcher directly in the second.
Basically what you’ll do in your exception game is simply initialize the dispatcher directly if it needs to by calling Initialize and setting the window handle.
protected override void Initialize()
{
if (!GamerServicesDispatcher.IsInitialized)
{
GamerServicesDispatcher.Initialize(Services);
}
GamerServicesDispatcher.WindowHandle = Window.Handle;
base.Initialize();
}
And then just remember to Update the dispatcher each frame:
protected override void Update(GameTime gameTime)
{
GamerServicesDispatcher.Update();
// update other stuff
base.Update(gameTime);
}
And there you go. No more need to make your GamerServicesComponent a static property or rely on that bug in the Game class. Much better.
Hi
I like this error handling series and I am very please I can get rid of my last component, by using the …Dispatcher instead BUT… you knew there would be a but.
I can’t get the Guide message to display in Windows. It works great on the Xbox.
What extra trick do I need to do to get it to work?
Many thanks
That is odd. I fixed one bug in the code above which is that you should always set the WindowHandle in the new game. But it’s strange that this works fine on Xbox but not on Windows.
Personally though, I don’t worry about it. Since running on Windows I’m always under debugger, I can see when errors happen. Alternatively if you wanted to run off the debugger (or ship the game), you shouldn’t use gamer services as they aren’t in the XNA redist. In that case I would use System.Windows.Forms and display a message box instead. Or, for bonus points, make a nice, neat form that shows what happened with a button to email it to you or send it to your server.
Thanks for the prompt reply. I had already moved the Window.Handle in my code but it made not difference to the problem. I have just finished creating a simple game form and that is what I use on both Windows and Xbox. It’s not quite as neat as the Guide display, but it does the job. Although I am only ever going to distribute the game on the Xbox I might distribute the level editor on Windows so I like to keep the two code sets as similar as posible.
Thanks for the examples they have been very helpful.