I’m coming from a Unity background, and in my projects, I tend to use the state machine pattern to handle how my characters control. For example, I’ll have the PlayerController class and it will have a member variable of type StateMachine which can call the Update, OnEnter, and OnExit functions of the currently active state (base class of type StateBase). I’ve found this to be very useful it complex projects that have many states like Locomotion, Combat, Swimming, Climbing, etc… because they control differently and the system does different things depending on state.
How are these things normally handled in UE4, particularly when using the Character base class? I’m tempted to go in and make a state machine, but when I google this, just to see how others are doing it, there isn’t much which makes me think people aren’t commonly doing things this way in UE4. I want to make sure my project has the best possible code standard. There are things I want to do while the player is only in certain states like while in air (like detecting ledges), and with the state pattern I would just look for ledges while in the air state object, but without this I would have to have an if statement that runs with every Tick, which seems a bit inefficient to me.
Posts like this make me miss Unrealscript where state scoped functions were so awesome. That said, there is already a simple FName based state machine implemented in Controller and it’s children (see ClientGotoState and ChangeState) as well as the movement physics system. Anything more complex than that requires custom code. In the case you are talking about I’d do the ledge detection in the movement component while it’s performing the falling state.
There are a lot of ways to implement a custom state machine, just depends on what you need it to do.
You could get really tricky and implement a good state machine with state UObjects/UStructs or Components. You could also utilize the FName based state system to do it with CallFunctionByNameWithParameters BUT that might be a bit slow. I’ve never looked at the performance characteristics of it.
So what I’m thinking is, I can have a state machine that inherits from AActorComponent. Then have other actual states that inherit from AActorComponent as well, so that way they can be added to the character and that would make it really easy to set and edit variables for settings specific to that state, like a swim speed or something.
Since components do not support C++ templates, I was thinking I could use the concept of interfaces in UE4 (which I found here: A new, community-hosted Unreal Engine Wiki - Announcements - Epic Developer Community Forums). Of course, C++ has no default concept of this, so it is effectively multiple inheritance. I was thinking that there could base like an IStateInterface that has the Enter, Exit and Update functions, and a state actor component could implement these functions. This could also work as the templated class so it is then possible to specifically pass in a character pointer or something like that. Then I was thinking I could use the default bCanEverTick to turn states on and off.
What do you think? Any criticisms?
EDIT: Was just trying to implement this and it almost worked until I needed to store the state variable in the StateMachine component, which means I have use type IState<typeInHere> but typeInHere depends on what the states are meant to interact with grrrr. So I can’t do that unless the component is a template, which it cannot be, and I don’t want to make unique state machine classes for each possible type. May have to go back to my original design for now.
I’ve ended up with an ActorComponent based State Machine, which I think is a nice design! Kept the interface separate, and then there is another base class specifically for my Character’s states which references the class.
Now I just need to figure out how I’m going to work with Unreal’s input system since the character input has bindings.