0
\$\begingroup\$

I'm trying to implement FSM according to Programming Game AI by Example, it is a pretty standard and straightforward FSM that goes like similarly to this (some stuff omitted and translated from C++ to C#, hope it's fine):

class StateMachine<Entity> 
{
    Entity owner; 
    State<Entity> state; 

    void StateMachine<Entity>(Entity entity)
    {
        owner = entity;
    }

    void Update() 
    {
        state.Execute(owner);
    } 
}

class State<Entity>
{ 
    Entity owner; 

    void Execute(Entity owner) 
    { 
        if (owner.Foo) 
            owner.Bar(); 
    } 
}

class Entity()
{
    public type Foo;
    private StateMachine<Entity> stateMachine;

    public void OnStart()
    {
        stateMachine = new StateMachine<Entity>(this);
    }
    public void Bar(){}
}

My problem with this state machine is that the because the state machine is not part of the class anymore (as opposed to, let's say, the naive "switch" / "if-else" implementation), it is necessary to expose internal logic of the Entity (e.g. variable Foo, method Boo()). Is this the right way to do it? What if the the Entity class grows, how does it scale. Is it really necessary to keep exposing more and more variables / creating getters/setters?

How are these FSMs implemented in larger projects?

\$\endgroup\$

1 Answer 1

1
\$\begingroup\$

Good question, but really the answer is that FSMs are pretty non-scalable by design. You can consider your question arising from the fact that a FSM is by nature ad-hoc and hard-codish. Usually a FSM is chosen to describe a simple and specific behavior. If you wish to do something more scalable alternative methods are going to be favorable.

I'd say this is fine. A particular state machine is going to want to know specific details of thing it is operating upon. In a "larger project" a state machine is still going to be a state machine. They are pretty simple. This is sort of like asking "what is a custom vector (array, std::vector) class like in a big code base". Well, it will be well tested, perhaps efficient and reliable, but it's still just going to be a simple vector.

\$\endgroup\$
2
  • 1
    \$\begingroup\$ fsm are flexible and need not be hardcoded, for instance as conditions and actions custom classes can be used to create any logic flow and execute any tasks with an fsm \$\endgroup\$ Dec 16, 2013 at 11:03
  • \$\begingroup\$ @LearnCocos2D I agree, which is why I gave an opinion on their nature, not a stated fact. Hopefully my opinion describes why what he's doing makes sense for a FSM. \$\endgroup\$
    – RandyGaul
    Dec 16, 2013 at 11:25

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .