Hey all,
I've recently come across a need to use RTTI in my game. I'm concerned about potential performance hits for using it. Are my concerns valid or is the performance hit for a inheritance graph traversal (every object in every frame) negligable?
For further info, here are some details on my situation:
I have two abstract base classes that an object can compose itself from: IController and IState.
IController has the virtual methods: Frame(), HandleInput(), and Render().
IState has the virtual method GetState().
All game object inherit from IController, but only some will inherit from IState (the ones that have a physical state in the world, ie a position, velocity, mass, etc). So, a sample object would be class Player : public IController, public IState {};
I'm keeping a master list of all objects in my game, a vector<IController*> m_Objects. Something like this: IController* c = new Player(). m_Objects.push_back(c); etc for every different kind of object I have. Every frame, I cycle through the list if IControllers and call their Frame(), HandleInput(), and Render() methods. It looks something like this:
void Frame()
{
std::vector<IController*>::iterator It;
for( It = m_Objects.begin(); It != m_Objects.end(); It++ )
{
(*It)->Frame();
(*It)->HandleInput(); // etc
}
}
However, if the object inheriting from IController* has state (also inherits from IState), I would like to call additional methods on it. So now, my Frame function would look like this:
void Frame()
{
std::vector<IController*>::iterator It;
for( It = m_Objects.begin(); It != m_Objects.end(); It++ )
{
(*It)->Frame();
(*It)->HandleInput();
// RTTI
IState* state = dynamic_cast<IState*>(*It);
if( state )
state->GetState()->DoStuffWithState();
}
}
So, is this bad or is it acceptable?