Take the 2-minute tour ×
Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

I'm confused on how the game's methods are called and when.

So I read that the sequence is:

  1. Initialize
  2. LoadContent
  3. Update
  4. Draw
  5. UnloadContent

Where 3 and 4 are the game loop, so after Update is called, Draw is called, then Update, then Draw, and so on. The game loop (Update and Draw) is executed 60 times per second.

Then I read in another place that Update and Draw can be invoked at different times and Update may be called more then one time before Draw and so on.

... and at the end UnloadContent is invoked after the Update if the Exit method is called.

share|improve this question
    
The 60 frames per second assumes that isFixedTimeStep is set to true, which by default it is. You can set it to false if you want to eke out the most performance from your PC, or for general benchmarking. –  ChrisC Sep 27 '11 at 16:47

3 Answers 3

There is actually two ways that XNA can execute the game loop, Fixed Step and Variable Step. When you setup your game to run Fixed Step, it will consistently call the Update method based on the targeted elapsed game time. So no matter how fast your computer may be, it will always call the Update method at the same elapsed game time interval, so Update and Draw will get called in succession as you described. However, if your game happens to be running slowly, XNA will begin to call the Update method multiple times before the Draw method, in order to "catch up".

Variable Step on the other hand does not attempt to "keep up", or make multiple Update method calls before the Draw method call, if the game is running slowly.

EDIT: Nice form post that explains Fixed Step and Variable Step is much more depth. gamedev.net

share|improve this answer
5  
I found the best explanation as to how it works (for me) is here: Understanding GameTime - Shawn Hargreaves Blog –  Richard Marskell - Drackir Sep 27 '11 at 13:22
    
Thanks, but I don't understand yet if the time of 16 millis. between each calling is for all the game loop or is for the Update and then for the Draw method. –  xdevel2000 Sep 27 '11 at 14:50
    
@xdevel2000, the game loop is just the Update and Draw methods. But you can call other methods within the Update and Draw methods, all of which will become part of the game loop. –  Neeko Sep 27 '11 at 15:42
    
@Neeko, yes but you don't answer my question! Please, I ask you if 16 millis. are for each method or are for all game loop (Update and Draw). Thanks. –  xdevel2000 Sep 27 '11 at 18:15
    
@xdevel2000 Sorry that I'm answering this over a year later, but I just noticed this question! The 16ms is for an entire, single game loop cycle. Not for every method called within that cycle. –  Neeko Dec 22 '12 at 2:42

Unless you are writing a game for Windows, and you are loading unmanaged content, don't worry about UnloadContent. If all you are doing is drawing with SpriteBatch and maybe playing SoundEffect and Song objects, just forget about UnloadContent() for now.

For Update and Draw, when you are getting started with XNA, just think in your mind that the XNA Framework will automatically call Update, then Draw, then Update, then Draw... and so on. Are there exceptions? Yes. Do you need to worry about them when you are getting started? No, unless you are an experienced game developer already. If so, accept my apologies...

If your game uses Fixed Time Step, then it will delay so that this loop doesn't go faster than the time step. Windows Phone projects add in a line of code to fix the game to 30fps. You'll learn a little about that by doing nothing more than creating a brand new Windows Game project and then a brand new Windows Phone Game project. Look for the one line of code on the Windows Phone project that fixes time step.

The comment above to look at Shawn Hargreaves post is spot on. In fact, he is THE source if you want frank developer talk on the XNA framework. It's a few years of posts to go through, but that is a good resource.

share|improve this answer
1  
To expand a bit on UnloadContent, that is meant to apply to the default ContentManager that the Game object provides. You can create new ContentManagers for loading and managing different content in your games yourself, but in this case it's not necessary. The default ContentManager is disposed when Game exits, which then automatically unloads the content. –  ChrisC Sep 27 '11 at 16:51

I don't know a way of changing the order, but Update and Draw are just functions so you could call them from other places - though I can't think of a use of this. As a general rule, you would just use Update, Draw, Update, Draw etc. like you said.

Unload is called after exit function is called yes.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.