Behaviour · const

FAILURE

A behaviour tree that is data, so it can be looked at, paused and stepped.

Explained in Agents and behaviour.

const FAILURE = 0
import { FAILURE } from '@driftengine/core';

In depth

The complaint this answers is not "a routine cannot be written". It can, and a consumer reported theirs: a table of places and a loop that decides where to send everybody once an in-game hour. It works. What they also reported is what it is — a hand-written behaviour tree that cannot be inspected, paused or stepped — and where it gets expensive, which is the moment a routine needs interrupting: an agent dropping what it is doing because it started raining, and picking it up again afterwards.

So the two things here are the two things a hand-rolled loop does not give you.

The tree is data. Nodes are arrays of integers and names resolved once at build time; the consumer's own functions live in a table beside it. A running tree can therefore say which node it is in, by name, without anything having been instrumented — activePath walks it — and a paused character can be advanced one node at a time.

A preempted branch is suspended, not reset, which is the whole of "picking it up again". A selector that switches to a higher-priority child leaves the lower one's cursors exactly where they were, so when the interruption ends the agent resumes at the step it had reached rather than at the start of its errand. State is reset only when a branch finishes — succeeds or fails — because that is when starting again is what starting again means. Every behaviour-tree implementation has to choose here, most reset on abort, and the difference is a villager who walks back to the shop after a shower against one who walks home and starts the day over.

Nothing allocates during a tick and nothing reads a clock. A tick is a function of the tree, the character's own cursors, and whatever the consumer's context object holds.