Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

XNA Differences between Initialize and LoadContent

What differences are there between Initialize and LoadContent? I have tried to brake the XNA game by moving things from LoadContent to Initialize and back but as long as you call things the appropriate way everything works no matter where I put it.

like image 477
Ryan Foy Avatar asked Jan 11 '23 14:01

Ryan Foy


2 Answers

The documentation for LoadContent states that

This method is called by Initialize

The documentation for Initialize states

In classes that derive from Game, you need to call base.Initialize in Initialize, which will automatically enumerate through any game components that have been added to Game.Components and call their Initialize methods.

So the base initialize method will initialize game components before any content is loaded, the LoadContent method ensures that your resources are loaded at the appropriate time.

like image 163
Sayse Avatar answered Jan 21 '23 23:01

Sayse


Game.Run() (see your Program.cs, i.e. game.Run()) is the method that runs:

1) Game.Initialize() (Which calls Game1.Initialize() and the initial Game1.LoadContent()).

2) Game.BeginRun() (which calls Update(), Draw() and subsequent LoadContent()/UnloadContent() (generally speaking).

MSDN - Game Class

In other words, initial LoadContent() statements can be put into Initialize(), but the problem is when you want to load, reload or unload things after initialization is done.

In short: Keep all your loaded content in LoadContent(), to avoid confusion.

like image 42
Ronnie 'Madolite' Solbakken Avatar answered Jan 21 '23 21:01

Ronnie 'Madolite' Solbakken