Why are player controller state machines usually built with Node instead of Resource?

Godot Version

4.7

Question

I’m watching some first person controller tutorials to try to learn 3D and first person exploration controllers. I was wondering why are state machine states implemented as nodes instead of resources. i.e. There’s a StateMachine node that holds several children node like CrouchingState, WalkingState, JumpingState and so on.

My first idea is that Node has process() and physics_process() which are where the magic happens, but you could just have a StateMachine node that calls into a (physics or normal) process_method(delta)inside the current state. Same for the _input() methods.

Signals are also something that’s not exclusive to Node. Going through the docs, I can’t see the advantage.

Is there a reason for this general preference that I could be missing? I tried to avoid the state machine route, but I don’t think there’s a better alternative when having so many possible actions, like running, climbing, grabing stuff, and so on. Especially since I can foresee having more than one machine, one for each part of the character (movement, arms, aiming&camera, etc..)

Do you need Resource either? Saving/Loading to disk isn’t a feature in many state machines. You could get by with enum/match statements. When using state machines to split scripts into multiple files then one can usually get by with extending RefCounted.

I’ve seen a few tutorials use the node names for switching states, I suppose that reduces some amount of resource management, specifically reduces the state machine’s code by one line per possible state. While also introducing the possibility to misspell state names.

I’d bet it’s easier to make tutorials about node-based state machines. New developers on Godot rarely create a script outside of nodes, or are introduced to loose scripts by Resources.

You can implement it in various ways. I’d opt for callables.

Many people find nodes convenient because they have a “visual feedback”. There are also “historical” reasons. It proliferated from 3.x days, when GDScript didn’t have callables, so using nodes was seen as the optimal implementation.

I’m generally annoyed by node-based state machines. There’s a bogus “common wisdom” about FSMs that circulates among novices, thanks to a number of popular but misleading tutorials; state machines are falsely advertised as some sort of antidote to complexity and spaghetti code. On account of that they tend to get widely misused.

I hadn’t considered simply inheriting RefCounted since I hadn’t noticed that’s the main difference with Resource.

I am currently using match statements, and within each I’d call a specific procedure to handle the physics/input for that state in the same player_controller.gd file, but it was growing quite big so I wanted to see how other people dealt with that.

From my CS background I see them more like a complexity management strategy, more or less. I mean the FSMs we studied were infinitely simpler, but thinking of them as encapsulated state with a well defined interface kinda makes sense?

FSM is a useful pattern when your problem domain can actually be modelled with it. However, in Godot context, people tend to predominantly use them for modeling “player state”, that’s what popular tutorials are showcasing as the main use case. Imo, in most game types, the player cannot adequately be modelled via FSM. It’s a bad choice of pattern; the player will typically not be a collection of mutually exclusive states, except perhaps in most simplistic games. Based on what we see here in the help section of the forum, people often get stuck with bugs and confusion when trying to implement it. Quite the opposite of proclaimed complexity management.

That’s why I started breaking down the character-level FSM into smaller FSMs for horizontal and vertical movement, hands management and so on, which ends up being manageable with match statements as Gertkeno suggested.