The Birds and the State Machines
State Machines while complex when you start, are extremely helpful for setting and customising a entities behaviour. Instead of having to hardcode a string of difficult behaviours I can now modularly attach behaviours together in a string to form complex behaviours for my enemies in Knott and the Waking Wilds.
What are State Machines?
State machines are a method for creating a flow for actions. Separating actions into reusable modules instead of making a massive if else statement of multiple behaviours. I know too well what happens to these chains, As something gets misplaced you can spend precious minutes to hours examining failures.
For instance let's say in a hypothetical sense you need to go to the shops to buy some milk.

When you run through the steps the Freecode is not so bad however will really only work for going to the shops to buy milk. However once we complete the flow, we realise we can’t buy milk because we can’t find any? We now need to make an addition to leave if we cant find it.

Now for something completely different, We need to find barber and get a hair cut. We could sit down and rewrite everything, however because we aren’t feeling like psychopaths today lets try reuses some of the code that we have already made.

We know how to drive, So we can reuse the Drive State and change the target. We also know how to Find something so we can change the target. However we don’t know how to receive the service. So that action will need to be created. Finally we know how to pay for something and drive back home.
We can reuse a bunch of those routines without being bogged down. While the above looks simple, In reality as routines and code gets more complex to accommodate niche and precise examples. Spaghetti will inevitably come knocking at your door. The Driving state can split into its own states from turn wheel, match speed via pedal and dealing with the unexpected flipping off the people who cut you off… That is everyone right?
If you are a beginner and you are still struggling to create the actions that need to be taken by the state machines. Learn to move objects and complete math. Then when you are confident, roll them into states!
Enum and Match, Made in Heaven.
Enum and Match go together like peanut butter and jelly. I had a similar system set up with a big if else chain separated with bools and value checks for No Good Guys. Things would constantly break and I would spend ages trying to figure out where in the chain something broke.
Below is a fun way to combine the two to make a process work incredibly straightforward. Whenever the function succeeds or needs to progress you can simply set the state to the next enum state. For example: state = states.SLOW_DOWN would begin calling the process_slow_down() function every frame instead of the previous state.

While the above is great, (And should be used within modular states too) we are still looking at the same problem as the Freecode Chaos. All of this is still in uses code in one object. We can use a similar flow as above however to create state classes to push behaviour from individual building block nodes instead.
You can create custom classes for State Manager and States scripts and modular connect behaviours in the Inspector Property Manager. This becomes super powerful as not only can you have an good visual representation of that is going on. You can also reuse states with different properties.


Following the example above… I have a State called Pause, they have a timer that delays the trigger of the following states, and all without having to hard code additional pauses into the other function. I have reused state pause from the example above for for three of these behaviours. (WelcomePause, NewGamePause, GoodbyePause) Note how each timer has a different max!

It is not just that, Win and Lose both use the same State for Adjusting the prize. And Welcome and Goodbye use the same State for displaying messages. When you look at these with when you get to larger game design with the considerations of things like Approach Enemy, Attack, Look for Weapons, Sell Item and so on, It can get incredibly useful for structuring complex behaviour.
For example take a look at what my enemy designs below it can get pretty complex when you have all these extra behaviours all in one script. However now that I can modularly put them all together it can make it incredibly easy to put behaviours into a fluid and easy way to work.

However, I will now point out a flaw with this method of behaviour linking. It can get incredibly convoluted to see the flow for behaviours as more States come into play. Because of this it has led me to develop a function that is attached to the state manager which allows me to create a mermaid flow chart querying the connections between all states.
The non-illustrated flowcharts have been generated via this function. It is quick easy and simple and when generated immediately enters the computers clipboard allowing me to live investigate why and where things have flowed the way they have.