Hello everyone!
I need help understanding the concept of patterns for the PLAYER and AI.
I am currently working on a controller for the player. At the moment it is just a script with if and else statements. Since this is a prototype and it is really starting to get bigger, it’s time to use the so-called “programming patterns”. And here a big question arose - do I even understand correctly what we are talking about:
For Player, one of the best options (subject to complex multifaceted mechanics) would be a Finite State Machine. Of course, there is also a Hierarchical State Machine, which I equate to the FSM for now. Do I understand correctly that there is either a giant sheet of code (without the FSM) or a State Machine? Does the Behavior Tree only apply to AI? Do I understand correctly?
For AI, it is necessary to use either a State Machine or a Behavior Tree. The choice depends on the complexity of the AI, that it is even possible to make a Tree with “leaves” from the FSM?
As a result, the player gets one pattern (FSM), the AI gets another pattern (Behavior Tree)?
Yes, the state machine breaks up your code so that different animations and behaviours can be applied to different states. If I am jumping, say, I cannot be digging at the same time. So the code and associated mechanics and animations (as well as possible behaviours like double jump) are encoded for each state.
For AI, I use both. The enemy state machine tells me if the enemy is orbiting, chasing, patrolling or retreating. The enemy behaviour manager tells me how the enemy behaves in different situations. So if the enemy is patrolling when it sees the player it begins chasing the player. If the enemy is chasing the player and the player gets too far away, the enemy goes back to patrolling. The state the enemy is in is controlled by the behaviour manager. Different enemies have different behaviours as well as states. A civilian enemy will only attack me if I attack it. A military enemy will attack me on sight. etc. etc.
Every actor with a set of behaviours requires a state machine to control which state it is in. The player does not need a behaviour manager because that is the players job, to decide what behaviour they want to do. The other actors or NPC’s require a behaviour manager to say what they are going to do next in certain situations.
Sometimes people just put the behaviour hardcoded into the state machine. So for instance, a child of the state machine called “attacking” might hard code that when the player is out of range, change to “patrolling”. That is ok for simple AI’s. I prefer to use a behaviour manager, so the state would call the behaviour manager such as attack_target_lost(). The behaviour manager would then say to change state to patrolling. Or for a different NPC it might say to hang around last seen location, or for another it might say return to home base. By not hard coding it in the states, I can re-use the states, even for NPC’s that might not have a patrolling state.
The advantage of this is I can share states between all my NPC’s. So I can add the attacking state, the patrolling state, and the orbiting states to anything. The behaviour manager, which is unique for each NPC type, I can then hand craft to reflect the behaviour of that type of character, without touching the states. (This works especially well with a separate movement manager, so states modify movement as opposed to rewriting everything for each state).
However, this is something you will just have to try out. There is no ‘correct’ way to do this and everyone has their own favourite approach which works for them best.
I hope that helps in some way.
PS for example here is the tree for an enemy player.