Hey everybody,
I am a self-taught programmer who wants to develop games. While fiddling around with the beginnings of a game engine, I had a thought: Why not make a standardized State Machine utility class?
The class, as I see it, would consist of the main class:
1 2
|
template <typename LogicType>
class Machine;
| |
Which would contain three internal classes, the State, the Node, and the Path.
1 2 3 4 5 6 7 8 9 10 11
|
template <typename LogicType>
class Machine
{
public:
template <typename LogicType>
friend class State;
template <typename LogicType>
friend class Node;
template <typename LogicType>
friend class Path;
};
| |
The State class would be similar to an iterator, except it would internally manage node and path traversal, as well as modifying (and checking for deletion) of an internally hooked "logic object".
The Nodes would consist of two function pointers: an initialization function, and a runtime function.
When a State first enters a Node, by either explicit overriding, initial entry into the machine, or traversal across a Path from another Node, the initialization function is called.
After this initialization finishes, the runtime function is called every time the machine is updated.
The Paths consist of a starting node, a destination node, and a boolean function pointer for checking a condition.
After the runtime function is called on each State, the machine finds all Paths leading from the specified Node, and checks each of them in the order they are found.
If one is correct, the state is moved to that node, and the process repeats itself.
When a State is created, a parent Machine and a "logic object" can be specified, in which case the state will be hooked and activated immediately, if possible.
Otherwise, a State will be inactive, and user-modifiable, until it is explicitly activated.
The State also checks the "logic object" regularly, and if the logic object does not exist in memory (checked (currently) using a static_cast to its own type, please give me any advice for better methods!), then the State is deactivated and removed (but not destroyed) automatically.
The source code for this is currently being written, please bear with me. I will post it here when the code has been thoroughly tested.