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

GameComponent Life-cycle

Last post 8/12/2008 8:45 PM by BShields. 6 replies.
  • 8/10/2008 1:57 PM
    • (920)
    • premium membership
    • Posts 394

    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 - Out now £5.99/$9.99

    Free Blitz 1UP games in return for testing!
  • 8/10/2008 2:05 PM In reply to
    • (920)
    • premium membership
    • Posts 394

    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 - Out now £5.99/$9.99

    Free Blitz 1UP games in return for testing!
  • 8/10/2008 4:47 PM 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).

  • 8/11/2008 7:13 PM 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.

  • 8/11/2008 7:34 PM 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.
  • 8/11/2008 9:59 PM 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
  • 8/12/2008 8:45 PM 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
var gDomain='m.webtrends.com'; var gDcsId='dcschd84w10000w4lw9hcqmsz_8n3x'; var gTrackEvents=1; var gFpc='WT_FPC'; /*<\/scr"+"ipt>");} /*]]>*/
DCSIMG