views:

114

answers:

1

In XNA, is it acceptable to create a GameService for an object that isn't a GameComponent? I know that a GameService is intended to be a way to access a GameComponent, but what if the object that you're trying to provide access to doesn't need to update on every update cycle? For example, I have a Camera2D object that can calculate a Matrix for SpriteBatch.Begin(). The camera doesn't need to update itself, as it's position, rotation, etc., are updated by the game code.

All that you need to create a GameService is a type and a provider object, so the provider object doesn't have to extend GameComponet, but is this an acceptable way of doing things? Is there a better way?

+1  A: 

Services don't have to also be a GameComponent. Just have a look at GraphicsDeviceManager, it provides the IGraphicsDeviceService and is not a GameComponent. The game services container is just a way of loosely coupling your objects.

Create an interface ICamera2D, make your Camera2D class implement it, and then register an instance of Camera2D as the service provider for ICamera2D in Game.Services. You then use that interface to communicate with the Camera2D instance in your other classes. This means that if at some point you have to change to OtherCamera2D, you only have to register it instead of an instance of Camera2D in your services container and do not have to modify your other classes.

smack0007
Thanks - this helped a lot.
Ben