Not exactly a tutorial but I recently wanted to check out the Godot State Charts extension and compare it to a simple state machine. So I made a simple comparison project. Maybe it's of use to anyone.
Source code: https://github.com/Toxe/godot-state-machines
Playable web version: https://toxe.itch.io/godot-state-machines-vs-state-charts
The blue guard and player use my own simple state machines and the red guard uses Godot State Charts. Move around with WASD or cursor keys.
(Please note that this is not intended to be a fancy stealth game with some sophisticated AI or any kind of polish. I just wanted to play around with a couple of things and the guards might be dumb but that's fine. If this were a real project I'd put code like the player detection or movement into their own components. But this test project is already way too big for its own good.)
Has anyone used Godot State Charts before? Seems pretty okay from what I have seen. Although unless I have multiple parallel states I'll stick with my own state machines because they are more readable to me, faster and I can configure and customize more in code.
My own state machine base classes, a helper GuardState to make things simpler for the BlueGuard (provide guard
and player
references and make working with animations simpler) and an example "follow_player" state.
I use simple nodes for each state with an attached individual state script.
(Godot State Charts brings its own class called State
therefore I needed to rename mine to MyState
and MyStateMachine
but I think I'll live. 😆 )
The RedGuard uses Godot State Charts and has all of its code (guard and states) in one single script. Which I really don't like. I could of course come up with ways to split up that state/behavior code but to be honest I noticed that I prefer my own solution and wanted to finish this test instead of expanding on it.
I like having one Node and script for each state like I do with my own state machine for the BlueGuard but doing this for the RedGuard would only add more nodes and the need to click even more in the inspector. The best middle ground would probably be to add a "behavior" Node that has one script containing all the code related to the states.
A big plus though is that Godot State Charts has a really nice debugger that you can just drop into your project.
In conclusion
Godot State Charts seems neat but I'd stick with my own solution. Although to be fair I did not really play to its strength because I don't have parallel states and also I did not put much effort into refactoring the code needed for Godot State Charts. Still, I'd give it a thumbs up. 👍️