XNA, Accessing ContentManager and GraphicsDevice anywhere anytime, the GameServiceContainer
A quick code snippet that I produced today and wanted to share.
Often you find yourself looking for a good path/way to give a certain component access to the ContentManager. Usually you will just plug it in the construct. However sometimes the object creating the new object doesn’t have access to this component as well. Things start to become a bit more complicated then, of course you can add it to both constructors, but things often start to clutter.
Objects like the ContentManager and the GraphicsDevice are often needed on the craziest of places in a game’s code. They can easily be shared as they are usually in a valid state throughout the runtime of your program and usually you don’t need more than one. For these kind of objects it would be easy to create a static class so that anyone can access them.
A common technique is to create a static List
Luckily XNA has the GameServiceContainer class which allows you to store elements like this:
There’s also GetService and RemoveService. However this casting can make a lot of lines way longer than they need to be. Also we need to make the container static and protected access to it, so let’s create a nice little class like this:
In the Initialize method of Gam1.cs put this:
Remember that GameServices is static so you can read, add and remove from anywhere in your code like this:
Quite a nifty way of doing this, if I may say that myself (A). However do note (as NullSoldi did on Efnet #xna) that this will increase coupling between your components as some class will start to depend on the non standard class we’ve just created while this otherwise wasn’t needed. However I personally think that the benefits greatly outweigh the small penalty on coupling.
Have fun with this snippet, let me know if you’re using it or found it useful for your own GameServices-like class.
- roytries
- roy-t
- roytri