XenoByteZero

Ok so I have a standard list of GameComponents, and need to loop through them on the containing classes Draw and Update methods. However some of the Components in the list are DrawableGameComponents.

So how do I only call the Draw method on the DrawableGameComponents that actually have a Draw method, and not the standard GameComponents that don't Is the only way to actually have 2 lists, one for Drawables and one for standards

cheers

Xeno


Re: XNA Game Studio Express Iterating through a GameComponents list?

dczraptor

foreach(GameComponent component in gameComponents) // use a for loop if you want

{

if(component is IDrawable)

((IDrawable)component).Draw(gameTime);
}






Re: XNA Game Studio Express Iterating through a GameComponents list?

Darkside

The good thing about game components is that you dont have to specifically call the draw and update routines during the Draw and update cycles as they are taken care of in the game framework, as long as you have added them to the game.components list.

When creating a drawablegamecomponent though you do have to add the Draw, initilize, Loadallresources and unloadallresources functions in manually, you can copy these from the main game component and change the proteted statement to public, then just add it to the main game class like any other component.

Also to remember is the order of components, if you have some drawable components that need to draw later that others (like an FPS component) then you need to change the order to something high(er).

For normal components you have an UPDATEORDER property, for drawable components you also get a DRAWORDER property, higher number = later, check the docs for more info

Hope this helps!

Darkside






Re: XNA Game Studio Express Iterating through a GameComponents list?

XenoByteZero

Thanks for the help. Im not using the Game.Components list, because im using a "Screen" based game design, where each screen has its own list of components, makes it easier to group the components together and do stuff like put them one on top of each other in draw order etc. This means on Game.Draw, it iterates through the list of active screens, and for each one iterates through the comonent list calling Draw on each one.

Didn't know abotu the draw order though, I will remember that.

Cheers again

Xeno





Re: XNA Game Studio Express Iterating through a GameComponents list?

Darkside

Do you mean screens or game states (menu / level 1 - / exit) etc, if so you may want to read George Clingerman's article on Game states in the forum (amongst others).

Each component can also activate and deactive it's draw and update routines

Update routines are controlled through the ENABLED parameter

Draw routines are controlled through the VISIBLE parameter

if false then the respective routines are not called.

Darkside