# FSM AI examples

## Recommended Posts

rpiller    839

Are there any code examples of a FSM for AI? I know it would be specific to whatever engine probably but I would love to see some code to get an idea on how to implement this for the engine I'm using. I've used FSM before for higher level states like (Logo, MainMenu, Options, Gameplay), but I've never used FSM for lower level stuff. It always seemed like getting access to required data from within states and between states was a challenge.

##### Share on other sites
ApochPiQ    22999

The problems you cite are exactly why FSMs are not the favored approach for much of the game AI world. There are ways to mitigate this problem, such as hierarchical FSMs, blackboards, and so on, but generally there are much better architectures to choose from.

##### Share on other sites
rpiller    839

I've looked at the Behavior design before and it seemed nice, but it seemed like it would have the same data access issue. Any other that has some working code I could look at?

##### Share on other sites
rpiller    839

I find stuff like the following: https://github.com/headchant/bhtlua/blob/master/aibht.lua which is great, but the implementations are always so simple and basic and don't try to access any actual game data. Again, I realize this would be specific to the game, but I would just want to see how someone does it. For example, the 'makeThiefFlee' action would actually need a way to tell the thief to flee. So it needs some mechanism to do that. What thief? What if there are 5 loaded thiefs in the scene?

Looking at the below on the first sequence. If the/a thief is near the treasure (well what treasure object? this condition would need to know about this treasure object so it could do some calcs to see if a thief is near it. I could do a bounding box check and find entities inside it and see if they are thiefs but the idea that this can be attached to many guards means they need to know about the treasure they are guarding which means these functions need to know about it.

One would also think that if isThiefNearTreasure returned true that if would have access to the thief object and would then pass it to the makeThiefFlee action because that actions needs to know what thief to make flee. This still very much seems like it has data access issues. What are the common ways to expose all the needed data (and sometimes specific configurable data (like what treasure to guard in this case))?

local simpleBehaviour = Selector{
Sequence{
isThiefNearTreasure,
makeThiefFlee,
},
Sequence{
chooseCastle,
flyToCastle,
fightAndEatGuards,
packStuffAndGoHome

},
Sequence{
}
}