Hello, I’m pretty new to game development but I’m having a lot of fun with it! I have two issues I’m dealing with right now that I’m hoping to get some guidance on.
They’re both related to finite state machines.
My states inherit from IState (not a MonoBehaviour), and my state machine is a MonoBehaviour.
The StateMachine class has ChangeState(IState newState) and ExecuteStateUpdate().
The object that the state machine is attached to will call ExecuteStateUpdate() in its Update().
ExecuteStateUpdate() calls the current state’s CheckConditions() to see if there’s a new state to change to, and then calls the state’s Execute().
Anyways, here are the two issues I have:
-
So each state watches its user’s input and reacts to these inputs. Many of the states contain physics usage like AddForce(). Isn’t this a conflict? I heard that physics calls need to be in FixedUpdate() and user input needs to be in Update(). So then, let’s say we put the StateMachine’s ExecuteStateUpdate() inside FixedUpdate() instead of Update(), we then have the problem of the inputs being read in FixedUpdate() instead of Update(). Any ideas? Is there another kind of state machine that I should be aware of that uses both Update() and FixedUpdate()?
-
Each StateMachine class calls CheckConditions(), before ExecuteCurrentState(). Sometimes the ExecuteCurrentState() function will send conditions to another state machine. If we have the case where one state machine sends new conditions to another machine, it will check its conditions, including the ones received from another machine that frame. We can also have the case where a state machine will CheckConditions(), then ExecuteStateUpdate, but then also receive its new conditions later that same frame from another state machine, meaning that it didn’t wait before receiving conditions before checking them.
I don’t want any of the state machines to operate out of sync like this. I would rather that they all CheckConditions() before any of them begins to ExecuteStateUpdate().
How do people normally accomplish this sort of behaviour? I’m new to programming, but I thought I would share my ideas on how to handle this; maybe some of them are close to the mark!
Idea 1: make use of LateUpate() or LateFixedUpdate().
Idea 2: Have a master state machine that holds a reference to every other state machine, which will call all of their CheckConditions() before calling their ExecuteStateUpdate(). Is this a normal way of handling this? Are there other programming concepts or types of state machines that I should know about that would give me the control I’m seeking?
Looking forward to responses : )