Psycho Potato

Happy new year to all!

I have a (silly) question here: I constantly finding myself passing ContentManager reference to a GameComponent while I initialize it. Is there a way not to pass this reference and retrieve ContentManager reference from other means (e.g. through game services, etc.)

Or do I just create a new ContentManager for each GameComponents that I have Won't that occupies more memories (and also slower)

Thanks,
Ben.

 




Re: XNA Framework HOWTO: Get ContentManager from within a GameComponent?

Bill Reiss

You could create a static class which contains your ContentManager and any other objects you need in other classes (SpriteBatch is one you might need all over the place) and then get it from there when needed. Something like this:

public static class SharedObjects
{
public static ContentManager content;
}

In your Game1 constructor, you would replace

content = new ContentManager(Services);

with

SharedObjects.content = new ContentManager(Services);

and then in your code wherever you need to access it:

SharedObjects.content.Load<Texture2D>("sometexture");

This code is off the top of my head, so I hope it's accurate.






Re: XNA Framework HOWTO: Get ContentManager from within a GameComponent?

Psycho Potato

Thanks Bill, but how is this SharedObjects class being sharable to a GameComponent residing in another assembly (Game Library project) Surely I need something like "extern" in C++ Sorry this is my first attempt with C#, coming from a C++ background.

Regards,
_Ben






Re: XNA Framework HOWTO: Get ContentManager from within a GameComponent?

Jon Watte

GameComponent has a Game property. You could expose a ContentManager property you your Game subclass, and get the content manager by casting the Game.

myContentManager = (Game as MyGame).ContentManager;






Re: XNA Framework HOWTO: Get ContentManager from within a GameComponent?

Bill Reiss

That changes things a bit.

You could have a local ContentManager object definition in your GameComponent and set it to the ContentManager created in Game1 right after it's created since the lifetime of the ContentManager would most likely be the game's lifetime.

Alternately you could load all of your assets in the Game1 class and set the textures, models, etc. in your GameComponent class in the Game1.LoadGraphicsContent method, then the GameComponent wouldn't need direct access to the ContentManager. I guess it depends on your specific needs for that particular GameComponent.






Re: XNA Framework HOWTO: Get ContentManager from within a GameComponent?

Psycho Potato

Alright Bill, I figure I should "export" the SharedObjects static class from my game library.

// In MyGameLibrary.dll
namespace MyGame.Library
{
public static class SharedObjects
{
ContentManager content;
}
}

And initialize "content" in the main game executable:

// In MyGame.exe
using MyGame.Library;
namespace MyGame
{
public class CoolGame : Xna.Game
{
CoolGame()
{
SharedObjects.content = new ContentManager(Services);
}
}
}

Thanks for your help! :)






Re: XNA Framework HOWTO: Get ContentManager from within a GameComponent?

Psycho Potato

Ah, my post arrived after more answers came in. Thanks Bill/Jon, both methods work well for me.

Cheers!
_Ben