Godot Version
v4.5.1.stable.mono.official
Introduction
Hello! In my current project, I reached the point where I needed to make the state machine. I began doing some researches on how to make one and all the tutorials were awful but in the end I kind of understood how it works.
I thought of re-designing the usual way that it’s made in most projects (i think it’s called “node based state machine”) which i thought was very bad the way they did it, or rather the node based was not how a finite state machine should be structured in an entity overhaul.
the state machine shown in the example video is a prototype of what i achieved in like, of 1 day of thinking and a few hours of coding so it’s very raw for now. I haven’t thought of what else to add so for now this is what I will show.
How it works
I have 2 main scripts, one is a template for all the states and the other one is the main finite state machine NODE.
In the finite state machine I will have a “Beginning State” variable to set the first state for the specific entity I want and then a dictionary<string, StateTemplate> to keep all the other states.
The State template script will keep the base functions and variables for any state I want.
I add the virtual keyword to the functions witch allows me to overwrite them with a child, otherwise it will keep the normal functionality.
When I want to make a new state I just make a new script that inherits from the StateTemplate and then make it a global class allowing me to create a resource out of the script. With the resource I can plug it in any state machine. this comes with the cost of having double files but i don’t think it’s too heavy performance wise.
Unrelated note
- This is my first time doing ALL of this, the project and showcase and post. I’m trying my best especially writing in English because this is my second language and most of the time I write short messages. ANY feedback would be nice even angry ones
- Yes, I’m working in C# because i think if i learn how to use C# rather than GDScript I will have a better understanding of how programming works and having an easier time understanding other languages and how they are structured.
- If anyone has anything to object, I would be glad to answer because I don’t know how optimal this method is but it feels pretty good.
- This is a solution as well to those people who want to have multiple scripts in a single node

