Good: I'm pretty new to C ++ and static languages in general. Since the years of ruby (and other dynamic languages), I do not know if this is possible.
I am creating a game state system for ... well a game. I want to make the system easy for me to cut and paste into other games without any (or very few) changes.
Two things I want to improve are the way to switch states and the way to hold state pointers.
Any number of states can exist, but at least 2–3 states will always be in memory.
Ugliness number 1.
I currently have a state manager class with something like this:
void StateManager::changeState(StateID nextStateID)
{
if (currentState())
{
currentState()->onUnFocus();
if(currentState()->isDestroyedOnUnFocus()) {
destroyCurrentState();
}
}
if (m_GameStates[nextStateID]) {
setCurrentState(nextStateID);
}
else
{
switch (nextStateID)
{
case MainMenuStateID:
m_GameStates[MainMenuStateID] = new MainMenuState;
break;
case GameStateID:
m_GameStates[MainMenuStateID] = new GameStates;
break;
};
setCurrentState(nextStateID);
}
currentState()->onFocus();
}
This approach works, but I don’t feel very pleasant.
Is it possible to pass a type? And then call it new?
new NextGameState;
? a class State.
№ 2.
, , , , - , .
State* m_GameStates[MaxNumberOfStates];
NULL, , , .
, :
m_GameStates[m_CurrentState];
. , , , NULL, 2 3 . [ : ?]
vector_ptr, , , , . , , Ugliness No 1., .
.
,
.
user245019