CORE AI SYSTEM
Planning & Execution Loop
The agent calculates a plan when it has no current action, validates the next action's preconditions, and updates execution until the action completes.
private void Update()
{
if (!isActive) return;
// Get a new plan if we don't have an action
if (currentAction == null)
{
CalculatePlan();
if (actionPlan != null &&
actionPlan.Actions.Count > 0)
{
navMesh.ResetPath();
currentGoal = actionPlan.AgentGoal;
currentAction = actionPlan.Actions.Pop();
if (currentAction.Preconditions.All(
b => b.Evaluate()))
{
currentAction.Start();
}
else
{
currentAction = null;
currentGoal = null;
}
}
}
// Execute current action
if (actionPlan != null && currentAction != null)
{
currentAction.Update(Time.deltaTime);
if (currentAction.Complete)
{
currentAction.Stop();
currentAction = null;
if (actionPlan.Actions.Count == 0)
{
currentGoal = null;
}
}
}
}
Goal
Create a flexible AI architecture that allows an agent to pursue goals through sequences of actions, rather than relying exclusively on hard-coded behavior sequences.
Implementation
The update loop requests a plan when no action is active, retrieves the next action, and evaluates its preconditions before starting execution. While an action is active, it receives frame updates until it reports completion.
Once complete, the agent stops the action and clears its reference so the next action can be processed. The current goal is also cleared when the plan has no remaining actions.
Key Technical Decisions
- Plan calculation when no action is active
- Precondition evaluation before execution
- Lifecycle methods for action start and stop
- Frame-based action updates
- Goal and action state tracking
Result
The system separates planning from action execution, making the agent's behavior easier to organize and extend. New goals and actions can be integrated into the architecture without placing every behavior inside a single monolithic update function.