Member-only story
Making a basic finite state machine (Godot4/C#)
Let’s discover how to implement a simple FSM in Godot!

State machines are one of the go-tool for game devs when creating AIs, or object behaviours. And, in fact, it’s not that complicated a pattern!
So, in this tutorial, let’s talk about how to make state machines in Godot 4 and C# using a hierarchy of nodes :)
However — we’re not actually going to make a character today. Rather, we’re going to focus on a simpler and less studied example, but that’s just as important, because it demonstrates how state machines can also be useful to environmental prop — we’re going to create a basic traffic light!

I know, might not sound like much; but trust me, it will teach us a lot, and it’s actually just one proof of the fact that your game environments should live and breath, too, even though they contain “inanimate” objects ;)
Of course, if you want to get this basic traffic light 3D model I made or the code for this tutorial, don’t forget to check out the Github repo with all my Godot tutorials over here :)
And as usual, since we’ll be coding our logic in C#, make sure that you have a version of Godot with .NET enabled.

As usual, don’t forget that you can get the demo scene and all the assets from this demo on my Github 🚀 with all my other Godot tutorials.
The tutorial is also available as a video — text version is below:
Now, with all that said, let’s discover how to setup our first finite state machine in Godot!