How to use IGameComponent in XNA


In this mini-article I will be talking about implementing IGameComponent.

You can implement it in a game class like that:

1.gif 

It will automatically add a new function named Initialize:

#region IGameComponent Members

public new void Initialize()
{
  throw new NotImplementedException();
}

#endregion

Initialize Function called when the component should be initialized. This method can be used for tasks like querying for services the component needs and setting up non-graphics resources.

So when you use this interface make sure you use in a GameComponent, because Game class has its own Initialize function and they dont get well.

Inside Initialize function you can call everything before Loading any content that XNA can read. Such as starting something like an audio,video or setting the first value of a variable.

If you inspected Game Class Structure you will see IDrawable,IUpdateable and IGameComponent are already implemented in nature. That explains every Game Class in XNA 4.0 works as a component.So importing other user-created DrawableGameComponents will be easy to work.


Similar Articles