Hey all,
I have a FSM for my movement that keeps track of the current state of the FSM. The state of the FSM gets updated twice in one Update call, but for some reason the value is only set to the first time it gets changed, not the last time. Here is the essence of the code:
There is a state for Dashing. The state buffers inputs on enter, changes states to the “Walking” state on Update(), and sends the buffered inputs on exit.
public CSMoveDash
{
public Enter()
{
Input.BufferInputs();
}
void Update()
{
if (Some condition)
{
FSM.ChangeState("Walk");
}
}
public Exit()
{
Input.SendBufferedInputs();
}
}
When a state changes (E.g. from Dash to Walk), the FSM object keeps track of what the current state is.
public class FSM
{
public State CurrentState;
public void ChangeState(State newState)
{
if (CurrentState != null)
{
CurrentState.Exit();
}
CurrentState = newState;
CurrentState.Enter();
}
public void Update()
{
CurrentState.Update();
Debug.Log(CurrentState); // For debugging only
}
}
Now, when the dash state is exited, this sends the most recent buffered input out to whatever script is listening to inputs. One of these scripts is the attack script, which interrupts the movement by putting the FSM into the “interrupted” state.
public class Attack
{
AttackInputPressed()
{
FSM.ChangeState(InterruptedState);
FSM.CurrentState.Update(); // For debugging only
}
}
Now the issue:
I have an attack input buffered. When the dash finishes, it transitions to the walk state and sets the current state to the walk state. And then the exit function is called, sending my buffered attack input to the Attack class (This is done with custom delegate classes). The attack class changes the FSM to the “Interrupt” state. The next line when I call FSM.CurrentState.Update() is only there for debugging, but it properly prints out the “Interrupted state” as the current state. However, on the next Update() call from Unity (And subsequent ones), it still shows the current state of the FSM as being the “Walk” state.
Any idea what the issue is? I’ve combed through the scripts for hours and there’s nothing else affecting this particular system. It also seems like Unity uses a single thread so I don’t think race conditions would be the issue here. Especially since the entire flow is working line-by-line rather than coroutines / async. Is there something with Unity and how it handles object referencing / etc that I don’t know about that would cause this issue?