What is the most effective way to create a State Machine in Fusion 2.5?
Let me explain what I mean by a state machine.
I have an enemy. We'll call him Goblin.
Goblin does various things based on conditions, however it's almost quite messy when dealing with dozens of events. I want to know people's personal ways of approaching a system if they were to make Goblin do the following:
Code:
[Entity.alterableValue]
IF (Goblin.distanceFromPlayer) > 500
--Goblin.animation = idle
IF (Goblin.distanceFromPlayer) < 500 AND (Goblin.distanceFromPlayer) > 200
--Goblin.animation = running
--Goblin moves toward player
IF (Goblin.distanceFromPlayer) = 200
--Goblin.animation = rangedAttack
--Goblin launches dagger object
IF (Goblin.distanceFromPlayer) < 200
--Goblin.animation = scared
--Goblin moves away from player
This is a pure example. I have tried many different ways and this seems to be the most effective however it is not without flaws.
Let's take a look at what I am trying to achieve.
If the player is farther than 500px the Goblin stands still.
If the player is closer than 500px but farther than 200px, the Goblin approaches the player.
If the player is exactly 200px away the Goblin will throw a knife, and stop moving.
If the player is closer than 200px the Goblin will run away to protect himself.
The issue I face is that none of these seem to work. The (hypothetical) Goblin would always get mixed up. Sometimes he'd run, other times he'd be clueless as to what he should do.
I desperately need a suggestion of how to better tackle this simple kind of AI, in a way that states are generated out of what the Entity was doing before the last event was triggered, in a nice and clean way that can be applied to large numbers of conditions and variables. Clearly this is ineffective and there are too many loose ends for the AI to become stable.