Download

Best way to do a simple finite state machine in C++

Hello, Unreal friends.
I have recently started programming in UE, and I am making a simple character controller.
I came from Unity, where I tend to use finite state machines to control my characters, as I find it a very intuitive, simple, yet elegant and powerful way of limiting player actions depending on whats happening in game.
However, I found tricky to do it in Unreal, being it a Event-based engine, where you bind input on start to specific functions.
This way, I find difficult to limit certain actions.
Lets say, if I’m in “Attack state”, and I don’t want the player to move while attacking. In Unity, being a Query-based engine, you just put the movement code inside the “Moving state”, and naturally when exiting the state the limitation of “can’t move” will happen.
In Unreal, however, as when I’m entering a state input is still binded, I should manually specify that you can’t do certain actions (in this case, moving). In a simple context, could be viable, but if i’m planning on escalate the states and add more functionallity, just going over every state telling what actions player shouldn’t do is the exact opposite of the purpose of a state machine.
The most elegant, self-contained way I have found is: when entering a state, I bind every input I need. When exiting state, I unbind all previously binded inputs, so I assure when I enter a new state, no functionallity from the previous remains.
This way, I force the machine to just do what every state allows it.
My question is: is this doable? I don’t know about the runtime cost of re-binding inputs, and being new to Unreal I don’t know if its a more elegant, intuitive way than re-binding inputs over and over again (which I suspect could be very prone to human error, like forgetting to unbind something).