In this lesson, you will learn how to build a centralized data provider to keep your AI’s internal world state perfectly mirrored with the game's evolving reality.
The Synchronization Challenge
In a GOAP (Goal-Oriented Action Planning) system, your AI makes decisions based on a "snapshot" of the world stored in its Blackboard. However, the game world is dynamic: doors open, items are picked up, and enemies move. If your planner thinks a door is locked when it has already been opened by another NPC, the AI will fail to generate valid plans. Instead of having every NPC poll the game world individually—which is inefficient—we use a Centralized World State Provider.
Implementing the World State Manager
Think of this manager as a "single source of truth." It acts as a bridge between the game engine's physical state and the abstract conditions used by your planners. It listens for events (like OnItemCollected or OnDoorStateChanged) and updates a shared data structure that your AI planners reference.
public class WorldStateManager : MonoBehaviour
{
// A shared dictionary representing world conditions
public Dictionary<string, bool> WorldState = new Dictionary<string, bool>();
public void UpdateState(string key, bool value)
{
WorldState[key] = value;
// Broadcast the change to all AI agents
EventManager.TriggerEvent("WorldStateUpdated", key);
}
}
By centralizing this logic, you decouple your AI from the complex triggers inside your gameplay objects. When an NPC’s planner needs to decide on a new goal, it doesn't have to check the actual Door object; it queries the WorldState dictionary.
Why Centralization Matters
- Consistency: All agents see the same version of the world simultaneously.
- Performance: You stop dozens of AI agents from performing expensive
Physics.RaycastorFindObjectOfTypecalls every frame. Instead, theWorldStateManageronly updates when a specific game event occurs. - Debugging: Because all state changes pass through one class, you can easily log every transition, making it trivial to find exactly when and why your AI’s perception of the world drifted from reality.
Key Takeaway: A centralized World State Provider ensures your AI acts on a reliable, synchronized map of reality by funneling all game events into a single, queryable data source.