A squad soldier's behavior tree keeps finishing a reload while a grenade lands beside him; how do you make the tree interrupt the running reload?
answer
- who re-checks the grenade guard
- priority order versus memory
- observers on a blackboard key
- self versus lower-priority abort
- the reload's halt hook
basics
~20 sPlace the grenade branch above the reload and make sure something re-checks its guard while the reload runs: a reactive root re-walked each tick, or, in an event-driven tree, a lower-priority abort observing the grenade key.
solid answer
~50 sFirst find who is supposed to re-check "grenade nearby?" while the reload is running. In a tree **re-walked from the root each tick**, the grenade branch must sit left of the reload under a **reactive** selector; a root selector **with memory** resumes the running reload branch without re-trying the grenade branch. In an **event-driven** tree, which ticks only the running nodes and re-evaluates on events, the grenade guard needs a **lower-priority abort**: it observes the `grenadeNearby` blackboard key and, when the key becomes true, halts the running lower-priority branch and restarts execution at its own branch. A **self abort** is the opposite case: a guard that stops its own running branch when its condition stops holding. Then check the chain end to end: perception actually writes the key, and the reload's **halt hook** cancels the animation and leaves the magazine count consistent.
go deeper
Recall that a higher-priority branch should interrupt a lower one, and that a running action must be told to stop when it loses control.
Explain the difference between re-walking from the root each tick and event-driven evaluation, and why composites with memory skip earlier guards.
Diagnose the missed interrupt along the chain: key written, guard re-checked, abort mode right, halt honoured; and pick self or lower-priority aborts deliberately.
Set rules for where aborts may sit and which keys may drive them, since unbounded high-priority aborts on noisy keys make a squad's behaviour thrash and hard to debug.
## Why the reload is not interrupted The soldier's tree has the right idea: a root selector with "escape grenade" ahead of "reload". The symptom says that, while the reload is running, **nothing asks the grenade question again**, or something asks it and gets the wrong answer, or the answer arrives but the reload ignores it. Which of those it is depends on how the tree is evaluated, so start there. ## Trees re-walked from the root each tick If the tree is ticked from the root every time with **memoryless** (reactive) composites, priority does the work by itself: - On every tick the root selector tries the grenade branch first. When its guard becomes true, that branch runs, the traversal never reaches the reload, and the tree halts the reload as a preempted action. - The usual way this breaks is a composite **with memory** above both branches. A root selector with memory resumes its running child, the reload branch, without re-trying the branches to its left. Memory lower down, inside the reload branch, does not cause this, because the root still tries the grenade branch first. - The other way it breaks is order: the grenade branch sits to the right of the reload, so it is lower priority and is only tried when reloading fails. ## Event-driven trees: observers and aborts Re-walking a large tree for many agents every tick is wasteful, so many engines make trees **event-driven**: they keep the currently running node or nodes, tick only those, and re-evaluate only when something happens, such as a child finishing or a blackboard key changing. In such a tree, the grenade guard is never re-evaluated while the reload runs unless it has asked to be. It asks by **observing** a blackboard key and declaring an **abort mode**: | abort mode | the guard watches while | when its result changes it | |---|---|---| | none | nothing | does nothing until the tree reaches it normally | | self | its own branch is running | halts its own branch when the condition stops holding | | lower priority | a branch to its right is running | halts that branch and restarts execution at the guard | | both | either of the above | does whichever applies | For the grenade, the guard needs **lower priority**: when `grenadeNearby` becomes true, the running reload branch to its right is halted and the grenade branch starts. A **self** abort fits a different need, such as a "chase target" branch guarded by "target visible?" that should stop the moment sight is lost. ## Diagnosing it Work along the chain from the event to the animation: 1. **Is the fact written?** Log the blackboard. If `grenadeNearby` never becomes true, the bug is in perception or in the code that should write the key, not in the tree. 2. **Does anything re-check it?** In a per-tick tree, look for memory on the composites above the reload and for the branch order. In an event-driven tree, look for the abort mode on the grenade guard and for an observer on the right key. 3. **Does the abort reach the reload?** A trace of each tick should show the reload being halted on the tick the key changed. 4. **Does the reload honour the halt?** A halt hook that does not cancel the animation leaves the soldier visibly reloading while the tree has moved on. Decide too what an interrupted reload does to the magazine count, so a resumed reload does not add rounds twice. ## Costs and traps - **Noisy keys thrash.** A lower-priority abort on a key that flickers restarts branches every time it flips. Smooth the fact where it is produced, or give it a minimum hold time. - **Aborts are side effects.** Every abort runs halt hooks, so actions must be written to be interrupted at any tick. - **Abort chains surprise designers.** A guard with "both" near the root can interrupt almost anything; keep high-priority aborts few and visible. - **Order still matters.** A lower-priority abort only interrupts branches to its right, so an emergency branch that sits low in the order cannot preempt the branches above it. The same tree may need to stop the soldier from flipping back and forth between two branches; that is the hysteresis problem familiar from state machines, and the cure is the same kind of minimum hold time.
- When would you use a self abort rather than a lower-priority abort?Use a self abort when a branch's own reason for running can disappear mid-way, such as chasing a target that drops out of sight: the guard watches its own branch and halts it when the condition fails. Use a lower-priority abort when a more urgent branch must take over from whatever lower-priority work is running, such as escaping a grenade.
- Why do event-driven trees need aborts when per-tick trees do not?A per-tick tree re-walks from the root, so a reactive selector re-checks higher-priority guards every time. An event-driven tree keeps ticking only the running node and re-evaluates on events, so no guard is re-checked unless it observes a key and declares how its change should abort running work.
- What should a reload action's halt hook do?Stop the reload animation and sound, release anything the action reserved, reset its progress so a later start is clean, and apply one rule for partial progress, either no rounds added or rounds added per completed step, so an interrupted and restarted reload never refills twice.
- Putting the grenade branch first in the selector is always enough to interrupt a running reload.
- A self abort halts lower-priority branches when its condition becomes true.
- In any behavior tree, a guard is re-checked automatically while the action below it runs.
- An aborted action cleans up by itself because it is no longer ticked.
- Adding aborts to every guard is free and makes an NPC more responsive.