skip to content

A GOAP NPC walking to fetch a key finds the player has taken it; how should it detect the broken plan, replan, and keep replanning affordable across fifty NPCs?

level: seniorimportance: should knowfreq 50%

answer

  1. plans are predictions
  2. validate each step against now
  3. replan on relevant change only
  4. branching factor to the depth
  5. queue searches, budget per frame

basics

~20 s

Check the next action's preconditions, including procedural ones, before and while running it, and replan when one fails or the goal changes. Keep it affordable by replanning only on relevant changes, capping search size and spreading requests across frames.

solid answer

~50 s

A plan is a prediction made from the facts at planning time, so the executor **validates** each step: its symbolic preconditions, its procedural checks (is the key still there and reachable?) and whether the goal is still the best one. A failed check invalidates the plan, and the planner runs again from the current state, here finding `KickDoor`, `EnterRoom`, `AttackTarget`. Scale is the problem: a search grows roughly as `b^d` for `b` relevant actions per step and plan depth `d`, and one explosion can make fifty NPCs replan in the same frame. Remedies: replan on events that touch facts the plan depends on rather than every tick, keep each archetype's action set small, cap depth or expanded nodes, queue requests and run a fixed number per frame, and let waiting agents keep a sensible current action.

go deeper

for a junior

Recall that a GOAP plan is built from the facts known at planning time, so the world can change underneath it and each step must be checked first.

for a middle

Explain the executor loop: validate the current action, run it, advance on success, and request a new plan on failure or when the goal changes.

for a senior

Quantify replanning cost from branching factor and depth, and show the controls: event-driven triggers, depth or node caps, a per-frame planning queue and goal-switch margins.

for a principal

Decide how much planning the game can afford per frame and per agent type, and where a cheaper reactive layer should handle fast reactions instead of the planner.

## Why GOAP plans go stale A **GOAP plan** is a sequence of actions computed from the facts the NPC believed at planning time. The world keeps moving: the player takes the key the NPC was walking to, an ally barricades the door, the target leaves the room. Nothing in the plan notices. The **executor**, the code that runs the plan one step at a time, has to. ## Detecting a broken plan - **Validate before and during each action.** Before starting `PickUpKey`, check its preconditions against the current world state, including **procedural** ones such as "the key still exists and is reachable". Many actions also re-check while running, because the key can vanish halfway through the walk. - **Watch the facts the plan depends on.** The remaining steps' preconditions form a dependency set; when perception or game events change one of those facts, mark the plan suspect. - **Re-rank goals.** A new threat can make a different goal, such as taking cover, more urgent than the one being pursued. - **Treat action failure as invalidation.** A step can fail on its own, for example when the route to the key is blocked. ## Replanning 1. Wind the current action down at a safe point; an NPC halfway through a vault should land first. 2. Re-run goal selection, since the best goal may have changed. 3. Plan from the current world state. With the key gone, `PickUpKey`'s procedural check fails, and the planner returns `KickDoor`, `EnterRoom`, `AttackTarget`. 4. Swap in the new plan. If none exists, fall back to the next goal; every agent needs at least one goal it can always satisfy, such as holding position, so it never freezes. ## What replanning costs Planning is a search. With `b` relevant actions to choose from at each step and a plan `d` actions deep, the worst case expands on the order of `b^d` nodes. With `b = 4` and `d = 5` that is `4^5 = 1,024` nodes for one search, which is small for one NPC. The trouble is correlation: one explosion changes facts for fifty NPCs at once, and `50 x 1,024 = 51,200` expansions can land in one frame, each copying fact sets and perhaps running procedural checks such as path queries. A good heuristic and duplicate-state detection cut the real numbers, but the spike comes from synchronised demand, not from any single search. ## Keeping it affordable - **Replan on events, not on ticks.** Trigger planning when a fact in the dependency set changes, the current action fails or goal selection picks a new goal. - **Small action sets per archetype.** A guard with eight actions branches far less than one with forty. - **Cap the search.** Limit plan depth or expanded nodes, and treat hitting the cap as "no plan". - **Queue and budget.** Put planning requests in a queue and run a fixed number of searches, or a fixed time slice, per frame. Waiting agents keep doing something sensible, such as holding position or finishing the current action. - **Cheap procedural checks.** Cache results such as reachability for the duration of one search instead of recomputing them at every node. ## Validating the rest of the plan Checking only the next step finds some breaks late. Suppose the door is barricaded while the NPC is still fetching the key: the next step, `PickUpKey`, is fine, and the NPC only discovers the problem at the door. A cheap extension simulates the remaining steps from the current world state, applying each action's effects in turn and testing every later action's preconditions, plus its inexpensive procedural checks. If a later step can no longer succeed, the plan is invalid now, and the NPC replans before wasting the walk. ## Failure modes to recognise | Symptom | Likely cause | Fix | |---|---|---| | NPC walks to a key that is gone | Preconditions checked only at planning time | Validate each step before and during execution | | NPC stutters between two plans | Goals swapping priority every few frames | Change goals only when the rival wins by a margin | | Frame spike after a big event | Many agents replanning at once | Queue requests with a per-frame budget | | NPC freezes | No plan and no fallback goal | Keep an always-achievable fallback goal | The common thread is that replanning is not free and not optional: skip validation and agents act on stale beliefs; replan without limits and the frame budget pays for every change in the world at once.

  • What is plan thrashing in GOAP, and how do you stop it?
    Two goals or plans whose relevance swaps every few frames, so the agent replans constantly and never finishes an action. Stop it by switching goals only when a new goal's priority beats the current one by a margin, letting the current action reach a safe point before abandoning it, and rate-limiting replans per agent.
  • Why not simply re-run the GOAP planner every tick for every NPC?
    Planning cost grows roughly with the branching factor raised to the plan depth, and procedural checks run inside the search. Doing it for every agent every tick spends the frame recomputing plans that have not changed. Event-driven triggers, search caps and a per-frame request queue make the cost follow what actually changed.

  • Once a GOAP plan is found the NPC can run it to the end unchecked.
  • Replanning every tick is cheap enough to be the default.
  • Planning cost grows linearly with the length of the plan.
  • An NPC must stand still until its new plan is ready.
  • A failed plan means the goal is impossible, so the NPC should give up.