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

Architecting State Representations for Planning

Define world states using key-value pairs to represent the environment and NPC status for use in planning algorithms.

In this lesson, you will learn how to abstract your environment and NPC status into key-value pairs, creating a structured "world state" that planning algorithms can process to make complex decisions.

From Behaviors to Planning

Up until now, you have used Behavior Trees to define how an NPC performs a sequence of actions. However, planning systems (like GOAP - Goal-Oriented Action Planning) work differently: they don’t just follow a tree; they search for a path of actions that transform the "current world state" into a "desired goal state."

To make this possible, you must move away from hardcoded nodes and represent your game world as a collection of bool, int, or enum facts.

The World State as Key-Value Pairs

Instead of asking "Can I see the player?", a planner looks at a Dictionary<string, object> that represents the world. Each key is a state variable, and the value is its current status.

public class WorldState
{
    // Dictionary representing the current facts of the world
    public Dictionary<string, bool> States = new Dictionary<string, bool>();

    public void SetState(string key, bool value)
    {
        if (States.ContainsKey(key)) States[key] = value;
        else States.Add(key, value);
    }
}

By defining your world this way, you allow the planner to perform "what-if" simulations. For example, if an NPC wants to reach the state {"HasWeapon": true}, the planner examines all available actions. It looks for actions whose Effects match that state and checks if their Preconditions are currently met in your WorldState dictionary.

Structuring for Success

When architecting these states, keep your keys semantic and concise.

  • Boolean Facts: {"IsHungry": true}, {"HasKey": false}
  • Numerical/State Facts: {"AmmoCount": 5}, {"Health": 80}

The goal is to decouple the NPC’s internal logic from the game world. By querying these key-value pairs, your AI can treat the game environment as a set of logical "slots" to be filled, allowing the planner to dynamically construct a sequence of actions—such as "Pick up Key" followed by "Unlock Door"—simply by evaluating which state changes are needed to reach the target goal.

Key Takeaway: Architecting world states as a collection of key-value pairs allows your AI to treat the game environment as a logical puzzle, enabling planners to dynamically calculate action sequences rather than following pre-authored trees.