GameState/Manager and the ContentManager

Jan 1, 2011 at 9:49 PM

I've been looking at the demos and didn't find anything for this.  Is there an architecturally correct way to load content with the GameState/GameStateManager?  Assume Xbox 360 as the target device and the standard XNA content manager is being used.


Jun 10, 2011 at 8:31 AM
Edited Jun 11, 2011 at 3:09 AM

Saw this post and figured I'd chime in, even if it was about six months ago, because I had a similar question.  From what I understand, the best practice is to do your content loading inside of the constructor.  To do this, you can either pass a reference to the IGameStateService/GameStateManager in your GameState's constructor (assuming the manager provides access to a ContentManager) or you'll need to pass the components required explicitly in the GameState constructor.  Personally, I'm using custom class that each of my primary states uses, which is comprised of a ContentManager, SpriteBatch, and some kind of access to the GraphicsDevice.Viewport.

Joseph G.

Nov 21, 2011 at 9:33 PM

In addition to the information provided by Bludo, the content manager provided by the XNA framework is very limited in that it cannot selectively unload content. You must unload all loaded content or none at all. There are two popular methods to getting around this issue.

1) create your own custom content manager by inheriting from the one provided by XNA, and extend it to provide this functionality. this solution requires a lot more work than the second

2) create multiple content managers. A global content manager for content which must persist throughout the entire game (gui graphics/sounds) and additional content managers for content which does not need to persist. I tend to go with the second solution and have each gamestate create its own content manager.