I have a problem with slow transitions in some cases. One of these cases is if it is mining and a player comes and attacks him. I have seen it take more then 7 seconds for him to go from mining to combat. In that time a higher level player can almost kill it. Causing the miner to look almost retarded in this case. Below is the state checking code is there a better way to do this so that the miner does not look retarded. I know that part of the problem is in the loop I have the dowork. but I have yet to see a better way to do this and still have a somewhat weighted states.
foreach (State state in States.Where(state => state.actionRequired))
{
if (LastState != state)
{
if (StateChange != null)
{
StateChange(new object(), new NotifyStateChanged(state.Name()));
Logging.Write("State changed: " + state.Name());
}
}
state.DoWork();
LastState = state;
break;
}
Is there a better way to manage states then a foreach loop?
When I first wrote this code it was not a finite state engine it was all just hard coded into the character. That worked well but code was to big and maintaining it became a hassle, because i had to change each character file when I added a new state to them. So I switched to this and it is working good except it is a lot slower in some cases.











