Serious Problem

If you are a new Irrlicht Engine user, and have a newbie-question, this is the forum for you. You may also post general programming questions here.
Post Reply
Jookia
Posts: 170
Joined: Wed Nov 19, 2008 1:11 am

Serious Problem

Post by Jookia »

Is there a way to get ALL derived classes from the parent in c++?

My problem is this.

I'm making a state system, with CStateManager as the main class and CState as the state classes.

I want to do something like..

stateSystem.nextState = 'mainMenu' which would be my mainMenu class, but I don't know how.


In conclusion:

Need to store CState states so I can switch between them using names.

The CStates states are actually subclasses CState, so it'd be CMainMenuState.
Sylence
Posts: 725
Joined: Sat Mar 03, 2007 9:01 pm
Location: Germany
Contact:

Post by Sylence »

Afaik this can be done using RTTI but it is compiler dependent.

In my state system I'm using a simple core::map to store the states. So the name of the state is the index in the map.
Software documentation is like sex. If it's good you want more. If it's bad it's better than nothing.
Jookia
Posts: 170
Joined: Wed Nov 19, 2008 1:11 am

Post by Jookia »

I really have no idea how to do this. If I try to do that, that means I have to create it before i make the classes and load them into that array before I have a driver variable.

Thing is, I want a state system that runs off classes derived from CState.
monkeycracks
Posts: 1029
Joined: Thu Apr 06, 2006 12:45 am
Location: Tennesee, USA
Contact:

Post by monkeycracks »

In the state system I use for my projects (not that any ever get finished :D), this is how I do it


IGameState - pure virtual interface for states to inherit, also holds enums for states
-CExampleState - state that inherits IGameState



CStateManager.cpp - in CStateManager m_pCurrentState is a private IGameState*

Code: Select all

#include "CStateManager.h"
#include "CExampleState.h"

void CStateManager::changeState(E_GAME_STATE NewState)
{
if(m_pCurrentState)
{
m_pCurrentState->dropState();
delete m_pCurrentState;
m_pCurrentState = 0;
}

switch(NewState)
{
case EGS_EXAMPLESTATE:
m_pCurrentState = new CExampleState();
break;

default:
break;
}

m_pCurrentState->initState();
}
Make sure you declare m_pCurrentState as 0 in CStateManager's constructor. Sorry about the lack of code formatting, just typed it up.
vitek
Bug Slayer
Posts: 3919
Joined: Mon Jan 16, 2006 10:52 am
Location: Corvallis, OR

Re: Serious Problem

Post by vitek »

Jookia wrote:Is there a way to get ALL derived classes from the parent in c++?
No, there is not, at least not generically. You can use dynamic_cast<> to see if a class A derives from class B. You could leverage that to do what you want, but then your code would have to know about all of the classes involved at compile time.
Jookia wrote:I'm making a state system, with CStateManager as the main class and CState as the state classes.

I want to do something like..

stateSystem.nextState = 'mainMenu' which would be my mainMenu class, but I don't know how.
Use an associative container or provide a mapping yourself. There is a thread about state machines that you may find useful. You can find it here. I posted some state machine code and an example that I linked to from that thread.

Travis
Jookia
Posts: 170
Joined: Wed Nov 19, 2008 1:11 am

Post by Jookia »

The only way I'm seeing here is..

Make the class then add it to the state object. Then initalize it with device after I start the game.


What I want to do..

Make the class once I start the game, when the class starts, create a bunch of objects based on classes derived from it and store them using an array.
Post Reply