Must all AI states be able to react to any event?
- by Prog
FSMs 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.
How is this used in games to design AI agents?
Consider a simplified class Monster, representing an AI agent:
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 implementing execute() differently. So far, classic State pattern.
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.
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. Is this correct?
If correct, 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?