Course contentsShow
← Unity Game Development
Lesson 549 of 8288. Artificial IntelligenceFree lesson

Defining Atomic Actions with Preconditions and Effects

Construct individual actions by specifying the requirements to start them and the resulting changes to the world state.

In this lesson, you will learn how to encapsulate AI behavior into "Atomic Actions" by explicitly defining the world state requirements needed to trigger them and the logical changes they produce once completed.

The Concept of Atomic Actions

In complex AI systems, an "Atomic Action" is a singular, indivisible unit of work. Rather than writing long scripts that check for every possible scenario, we decompose AI logic into discrete tasks—like PickUpItem or OpenDoor. Each action carries two vital pieces of metadata: Preconditions and Effects.

  • Preconditions: A checklist of world states that must be true before the action can begin (e.g., "Is the player in range?" or "Does the agent have an empty hand?").
  • Effects: The resulting changes to the Blackboard or game state once the action succeeds (e.g., "Hand is now full" or "Door is now open").

Implementation Structure

By modularizing these, your AI planner doesn't need to understand the how of the task; it only needs to know when it is valid to execute and what the outcome will be.

Consider this simplified structure for an action node:

public abstract class AtomicAction : ScriptableObject {
    // Requirements to start
    public abstract bool CheckPreconditions(Blackboard blackboard);
    
    // Logic execution
    public abstract void PerformAction(GameObject agent);
    
    // State changes post-execution
    public abstract void ApplyEffects(Blackboard blackboard);
}

By defining an action this way, you create a contract. For an EatFood action, the Precondition verifies HasFood == true, and the Effect sets HungerLevel = 0 and HasFood = false.

Why This Matters

When your AI system treats behaviors as atomic blocks, you stop relying on complex if/else ladders. Instead, you create a "Planning" environment where the AI can look at the current world state, identify the gap between the current state and the goal state, and select the specific AtomicAction whose Effects bridge that gap. This keeps your code clean, modular, and—most importantly—easy to debug, as each action is self-contained and predictable.

Key Takeaway: By binding every action to specific Preconditions and Effects, you transform your AI from a rigid script into a flexible system that treats behavior as a series of state-driven transitions.