Loading/ unloading content in a separate class

Hello!

I am trying to create a separate ContentHandler class in my game, but I am having troubles loading/unloading content outside of the main game class.

I believe that in the olden days of XNA you could simply pass the ContentManager in the constructor for the new class, like so:

public ContentHandler(ContentManager content)
{
_content = content;
}

And then load/unload the content in the class’s methods, but I tried that and it doesn’t seem to work with MonoGame. Could anyone point me in the right direction?

1 Like

forget it, I just found out I was doing everything wrong :wink:

1 Like

Just in case anyone is an idiot like me and has the same problem, I had to:

  1. Add “using Microsoft.Xna.Framework.Content;” directive in my ContentHandler class;

  2. Make ContentHandler a subclass of ContentManager class, which is inherent in the framework;

  3. Add “IServiceProvider serviceProvider, string rootDirectory” in the constructor of the class;

  4. Instantiate a ContentHandler in the main game function, under this line:

    Content.RootDirectory = “Content”;

and pass Content.ServiceProvider and Content.RootDirectory to it like so:

content = new ContentHandler(Content.ServiceProvider, Content.RootDirectory);

Now I can load/unload game content within that class.

1 Like