[FREE] Simple Finite State Machine - Blueprint - For 4.11 and Up

Hello everyone! I’m here to share this tool to help you manage the logic of your characters by using states, it’s very simple but I hope it works for your projects.

The following image shows the relevant nodes you’ll need. The “FSM” node is required in order to access the States enumerator, which will be used with a “Switch on” node, notice that it needs to be connected to the Add State and Set State macros. The “Add State” macro is the brain of the state machine and is needed every time you create new states, every time you enter a state you can print it on screen, you can also print the time passed since the state is active, the On Enter execution output only fires one frame and could be useful to set initial values for that state, the On Update fires every frame, here’s where you do the logic and your checks to change to different states, the On Exit fires one frame, could be useful to reset some values when you leave that state. You can query the current and previous state, the “Not Exit” bool is useful to prevent conflicts with the controller inputs in a state, I will explain more about this in the next tutorial. I hope this helps someone, any comments/feedback are welcome.

https://docs.google.com/uc?id=0B6peUoeJH_CaMjFBRlVWTVFTVjA

TUTORIAL

[spoiler]We’ll start by creating a new project using the Third Person template, now let’s unzip the file in your project’s content folder.

[SHOT]https://docs.google.com/uc?id=0B6peUoeJH_CaZF9VVmVMcGtxSFU[/SHOT]

In you content browser you will see the FSM folder, inside is an enumerator.

[SHOT]https://docs.google.com/uc?id=0B6peUoeJH_CaZTVnbS1UaGxCRU0[/SHOT]

here is where you add new states, let’s double click to add a few of them.

[SHOT]https://docs.google.com/uc?id=0B6peUoeJH_CadzJ6cVJ1OXd6MVU[/SHOT]

Let’s open the ThirdPersonCharacter blueprint and add a new component, type “FSM” and select it.

[SHOT]https://docs.google.com/uc?id=0B6peUoeJH_CaREdTMWlvUVFaOVU[/SHOT]

If you select the “FSM” component, in the details panel you can see the “States Enum” enumerator, the ideal thing to do would be to choose your initial state from here, but in the current build 4.14 there is a bug that ignores when you choose default values for enums, so we need a workaround.

[SHOT]https://docs.google.com/uc?id=0B6peUoeJH_CaVXdmU3dhdTFNVmM[/SHOT]

Let’s add an Event Begin Play, drag the “FSM” component to the graph and from the node get the “Set States Enum” enumerator node and set it to any initial state you need, in this case we need “Idle/Run”

[SHOT]https://docs.google.com/uc?id=0B6peUoeJH_CaTHRuaHc0MXpJWTQ[/SHOT]

[SHOT]https://docs.google.com/uc?id=0B6peUoeJH_CaM0k1QlUyREZsWm8[/SHOT]

Get a “States Enum” from the “FSM” node, add a “Switch on StatesEnum” node and connect it to an Event Tick.

[SHOT]https://docs.google.com/uc?id=0B6peUoeJH_CaSWxkNUNrRFhtWHM[/SHOT]

This was the initial setup for our state machine, now let’s add our first state, drag a wire from the “Idle Run” execution output, type “Add State” and hit enter.

[SHOT]https://docs.google.com/uc?id=0B6peUoeJH_CaSXdDOHdxaGtVdGc[/SHOT]

[SHOT]https://docs.google.com/uc?id=0B6peUoeJH_CaNF9hODJ3by1XZlU[/SHOT]

The default behavior of the Third Person Template basically is to move, idle and jump, but there is a problem with this, if you try to move in the air, the controls for movement are still responding, allowing your character to rotate in the air and even change the impulse direction, In our case we don’t want that behavior, so let’s fix that, now let’s link all the nodes responsible of making our character move to the idle/run state. Next we are going to do something I like to call “Filter Controller Input Events”, which consist on creating custom events that we are gonna call in a “Switch On” node. So, let’s create two custom events, one for “Move Forward” and the other for “Move Right” input events.

[SHOT]https://docs.google.com/uc?id=0B6peUoeJH_Caam83dExEUXNkZ1k[/SHOT]

Let’s add a float to each custom event that will pass the axis value of the original input events.

[SHOT]https://docs.google.com/uc?id=0B6peUoeJH_CaN3k0bHdSNjJSWnc[/SHOT]

Now let’s connect the custom events to the “Add Movement Input” nodes, the originals should be connected to a “Switch on StatesEnum”, and let’s call the corresponding custom event functions. Now the movement input is linked to the Idle/Run state.

[SHOT]https://docs.google.com/uc?id=0B6peUoeJH_Cadzd1VkhOZlVCOXM[/SHOT]

Let’s do the same for the Jump input event.

[SHOT]https://docs.google.com/uc?id=0B6peUoeJH_CablllUE5sWnVJM3c[/SHOT]

Here is where we use the “Not Exit” bool, which will be helpful to avoid possible bugs when pressing two buttons at the same time, for example, as we are in the idle state, we are allowed to Jump from here, but let’s imagine we also have a punch action, so if I press these two buttons at the same time, it is likely enter in conflict with the “On Exit” execution output, because at that moment the other pressed button can do his actions, I hope I’ve explained well.

[SHOT]https://docs.google.com/uc?id=0B6peUoeJH_CaSkdIQ0VNOTRhYlU[/SHOT]

We can do the same for the movement input custom events.

[SHOT]https://docs.google.com/uc?id=0B6peUoeJH_CaWXJwODhQUmExY0U[/SHOT]

It’s time to check if the character is in the air, so we can switch the state to “Jump”, for that I like to create a reference to the Animation Blueprint in the Event Begin Play, this way we can access to all the variables from that blueprint.

[SHOT]https://docs.google.com/uc?id=0B6peUoeJH_CaUzEwdmdNOGNrbE0[/SHOT]

If the “Is in Air?” var is true we call the “Set State” macro and from the state enumerator list we choose “Jump”. Don’t forget to connect the “FSM” node to the macro.

[SHOT]https://docs.google.com/uc?id=0B6peUoeJH_CaMFo4dzZ5UzEtemc[/SHOT]

As we are adding more complexity to the state it is very useful to collapse all the corresponding nodes into a collapsed graph.

[SHOT]https://docs.google.com/uc?id=0B6peUoeJH_CaWXloYWpSQm9NQWM[/SHOT]

[SHOT]https://docs.google.com/uc?id=0B6peUoeJH_CaTU5kMFZoZUtUZms[/SHOT]

Let’s add a new state macro and connect it to the jump output of the main switch on states, here we use the “On Update” to check every frame if the character is no longer in the air, if true we set the state back to idle.

[SHOT]https://docs.google.com/uc?id=0B6peUoeJH_CacHNJR1AxdWFPUXc[/SHOT]

Let’s test it! now every time you try to move your left stick in the air, you’ll notice that you cannot rotate or change direction.

[IMG]https://docs.google.com/uc?id=0B6peUoeJH_CadHZORWxKa19QZ3c[/SHOT]

Now let’s do something wild, create a new state called “SuperJump”, let’s make it so every time you land from air if you hit the jump button right after, it will perform the super jump.

[SHOT]https://docs.google.com/uc?id=0B6peUoeJH_CacjhGYzFnQ1kyQ1U[/SHOT]

[SHOT]https://docs.google.com/uc?id=0B6peUoeJH_CaV2tNRXpSeHdIUW8[/SHOT]

Let’s collapse all the nodes of the jump state into a function, so we can reuse it for the super jump state.

[SHOT]https://docs.google.com/uc?id=0B6peUoeJH_CaZXQ3eXV2Z2ZRV28[/SHOT]

Now create two float variables, “DefaultJump” with a value of “600”, and “SuperJump” with a value of “1200”

[SHOT]https://docs.google.com/uc?id=0B6peUoeJH_CaY3p1LW02cTlFUGM[/SHOT]

Let’s double click the Idle/Run graph, look at the selected nodes, all this do is, every time you press the jump button, it will check if the time is less than 0.1, if true it will set the character’s JumpZVelocity to the SuperJump value, and call the jump function. In case it fails, just calls the jump function. On Update, it will check if the character is in the air, if true it checks if JumpZVelocity is set to the “SuperJump” value and set it to that state, in case not it just calls the regular jump state. On Exit, every time we exit this state we set the JumpZVelocity to the default value.

[SHOT]https://docs.google.com/uc?id=0B6peUoeJH_CaNS1keVc1S1g4X0E[/SHOT]

If we try to play again, the super jump works fine, but there is one problem, every time you land from a super jump the character will do another super jump, let’s constraint it to a jump and then super jump only behavior. To do it we need to know which was the previous state, if that previous state was the super jump then we are not allowed to perform another one, and just a regular jump.

[SHOT]https://docs.google.com/uc?id=0B6peUoeJH_CaUnpGdHZuSGFWb2s[/SHOT][/spoiler]

File Link:

https://drive.google.com/file/d/0B6peUoeJH_CaVFZyWTVJbVlZaXM/view?usp=sharing

Thanks but none of the pictures are showing. :slight_smile:

hi
thank you but could you try and re-uploading the pictures ?

Really? I can see them perfectly, I will take a look from a different computer and fix it asap, thanks.

Fixed! I had to upload them to my google drive account, now it works.

Added Support for Hierarchies

Hi!, I’ve extended the functionality of the tool and is more powerful now, I’ve added support for hierarchies, this could be useful in many ways, for example you can create an exclusive branch in a higher priority when the character gets hit, the same way you’d do it in the animation blueprint, this means that you don’t have to worry about transitions from each state of your main locomotion state machine to the get hit state machine, just call the “Get Hit” event from anywhere and the animations will blend smoothly. I also removed the need of enumerators, I’ve added new nodes and the ability to set a new state when a non loopable animation ends.

I’m still testing before sharing just to make sure it works, if you guys have any questions or suggestions feel free to let me know, I can also make a video tutorial in case anybody is interested.

https://docs.google.com/uc?id=0B6peUoeJH_Caek9UUlY1WDR5MzQ

https://docs.google.com/uc?id=0B6peUoeJH_CaaVJ1Zk1aSGl2SEk

can u make a video of how to use / implement for new users
i would really like to know how to use an statemachine