Hi, I've recently started learning XNA and i set out to make a re-usable framework and a game. And one of the things i've found myself asking is should i be using the DrawableGameComponent in the way i am.
Quick summary of what i'm doing:
I created a Behavior class and a Game Entity class. Game Entity holds all the core things that i think it should for now, position, size, rotation, type. As well as a list of the class Behavior that it will run through sending itself to like so:
public override void Update(Microsoft.Xna.Framework.GameTime gameTime)
{
base.Update(gameTime);
foreach (KeyValuePair<string, RBehaviour> element in behaviors)
{
behaviors[element.Key].Update(this);
}
}
And i added a similar thing for drawing, so that drawing can be done by behaviors as well. Its not the greatest set of classes in the world but at the minute it is not my goal to create the perfect classes for the job just ones that will work and allow me to learn from them.
Summary of Problem:
I first derived my Game Entity from Drawable component, and all game entities were put in the components list like so:
public RGameEntity(Game game) : base(game)
{
game.Components.Add(this);
...
i first thought that i was utilizing an XNA feature but a friend pointed out that it wasn't really what Drawable Component was for.
Doing so has also meant that ive had to throw around the Game variable so that dynamic creation of Game Entities is possible (which is annoying) and i also did some pretty horrible bodges with my sprite batch and other things to get things drawing. Which so far i haven't come up with alternate solutions to. I can post the code of these bodges for people confused on why i needed them but for now i find them quite embarrassing....its that bad...
However i removed this dependency on Drawable game component, made the code nicer, got everything working again, and everything now ran a lot slower with a ridiculously bad frame rate. At the moment all entities are added to a static list of entities when they are created like so:
public RGameEntity()
{
....
inMemory.Add(this);
...
}
and then ran through in static update and draws
public static void DrawAll(Microsoft.Xna.Framework.GameTime gameTime)
{
foreach(RGameEntity it in inMemory)
{
it.Draw(gameTime);
}
}
public static void UpdateAll(Microsoft.Xna.Framework.GameTime gameTime)
{
foreach (RGameEntity it in inMemory)
{
it.Update(gameTime);
}
foreach (RGameEntity it in toDelete)
{///this will remove all possible references to the game entity and so GC will delete it
it.behaviors.Clear();
inMemory.Remove(it);
}
}
again im aware this isnt an elegant solution (im aware none of my code really is in this project so far) but i would of thought this would work the same as the drawable component cycle but with less optimization.
However the whole thing ran a very considerable amount slower untill i stopped the fixed time step stuff with this line in the init:
this.IsFixedTimeStep = false;
And then everything ran as it did when they were derived from Drawable Component..
My Questions:
-What does drawable component actually do, is it ultra optimized?
-What should Drawable component be used for, should i carry on using it in this way and find a way around my bodges?
-If not why did my project run so slowly the other way, im assuming its to do with how XNA will sacrifice draw loops for update loops, can someone shed some light on how that works aswell?
Thank you for any constructive input =]