XNA Creators Club Online
Page 1 of 1 (7 items)
Sort Posts: Previous Next

GameComponent Life-cycle

Last post 12/08/2008 20:45 by BShields. 6 replies.
  • 10/08/2008 13:57
    • (870)
    • premium membership
    • Posts 371

    GameComponent Life-cycle

    Hi All,

    I've had a nose around these forums, Googled, and read the two books I've got on XNA, but nowhere can I find a decent description of XNA's GameComponent lifecycle.

    In what order do Initialize, Update, Draw and LoadContent get called? The MS reference for LoadContent said that it should be used for loading any graphics-specific stuff; however when I do this my component's Draw method is called before LoadContent (which seems a little strange!) and is therefore throwing nullpointers because it can't find references to any of the textures I've tried to load in.

    I'm writing my code like this - is this 'best practice'?

    Constructor - creating instances of things that don't rely on external sources, or instances that will ask for external resources later

    Initialise - getting references to Services (SpriteBatches, ContentManagers etcetera)

    LoadContent - making calls on those services I've now got references to in order to load in textures and the like

     

    Managing Director, Binary Tweed Ltd.

    binarytweed.com New games that're a bit like old games, but better.

    Clover: A Curious Tale - New, improved, and coming to PC.

    Clover - Watercolour political platform puzzler.

    Free Blitz 1UP games in return for testing!
  • 10/08/2008 14:05 In reply to
    • (870)
    • premium membership
    • Posts 371

    Re: GameComponent Life-cycle

    Oh forgot to mention in the original post - is the ordering of Update and Draw predictable? For instance, I've got a component that encapsulates controller input that I'd like to ensure gets update called on it before any other components. At the moment I'm doing this explicitly in my Game instance and not adding it to the component list.
    Managing Director, Binary Tweed Ltd.

    binarytweed.com New games that're a bit like old games, but better.

    Clover: A Curious Tale - New, improved, and coming to PC.

    Clover - Watercolour political platform puzzler.

    Free Blitz 1UP games in return for testing!
  • 10/08/2008 16:47 In reply to

    Re: GameComponent Life-cycle

    Each game component has an UpdateOrder and, if it's a drawable game component, a DrawOrder property. You can set those to whatever you like where smaller numbers are update first. So for your input component, you could put this in its constructor:

    UpdateOrder = int.MinValue;

    Which pretty much guarantees it will update before anything else (the only exception being if another game component sets itself to that same value).

  • 11/08/2008 19:13 In reply to

    Re: GameComponent Life-cycle

    Deej:
    In what order do Initialize, Update, Draw and LoadContent get called? The MS reference for LoadContent said that it should be used for loading any graphics-specific stuff; however when I do this my component's Draw method is called before LoadContent (which seems a little strange!) and is therefore throwing nullpointers because it can't find references to any of the textures I've tried to load in.
    The order should be Initialize, LoadContent, Update, Draw, Update, Draw, Update, Draw, etc.

    I just tested it, using Console.WriteLine in the appropriate methods of a DrawableGameComponent:
    Load Content
    Initialize
    Update
    Draw
    Update
    Draw
    Update
    Update
    Draw
    Update
    Draw
    Update
    Draw
    Update
    Draw
    Update
    Update
    Draw
    Update
    Draw
    Update
    Draw
    Update
    Draw
    Update
    Update
    Draw
    Update
    Draw

    It seems odd that LoadContent comes before Initialize. The double updates were only happening when I changed windows.

  • 11/08/2008 19:34 In reply to

    Re: GameComponent Life-cycle

    Where did you stick those Console.WriteLine calls? Before or after the call to base.Initialize in your game component? Can you post up the code for the Initialize and LoadContent methods you had? Initialize almost has to be called first as it's the base.Initialize call down to DrawableGameComponent.Initialize that actually triggers the call to LoadContent if I'm not mistaken.
  • 11/08/2008 21:59 In reply to

    Re: GameComponent Life-cycle

    BShields:

    It seems odd that LoadContent comes before Initialize. The double updates were only happening when I changed windows.

    base.Initialize calls LoadContent on the component.  So depending on where your WriteLine call is compared to base.Initialize, you'll get a printout that puts LoadContent first.

    Ex-Programmer/Writer - XNA Game Studio
  • 12/08/2008 20:45 In reply to

    Re: GameComponent Life-cycle

    Nick Gravelyn:
    Where did you stick those Console.WriteLine calls? Before or after the call to base.Initialize in your game component? Can you post up the code for the Initialize and LoadContent methods you had? Initialize almost has to be called first as it's the base.Initialize call down to DrawableGameComponent.Initialize that actually triggers the call to LoadContent if I'm not mistaken.
    I put them on the first line of each method. It's possible I had some other code screwing up the order, because I just stuck it in the first DrawableGameComponent I found in the project I had open (IIRC, the component I used was a ScreenManager derived from the GameStateManagement sample)
Page 1 of 1 (7 items) Previous Next