LoadContent does not work in my components

For some reason, the LoadContent method is not called in my components. For example, I have a 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: if you remember that the initialization order and the LoadContent all work fine!

+4
source share
2 answers

I suspect your problem is related to the Initialize function. LoadContent is called Initialize . You need to check two things:

  • Make sure you create and add your game component to Game.cs before calling base.Initialize() . In the above code, you create and add a component to the LoadContent Game.cs function that occurs after Initialize .
  • Make sure the Initialize function in your Editor class calls the basic Initialize function:

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

Check out this blog post by Nick Gravelin for more information. Especially with regard to your question, Nick writes in his post that:

  • You will first receive an initialization request. Here you usually put a non-graphical initialization code for your game *. You also need to make sure that you call base.Initialize (). When you do this, the game does a few things:
    • Calls Initialize each GameComponent component in the Components collection.
    • Creates a GraphicsDevice.
    • Calls LoadContent in the game.
    • Invokes a LoadContent for each DrawableGameComponent in the Components collection.
+13
source

LoadContent will not be called if the GraphicsDeviceManager has not been registered before base.Initialize is called. The following code correctly registers the GraphicsDeviceManager for the game.

 public class Game1 : Microsoft.Xna.Framework.Game { GraphicsDeviceManager graphics; public Game1() { graphics = new GraphicsDeviceManager(this); } protected override void Initialize() { base.Initialize(); } } 
+4
source

All Articles