Tasks
At the highest level a behavior tree is a collection of tasks. Tasks have a similar API to Unity’s MonoBehaviour so it should be really easy to get started writing your own tasks. The task class has the following API:
// OnAwake is called once when the behavior tree is enabled. Think of it as a constructor. void OnAwake(); // OnStart is called immediately before execution. It is used to setup any variables that need to be reset from the previous run. void OnStart(); // OnUpdate runs the actual task. TaskStatus OnUpdate(); // OnFixedUpdate executes during the FixedUpdate loop. The TaskStatus must be returned within OnUpdate. void OnFixedUpdate(); // OnEnd is called after execution on a success or failure. void OnEnd(); // OnPause is called when the behavior is paused or resumed. void OnPause(bool paused); // Returns the priority of the task, used by the Priority Selector. float GetPriority(); // Returns the utility of the task, used by the Utility Selector for Utility Theory. float GetUtility(); // OnBehaviorComplete is called after the behavior tree finishes executing. void OnBehaviorComplete(); // OnReset is called by the inspector to reset the public properties void OnReset(); // Allow OnDrawGizmos to be called from the tasks. void OnDrawGizmos(); // Keep a reference to the behavior that owns this task. Behavior Owner;
Tasks have three exposed properties: name, comment, and instant. Instant is the only property that isn’t obvious in what it does. When a task returns success or fail it immediately moves onto the next task within the same update tick. If you uncheck the instant task it will now wait a update tick before the next task gets executed. This is an easy way to throttle the behavior tree.
The following flow chart is used when executing the task: