Finite state machine for an enemy, for a godot beginner

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By Hurtolin

I am very new using godot, it is the first engine that I am dedicating time to. I am currently working on my first game, which is a simple stealth game. I have my player, but now I need to program the enemy. I know that I need to make a finite state machine, so that the enemy can patrol, chase the player, attack him and return to patrol. The problem is that the tutorials that I find are a bit vague and do not explain 100% the steps that must be carried out, from the enemy script, to the states and the respective machine, could someone help me with that? considering I’m very new to this. Sorry for the bad English.

your question is too broad

Ando | 2022-08-04 08:43

Hey, since you mentioned you’re a beginner, I would recommend you not getting too deep into academical stuff for now, all those fancy computer science patterns can cause more harm than benefit if used without enough confidence.
Think about it this way: a state machine - is a mechanism where you have some state variable, usually an enum, with values like: idle, patrol, attack, etc. And some logic of how these states change from one to another, like:

if (state == EnemyState.patrol) && playerDetected {
  state = EnemyState.attack

Maybe one important thing to mention here is that e.g. stopAttack - is a bad example of state, because it’s basically not a state, but a momentary action, so you should avoid defining states like that.
The exact implementation of this logic, as well as its complexity, is up to you. You don’t need any rules from smart articles to restrict you from defining your state machine the way you want.
And after successfully implementing your own state machine you will have enough experience to start looking at those articles and see which fancy ideas you could borrow from them to make your code even better.
Good luck!
And sorry if this is not exactly an answer you wanted to hear :slight_smile:

max.marauder | 2022-08-05 12:07