Friday, October 14, 2011

XNA Networking Events, In a Nutshell

In my explorations of Microsoft's XNA and its networking features, it quickly became apparent that utilizing events makes life much easier. Managing game states in a networked game can be a pain. The most basic states are when you are picking a session to join, then when you are in a lobby of other players waiting to play the game, finally to actually playing the game. Although it would be possible to switch between these states without using some of XNA's baked in networking events, it results in much more complex code that is more likely to break and generally cause headaches. So here are some tips.

When you have successfully joined or created a NetworkSession, the first thing you should do in your lobby is hook the following events:

  • NetworkSession.GamerJoined
This event is fired whenever a gamer joins a NetworkSession. You want to do any initialization of the gamer here, or perhaps display a message onscreen displaying the gamers gamertag and "has joined".
  • NetworkSession.GamerLeft
Just the opposite of above. Whatever needs to happen when a gamer realizes that he left the oven on.
  • NetworkSession.SessionEnded
This is a little more interesting. This happens when a session is disposed. This could happen when the host of the game disconnects, and you don't have any host migration code. It could happen if your pet chews through your ethernet cable for example. You would most likely want to kick the player back to the session finding screen, gracefully of course, maybe be outputting a reason why the session so rudely ended.

  • NetworkSession.GameStarted
This is called when
NetworkSession.StartGame()
is called somewhere in your code. The advantage of using the start game function in your code is that the GameStarted event will be fired to each player. Letting you centralize any initialize anything you need to before the game starts.

  • NetworkSession.GameEnded
As you might expect, this puppy gets fired when you call.
NetworkSession.EndGame()
. Once again, allows you to transition the player to another state. The most likely scenario is changing to the lobby screen when this happens.

So, how do you hook these events? Its simple, lets take GameStarted for example:

// Assuming your NetworkSession is stored in CurrentSession

private void HookSessionEvents()
{
//hook all your events here, just using this one as an example
CurrentSession.GameStarted += CurrentSession_GameStartedHandler;
}

// change screen
private void gameStarted(object sender, GameStartedEventArgs e)
{

// Reset everything when we are starting a new game.
NetworkSession session = (NetworkSession)sender;

for (int i = 0; i < session.AllGamers.Count; i++)
{
GamerObject gOjbect = session.AllGamers[i].Tag as GamerObject;
gObject.Reset();
}

// show message on the screen for 3 seconds
ScreenManager.AddMessage("START THE GAME FOOL!", 3000);

// switch to your game screen / state here
}

Got it? Good. Now you are ready to tackle the actual game logic! That will have to wait until another time though.

No comments:

Post a Comment

Note: Only a member of this blog may post a comment.