The fun part
Most game developers online seem to dread when its time to start coding AI for their game. Its understandable, coding AI is tedious and often involves a fair bit of debugging. Not only that but what behavior framework should you use? Should you use one at all?
For the amount of separate AI entities I plan on having for this game I knew I couldn’t leave it as unstructured code in a single script. I wanted all of the player’s army units to inherit from one class and all the enemies to inherit from another class. I’ve used finite state machines in previous projects so that’s where my gut told me to go, but I wanted to try something new. I wanted to try behavior trees.
Godot has a great addon available that works with Godot 4 called Beehave that essentially creates the functionality of a behavior tree for you. That simplicity wouldn’t stop me from being confused on how to get it to work though. After a bit of reading about the code that runs a behavior tree like Beehave and watching a few tutorials on how to implement it in Godot I was eventually able to get a character that would follow the mouse. Ultimately though, I decided that I was too inexperienced with behavior trees and unconfident in the way I set the tree up. Perhaps I’ll come back to Beehave when I design the first boss.
Back to ol’ reliable state machines. State machines’ logic is simple:
- Give entity default state of being.
- Give it the other states it can be in.
- Give it behavior for each state.
- Give it logic to decide when to switch states and which state to switch to.
A bit of paraphrasing but that’s the gist. I enjoy state machines because they allow me to give more specific behaviors to entities that may all share the same base class without having a jumbled mess of if-else statements. For more information on them and how to use them in Godot I recommend this video.
Eventually I had my skeleton moving at the player’s command:

After going back and looking at the code for this unit for the first time in awhile, I now realize that I’ll probably have to move much of it into the state machine as too much of the logic is handled in the base class script…a problem for a future dev log.
Also I made this design for the player character:
