AI agents with FSM: a question regarding this
- by Prog
Finite State Machines implemented with the State design pattern are a common way to design AI agents.
I am familiar with the State design pattern and know how to implement it.
However I have a question regarding how this is used in games to design AI agents.
Please consider a class Monster that represents an AI agent. Simplified it looks like this:
class Monster{
State state;
// other fields omitted
public void update(){ // called every game-loop cycle
state.execute(this);
}
public void setState(State state){
this.state = state;
}
// irrelevant stuff omitted
}
There are several State subclasses that implement execute() differently. So far classic State pattern.
Here's my question:
AI agents are subject to environmental effects and other objects communicating with them.
For example an AI agent might tell another AI agent to attack (i.e. agent.attack()). Or a fireball might tell an AI agent to fall down. This means that the agent must have methods such as attack() and fallDown(), or commonly some message receiving mechanism to understand such messages.
My question is divided to two parts:
1- Please say if this is correct: With an FSM, the current State of the agent should be the one taking care of such method calls - i.e. the agent delegates to the current state upon every event. Correct? Or wrong?
2- If correct, than how is this done? Are all states obligated by their superclass) to implement methods such as attack(), fallDown() etc., so the agent can always delegate to them on almost every event? Or is it done in some other way?