Finite State Machines in Godot 4 in Under 10 Minutes
ฝัง
- เผยแพร่เมื่อ 29 เม.ย. 2024
- In this video we'll walk though the setup and creation of a Finite State Machine (FSM) in Godot 4.
Finite State Machines are incredibly useful for managing simple states and transitioning between them. For this video we will look at an enemy with the states of wandering, following, and attacking. Then, we'll build the wander and follow states together in Godot.
Resources:
Additional State Machine tutorial by GDQuest: www.gdquest.com/tutorial/godo...
Sprout Lands (Tilemap and character): cupnooble.itch.io/sprout-land...
Dino (Dino): arks.itch.io/dino-characters
Ending music: • The 25th Hour - Schlat...
Chapters:
00:00 Intro
00:27 State Explanation
01:06 Wander State
01:28 Finite State Machine
01:59 Godot Time
02:07 Base State Setup
02:28 State Machine Initial Setup
03:55 Idle State Code
04:40 Testing the Idle State
05:08 Follow State Code
05:40 Testing the Follow State
05:58 Transitioning Between States
06:34 Afterthought
07:03 Cow - วิทยาศาสตร์และเทคโนโลยี
I started to notice a lot of traffic coming in from TH-cam and I think I just found one of the reasons why. Thanks for linking my sprites. 😊
Thanks for making such great sprites! They're one of the first ones I reach for when prototyping. I'm happy you don't mind me using them in a video like this
I feel like I've used your dinos for prototyping every platformer I've made, you're a legend within my group
so you are telling me that there is better way to manage code than writing 2000+ lines in single Gdscript?
@@impregnat0rjust write good code lulz
(obv joke is obv)
😂😂😂
Can I get a side of garlic bread with that spaghetti code? 🍝 🍝 🍝
@@impregnat0rI mean if you are coding you should take it into consideration yourself. Also, most people don’t start coding with GDScript. Surely some do, but not the majority. In those instances they should already be familiar with scripts working in conjunction with one another. I’ve never thought to put all my lines in one single script, but I also am in school for computer science and have had an advantage of learning good habits because of that.
@@impregnat0r also the approach of using dictionaries in this tutorial is an inefficient method.
ERRATA
- For the state class, the Process and Physics_Process functions are different than the built in _process and _physics_process functions. This should be clearer and I recommend using names like state_process and state_physics_process to avoid confusion.
- 3:31, the enter and exit functions called off of current_state and new_state should be capitalized
THANK YOU!
Im confused, so is it supposed to be _process and _physics_process like in the video, or Process and Physics_Process like in the comment?
@@zysdota The main issue for me was my Exit and Enter functions weren't' capitalized
a good idea. Thank you for your great video. Ill propably input an "EnemyState" extending "State" as for my navigation code i am working with a NavMesh and the export values are already exploding
@@zysdotaas far as i understood he is using "Process" because it gives him more control as in the State Manager he then assignes _process()
state_process is just a better name to differenciate it from the base godot class
If I hadn't known Finite State Machines for years beforehand, I would definitely know by now!
Your concise concept & excellent explanation makes FSMs - an often over-complicated yet rather basic topic - easily understandable by beginners as well as intermediate developers IMHO.
Awesome content quality I have rarely encountered with video tutorials, and especially on TH-cam.
Nicely done, keep it up!
Just wanna say that you did a great job explaining everything. Lots of youtubers often forget to bridge the gap between what they know and what actual beginners know. In the military we used to call it "barney style". Directions given in a way that a child could understand, and under the assumption that an adult could then branch out and use these tools for a complex mission.
this is what everyone needs to adopt!
I concur. This youtuber has the good stuff in regards to teaching absolute beginners to understand things like State, Finite Machine State, etc.
This channel is a treasure for indie godot devs.
definitely not a beginner tutorial, way too fast and didn't explain a lot of things, or maybe i'm just slow
@@PhotoBomber unfortunately tutorials are very general and you wont find perfectly catered material online or even in a classroom the only way you can achieve that is with a 1 on 1 tutor. i will say if it didnt make sense to come back to it after taking some notes and see if it comes together better with a different perspective. remember that learning how to code or really any skill has less to do with curriculum and more with immersion. aka if you want to learn how to code a game, the most efficient way is to actually code a game. along the way you will be able to identify what you know / don't know and then tutorials like this will become much more useful.
@@OHTASISAN I'm using chatgpt to learn to code, it's quite the 1 on 1 tutor, and catered to my preferences
I really love this tutorial,
Simple straight to the point and very intuitive.
Thanks for the amazing tutorial.
Please keep doing more Godot tutorials! Even as a total beginner who knows nothing about coding, I was getting something out of this.
I LOVE your Godot tutorials, I really hope you keep doing them!
I've watched so many tutorials on state machines and never understood them until now. Thank you very much!
Unashamed to admit I set the speed of the video to 0.75 and rewound the video dozens of times.
This is the most beautiful explanation of state machine I have come across. Kudos!
For anyone who is encountering an "invalid call" error, change the .exit() and .enter() at the bottom of the state machine scripts to .Exit() and .Enter() , this fixed all my issues.
I really like how the tutorial is fast paced but still gives enough info to understand the code
You have made the best Godot tutorials i have seen. You explain everything so clearly. Keep up the good work.
Such a simple and brief, straight to the point tutorial. I'd like to see more states as a part two, like one to circle/strafe around the player and attack, and maybe a second type of attack.
Long time had i suffered having no idea how to build such a state machine thing, before I found this.
This tutorial is of great help. Thanks a lot for your work!
I want you to know that your Godot tutorials are very good, and that you should continue making more of them.
I dont even understand state machines yet, I used this purely for knowing how to make something which follows the player. Already its been an incredible help, given a few months of learning I imagine the other parts of this will help me too. For some reason the previous things I'd tried like 'get_root' and copying node path were not working and I don't know why. But this worked, and knowing about groups is good too!
Man, this tutorials are amazing. I subscribed and I'm waiting for more.
Thanks
I do not comment that much on TH-cam videos. However I wanted to say that I wanted to learn how states machines were implemented on Godot and I never really grasped how. But this video that I found randomly explained perfectly the way. It changed my to make enemy AI. Thank you so much !
Got to say, this channel is truly underrated and it needs more audience, your videos so far are all I need for my new project using Godot 4. Thanks :)
I really hope these tutorials continue. I watch it slowed down a bit because you speak quickly, but your concise and informative so I subbed
This was one of the best videos on FSM. I hope you start to post more. like Player Movement / Attack and how you would handle that in a FSM.
Loved the video! Can't wait to get home to set it up. Thanks.
That's the most intuitive implementation of a finite state machine I've ever seen 👍
One of the best godot tutorial videos. In the past with Unity, I had built a very complicated state machine without any tutorials, and I wish I had seen something such as this, because I wouldn't have so much spagghetti (e.g. once the physics state machine was merged with AI behaviour, tech debt was sealed)
Nice. Very clean. It is also no problem to introduce more states and transitions between them.
Nicely explained.
Will definitely use this one to show to others on the beginnings of a FSM!
thank you so much. this guide helped me alot. I ended up re-writing the whole player code but I've finally managed to implement player movement and auto attack system without weird ass bugs. so it was worth it!
My godot game and game dev skills grow stronger with every video! Thank you so much!
Good example! Nice and simple. I usually use an enum for the states instead of strings...though this can be mildly difficult in Godot since the enum has to be imported into everything somehow. For one project I had an autoload class just for all the enums, but I'm not sure this is the best way to do it. I like the way the editor knows what the states are called after because these are in the enum. Maybe it's not as simple, but I find it harder to mess up.
this is incredible🌟 thank you for all those clean useful concepts
please make more Godot tutorials
I did not see such useful video for a while. Al three for Godot actually. Subscribed. So please do more :)
It really helps me to structure knowledge I already have and get new tricks to use.
Just getting into Godot myself. Interesting use of nodes for the state machine.
Thanks for the tutorials ! I'm still new at Godot, you really helped me well !
Please keep making more the last 3 videos are amazing.
Finally! I find tutorial with good code. With early return implementation, value verification and without other smell code. It's simple and brilliant 👏
Super good video, great outline to start making Finite State Machines in a game. Used this with other tutorials to get a basic Idle/Run/Airborne state machine working in a 2D platformer. Great lesson overall!
Took a little while to realise i needed to add "owner" as the prefix to a lot of values, as it's not the state nor statemachine that was holding the velocity and gravity variables, but the Player Object instead.
Thanks for this, really helped to see a bare bones example. Was able to easily adjust for 3D nodes
Great video, I was actually looking to clean up the way I handle state machines in my game, and this video is going to be very helpful!
Excellent guide, well done.
Great explanation, as always! 💙
Awesome explanation! This helped me out a lot! Good mix of theory, then demonstrating on how to put it into practice.
Top marks from me! I particularly like how you've approached decoupling the reference to the CharacterBody in the state by using an exported variable. This was always my gripe with other implementations and didng feel very clean at all.
Really love your tutorials. Hope you make more, im learning a lot from them
I got into Godot literally yesterday, and I've already watched all your videos on it. You provide perfectly concise and understandable content. Thank you for doing what you do, will be looking forward to more!
HEY YO MY MAN
im one of those that spends hours researching tutorials and i have a very large bone to pick with how the majority of people (even in great istitutions like universities) teaches things.
and since others already pointed it out im not gonna repeat myself and just say: you u got som good shit goin.
please keep on making godot/gamedev tutorials im sure your following will grow!
and of course, thank you very much for the effort you put in making these videos!
This channel motivated me to start learning Godot the explanations are soooo good
I'm absolutely subbing and keeping this as my note on gd script thanks
Amazing tutorial! I would love to hear your explanation on concurrent state machines as that's still something I'm struggling with. (For instance: Having a bird enemy which can idle, wander, follow and attack, but also be on the ground or flying, without having to make 8 states in a single state machine)
Dude this is best tutorial for State machines!
Very quality video for such a small channel. Looking forward to more
Used the basics of it in my game. Great video thanks.
What I think is great about this tutorial is that you don't need to know about the way each state behaves. I like that this isn't the main focus as each state for each game is different for each game dev.
At the same time, each state explained here is also extremely versatile due to it's simplicity so if you wanted to you could still use it and just iterate upon it later. But just to know how states work is quite wonderful.
man's literally fireship
not yet.... not enough memes
Wow thanks m8, this worked like a charm for my own project. I can't wait to keep building on it!
With component architecture, Godot really is so unbelievably intuitive relative to other software.
Wait how did you get past the error with the transition?
@@soundrogue4472 What do you mean. Signals handle state transitions as per the video
Great tutorial! maybe you should make one about context-steering behaviour to make the enemy moves smarter around
a good tutorial that doesnt start with a 2hours intro , thank you
These videos are great. Cant wait for whatever you ake next.
I hope we are going to see more videos like this in the future from you.
Great tutorial. Eagerly waiting for your next one
As a code architecture and statemachines fanatic, I thank you for this.
One of the few good tutorials on godot
Thank you man! I'm going to use this!
Thank you, master!
Good Tutorial. However when it comes to somewhat beginner Tutorials like this one, I'm a big advocate of "Show, don't tell".
The examples at the very end are certainly worth tackling, if not for code-re-usability and decoupling, which is a hard concept to grasp for game development.
The Video being this short would certainly not be degraded by 3 more minutes of you showing these concepts, at least briefly.
Awesome video!
A neat trick for switching states: using the return value from the state's process function instead of signals.
That way, you can have @export variables for each state to define its next potential states, and return them to the state manager that does the switching
This also removes the need for a dictionary!
Your video are so great! i really want to see more.
Holy shit this is actually really high quality
It would be awesome if you could provide the source code for this tutorial so it would be easier to reference ^.^
Thank you a lot! Excellent Explanation
This is one of the best tutorials and implementations I have ever seen.
It's a shame that you haven't posted any recent videos as your content is very helpful. :(
I'm actually working on more videos! I'm just a terrible procrastinator
yeah! go go go@@Bitlytic
@@Bitlytic Please make a "Utility AI" tutorial video.
I know how to create a Utility AI in Unity, but Godot structure is different and really mess with my way of thinking.
thanks so much for this. I'm currently splitting up my 2000 line single script into 10 smaller scripts
dude this is awsome
I don't think I ever learned things regarding Godot or coding as fast as I do here. Also I like your voice.
How a awesome tutorial! it's clear and useful. I am newbie in program and i can understand from this video. I really hope you keep doing them! I miss you Bro😄
hoping you could create a whole tutorial on this project you making a video of! your explanation is superb. thank you.
I used this approach for a while in situations when the `match` switch approach gets too complicated.
In a project with action platformer controls this approach wasn't enough too. So I blended it with the behavior tree style - states having under states that get checked every frame if that will execute its logic for the current frame. Like PrimaryState (WallSlide, Fly up, Fall, Run, Idle) and JumpGroup( Walljump, JumpBuffer, DoubleJump, JumpDown, Jump). In a hierarchical order.
Now I'm thinking about ways to improve it.
For me, the next step was to define hierarchical states so that shared behavior can be defined in ancestor states. For example, WalkState inherits TravelState. TravelState exports a speed property that it uses to determine velocity along with the input_direction property that it inherits from the DirectionInput state. Most of my states also are descended from an AnimationState which exports a property for the animation name, the animated sprite node, and starts the animation in the enter method.
The downside to this approach in Godot 4 is that since there is no longer any implicit super() call, you need to remember to call that anytime you override one of the state methods, and as well check if the superclass logic returned a new state that you should transition to instead.
Lastly, there's also no reason your StateMachine class can't also inherit State and be a state of its own with its own child states to further modularize your behavior, especially if there's cases you want to consider some external conditions and don't want to bloat your root state machine.
@@zarblitz Do you guys use gdscript or C# for your code? Would writing state machines be better in one or the other? Do you have source code for your method? I'm just interested in learning how your method works.
More videos like this, this is great!!!
I thought this was a pretty great video. You are explaining things clearly and concisely. I didn't know how to implement it in Godot, and now I do :D So thanks for that!
The only thing I think it's lacking, is focusing a little bit more on how to export those states outside of the state machine, even if you dedicated like a minute to this at the end of the video. In the video, the calculations for the Idle and Follow state are done inside the state itself, but in general, a state machine is also a tool to pass on an information that needs to be accessible for several systems. I feel like one might come to the conclusion that you need to move your code inside states scripts. When in reality, if you put a signal that emits "new state coming" at the end of the transition of the state machine, you can connect anything (like an Animation handler, a mouvement system etc.) so that they fire when the state is appropriate.
Minute point nonetheless, I really liked that video, and I'll probably check out what you've done otherwise.
Amazing video man! It would be great if you'd go deeper into state machines, well done!
Great video! Thank you so much ♥
yes! I found the best godot tutorial ever
this video help me alot thank for makeing it :D
It worked perfectly I am going to use everyday and all Night
really hoping to see how to actually make attacks, with this (im new to godot).
your video has helped me a ton on understanding anything tbh
Thanks, it really helped.
Wait more next video, I apreciate.
Really great video. But, atm my brain couldn't handle it XD. will go back to this vid later!
Very good! Please make "items system" tutorial (more scalable and simple like this tutorial)
ideally you would want to have the states to be independent of each other, since if you wanted to create a new enemy that instead of following, it runs away when the player you would have to make minor rewrites to your states to make it work. (Instead of using the same wander state, and just changing export variables for example)
In that case is it best for the machine itself to decide which state to transition to?
@@entropywilldestroyusall1323 you could either extend the state machine class for more complicated ai, or you could create an event listener that waits for a condition or a set of conditions ( probably just a signal) and then asks the standing to transition to another state
you can combine it with a Markov chain to determent what state is allowed to come next base on the current state.
I justa wanna ask if isn't better to manage the states from the enemy script, I'm learning how to code like this and I like it, but i think if you manage the transition directly in the enemy script the states are more reusables and don't force to move them to one state to another by default, what do u think?
Thank you for including captions in your tutorial. Almost no tutorials have captions, which can make it harder to follow, and especially harder to hear when the narration is quiet. The tutorial itself is too fast though, and could have used more examples. I got lost as to where the individual scripts went because you went too quickly.
Look up automata theory, if you design an automata first then program it into states. Highly recommended, I did it with SDL2.
Does this method mean I don't need to implement AnimationTree since it will travel in different nodes with different animations and functions?
So good’
Watching this i felt like the Kel meme from Good Burger. "Yes I know some of these words".
Im far too green at coding for this but saving it for later when hopefully I can understand more than the first 1.5 minutes.
Awesome tutorial, although the fast pace not typing kind of make it hard to do it at the same time, a lot of times I needed to pause and go back.
Godot 4 recently received a new add-on called LimboAi. It is designed to make it easier to create state machines for enemies in games. I wonder what would be your opinion about it (as an experienced developer). It looks really cool on paper, but their repository doesn't have much information on how to use this AI system. And I'm a newbie.