Controlling AI with Enum State Machine

I’m trying to switch between two infinite loops (states) using an enumeration and a switch statement.

My goal is to spawn numerous pawns and have them follow the main character, which is working fine. When I try to switch to another state (moving to a target location based on where the mouse is) by pressing the “f” key, only one pawn tries to move to that position instead of all the pawns. Also the one pawn seems to be glitching between moving to the new target position and following the main character.

How do I run each of these states infinitely until another button is pressed and change the state of ALL pawns, not just one?

Using the Target Actor input, the AI can move to a changing location without constantly calling MoveTo.

You can do this by replacing your timer and moveto with a simple loop:

Using StopMovementImmediately or calling a different AI MoveTo will cause it to trigger OnFail and stop the loop.

This is also much cleaner and easier to maintain.

Have a look at State Trees - bit of learning curve but you will need a more sophisticated AI brain at some point so its worth investing the time now so you don’t have to do a rewrite later. Overview of State Tree in Unreal Engine | Unreal Engine 5.1 Documentation

1 Like

Ok, I went ahead and learned about State Trees and I have it mostly figured out except for the state transition portion. I am not able to detect a key press when in the evaluation blueprint so I created 2 booleans in my AI Controller and set them as outputs. I then used a get function in the Evaluation Blueprint so I can use the variable to transition into the next state. The problem is, while the output variable is changing in the AI Controller blueprint, it’s not updating in the Evaluation Blueprint, so it never transitions to the next state.

Can you please let me know what I’m doing wrong?

Update, looks like I AM getting a true value in the evaluation blueprint. So my question is, why is it not transitioning to the “Move” state shown in the State Tree in the previous post?

I would separate the two things. Create something that says the button has been pressed (e.g. a Boolean in the Player Controller) and then use the “State Tree Evaluator” functionality to check the state of that variable and instruct the state tree to change state.