Search code examples
c#xnaxna-4.0

What is a good way to handle loading multiple content, in the LoadContent() method/function


So I know it is bad to load content on constructor calls but is it ok to call a .load function for a class from the LoadContent()? ex.

protected override void LoadContent()
{
    spriteBatch = new SpriteBatch(GraphicsDevice);
    viewport = graphics.GraphicsDevice.Viewport;
    player1.load(spriteBatch, Content);
}

Within the .load I would have Texture2D playerT = Content.Load<Texture2D>("player");

Is that ok? or what is the best way to handle multiple content that needs to be loaded?


Solution

  • The issue with loading things in a constructor call is that your likely going to want to unload content at times to save memory but keep an instance of the class around that way the class is ready to be used again by simply calling load(). So loading any time after the Game class initializes itself is fine and should be done based upon when the content is needed not a design convention.

    So anytime past Game.Init is fine. And if the player class needs content right off the bat then calling load inside LoadContent() is the best place to do it.