Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

LoadContent does not work in my components

Tags:

xna

For some reason the LoadContent method does not get called in my Components. For example I have Game class in which I do:

//Game.cs
protected override void LoadContent() {
  editor = new Editor(...);
  Components.Add(editor);
}

//Editor.cs
public class Editor : DrawableGameComponent{
  Game game;
  public Editor(Game game, ...):base(game){
     this.game = game;
  }

  //THIS method never gets called!
  protected override void LoadContent() {
          background = game.Content.Load<Texture2D>("background");
          base.LoadContent();
  }
}

Any tips?

EDIT: When you keep in mind the order of Initialize and LoadContent everything works out fine!

like image 871
Andriy Drozdyuk Avatar asked Dec 03 '22 08:12

Andriy Drozdyuk


1 Answers

I suspect your trouble is due to the Initialize function. LoadContent is called by Initialize. There are two things you need to check for:

  1. Verify that you are creating and adding your drawable game component in Game.cs before base.Initialize() is called. In the code above, you are creating and adding the component in the LoadContent function of Game.cs, which occurs after Initialize.
  2. Verify that the Initialize function in your Editor class is calling the base Initialize function:

    public override void Initialize()
    {
        base.Initialize();
    }
    

Check out this blog post by Nick Gravelyn for more information. Particularly relevant to your question, in his post, Nick writes that:

  1. First you’ll get a call to Initialize. This is where you generally put non-graphical initialization code for your game*. You also need to make sure you call base.Initialize(). When you do, the game does a few things:
    1. Calls Initialize on each GameComponent in the Components collection.
    2. Creates the GraphicsDevice.
    3. Calls LoadContent on the game.
    4. Calls LoadContent for each DrawableGameComponent in the Components collection.
like image 146
Venesectrix Avatar answered Dec 20 '22 07:12

Venesectrix