Jump to content
  • Advertisement
Sign in to follow this  
SelethD

In XNA can class object access Content Manager?

This topic is 3517 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I am programming in XNA and I have several class objects that need to load some graphics and then unload them when they are finished. So far I have been passing a reference of the loaded graphics to the object so that the object can use this graphics during its 'draw' function. What i would like to do is have my object somehow be able to access the Content Manager to have the loading of its graphics internally, so i only have to pass 1 reference of the content manager to my object during its load function and no references to be passed during the render. So what is the Content Manager? is it a certain type? how would i reference it Sorry if im not very clear, im not too familar with the proper terminology. Thanks

Share this post


Link to post
Share on other sites
Advertisement
There are many ways to do this.

Pass a reference to the content manager, and then load the same assets again. You'll just get references to the previously loaded assets.

You can also have many content managers, and when you dump them, they automatically unload everything they loaded.

The content manager is a class called ContentManager. It has an instance called Content in the Game class, which Game1 derives from. If you just pass a reference(Parent) around to 'Game1', you can access Parent.Content, and any classes local content manager too.

Share this post


Link to post
Share on other sites
You can also have a class named for example, Globals.cs, width a static content manager declaration:

class Globals
{
public static SpriteBatch mySpriteBatch;
public static ContentManager myContentManager;
...

When you init your game, you can assign the ContentManager of your device to this reference, at Initialize method of Game class.

protected override void Initialize()
{
base.Initialize();

Globals.myContentManager = this.Content;
}


At your classes, you can reference this global content manager, global sprite batch etc. for example:

texture = Globals.myContentManager.Load<Texture2D>....



Share this post


Link to post
Share on other sites
You already have that stuff in the game1 class. No need to re-assign it to Globals. You're just doing more work than you have to do. Just pass a reference to Game1 and your all set.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!