Well, or anyone else, but Kup is very helpful at this...
I find myself writing a lot of classes that use states (all game programmers do) and I'd wondered about writing a generic StateSwitching base class (or similar) to use with all of them. The class would just basically accept two functors for each state - one to call when the state is switched to, and one to call when it's switched from, sort of analogous to "loading" and "unloading" a state.
The StateSwitching class interface would be something like:
I don't know that the way I'm going about it is even the most modern way to think about it, so I guess my question is whether or not you're familiar with abstract or inheritable implementations of state-switching mechanisms and how they're implemented in C++. A way to pass member functions of derived classes into base classes wouldn't hurt, either, if you know of any. A language that supports closures seems like it would probably have better facility for this stuff, but sadly I am not working in one at present. I suppose I could define the state-switcher as a template class, but
I know this is kind of detailed, so if you don't have time/interest in helping it's completely understood.
I find myself writing a lot of classes that use states (all game programmers do) and I'd wondered about writing a generic StateSwitching base class (or similar) to use with all of them. The class would just basically accept two functors for each state - one to call when the state is switched to, and one to call when it's switched from, sort of analogous to "loading" and "unloading" a state.
The StateSwitching class interface would be something like:
Code: Select all
The problem that occurs that makes this impossible is implementing the functors. To preserve good data hiding, I would like to have the functors be implemented as or call private/protected functions in the derived classes, like:class StateSwitching
{
protected:
void RegisterStateFunctors(int stateID, functor loadFunctor,
functor unloadFunctor);
public:
void SwitchState(int stateID);
int GetCurrentState();
}
Code: Select all
However, the strong typing on C++ pointer-to-member functions mean that I can't pass
const int ATTACKING = 0;
const int DEFENDING = 1;
const int KO = 2;
class Enemy: public StateSwitching
{
private:
void ChangeStateToAttacking();
void ChangeStateToDefending();
void ChangeStateToKO();
public:
void Initialize()
{
RegisterStateFunctors(ATTACKING, &Enemy::ChangeStateToAttacking, NULL);
RegisterStateFunctors(DEFENDING, &Enemy::ChangeStateToDefending, NULL);
RegisterStateFunctors(KO, &Enemy::ChangeStateToKO, NULL);
}
//some other code that uses the Enemy class
{
Enemy orc;
if (/*something*)
orc.SwitchState(ATTACKING);
else
orc.SwitchState(DEFENDING);
if (/*orc dies*/)
orc.SwitchState(KO);
}
Code: Select all
as a parameter to &Enemy::ChangeStateToAttacking
Code: Select all
.StateSwitching::RegisterStateFunctors
I don't know that the way I'm going about it is even the most modern way to think about it, so I guess my question is whether or not you're familiar with abstract or inheritable implementations of state-switching mechanisms and how they're implemented in C++. A way to pass member functions of derived classes into base classes wouldn't hurt, either, if you know of any. A language that supports closures seems like it would probably have better facility for this stuff, but sadly I am not working in one at present. I suppose I could define the state-switcher as a template class, but
Code: Select all
seems like it would just be jumping through a lot of hoops for the final functionality I'd be getting.Enemy : public StateSwitching<Enemy>
I know this is kind of detailed, so if you don't have time/interest in helping it's completely understood.