Advertisement

Behavior in an FSM - Internal or External?

Started by August 20, 2009 08:41 AM
1 comment, last by bepawuca 15 years, 6 months ago
In other words, when I implement an FSM for behavior management, should I do it like this? (C#, for the purpose of this example)

delegate void BehaviorFunction();

public class State
{
    ...

    // The behavior is run internally by the FSM
    public BehaviorFunction behavior;
}

Or like this?

public class State
{
    ...
}

...

// The behavior executed is run externally by checking the current state
switch(currentState)
{
case Afraid:
    ...
}

Or maybe I should be doing this some other way?
I prefer to overwrite a statehandler-method like onState to implement my behaviour. For instance:

public interface StateBase{  void onState();}public class AfraidState : StateBase{  ...  public void onState()  {    // implement behaviour  } }

So you can add new state-implementations just by building new StateClass derived from the interface StateBase. If you are using a switch-statement you have to touch a lot of places in your code.
I am not quite sure if your first approach should descripe something similar to this, maybe the approach was the same.

With best regards,
Kimmi

A complicate solution may indicate a not understood problem.


[twitter]KimKulling[/twitter]
Advertisement
I should have thought of doing it that way. Thanks, Kimmi.

Quote:
I am not quite sure if your first approach should descripe something similar to this, maybe the approach was the same.



The solution you gave is more like the first one in that it prefers the containment approach; the only real difference between the two is that the way you suggested has earlier binding than mine.

This topic is closed to new replies.

Advertisement