Let's talk about state machines

So I started reading Modeling Software with Finite State Machines: A Practical Approach by Wagner et al., and a particular couple of paragraphs has become a brain worm for me for at least a week.

If the present input values are sufficient to determine the outputs, the system is a combinational system, and does not need the concept of state.

Sometimes combinational systems… are considered a kind of degenerated state machine, with only one possible state…

Besides the vagueness of what they really mean by “one possible state” (did they mean that it has no memory of previous states? Surely a system that is so petrified that it only has one state isn’t a system at all, let alone a state machine??) it got me thinking about the state machines we tend to make in our godot projects.

Often times, examples i’ve seen of state machines do in fact require the transition OUT of one state in order to enter another, but then again, often we just see a state being entered any time its required “combinational” logic is satisfied.

Personally, I’m a fan of creating a “state machine” that more or less looks like a big function with sub parts or sub functions that have the logic for entering them and their behavior together in one place. I also try to make sure the logic for entering each individual “state” is specified and differentiated enough that there are no two states competing to be entered at once. If the entry and behavior logic are clean enough, I find I never have to explicitly exit a state.

But maybe that’s just me.

What do you think?

Yes this is what they mean by a “combinational” system, it can take any input and provide a correct output and does not require state.

If I add two numbers and return their sum, this is combinational system. The input is sufficient to output a correct result.

If I have a lock where I need to provide a specific number to unlock, the secret number is the state thate makes it a state machine.

Concerning Godot, it has a state machine system built into the animation systems. I have written basic state machines, but I think I will just refactor to use the animation system.

1 Like

You know, I’ve heard about that and thought of it too. Couldn’t wrap my head around implementing it. Sounds like it helps keep transitions smoother. 2D or 3D? Seems like the 3D animation system has more guides than the 2D animation system for this purpose.

I’m pretty sure it’s agnostic to dimensions. It modifies the properties of objects.

Animation tree is where you want to begin, it has the internal nodes related to state machine building and you can even make a graph of your state machine.

I found this video helpful

The only drawback is the internal variables are inconvenient to modify. But if used right can save you a lot of code.

He shows how to use conditions, but learning how to use the expression option is a lot better imo.
1000003414

Animations can also call functions. Which is very handy. It is a very powerful system.

Ohh animationtree is the node you’re using. I can’t remember which ones I had read about but they weren’t working so nicely and each one was specific to either 2d or 3d. Ended up giving up on them.

This is a whole new rabbit hole to explore! Seems like a nice, clean approach that should reduce errors.