How to use BehaviorTree node

If you haven’t completed the previous tutorials yet, please complete them starting from Basic Usage.

Starting from this tutorial, we have updated the Logic Toolkit used in the tutorial to version 1.10.0.
If you haven’t updated it yet, please do so before reading further.

This time, let’s use the Behavior Tree node.
The contents are as follows:

  • Create a GameObject with a Logic Behavior component.
  • Open and edit the graph in the Logic Editor window.
  • Execute BehaviorTree nodes with Behavior Tree Execute.
  • Execute child BehaviorTree nodes in order with Sequence.
  • Selectively execute child BehaviorTree nodes with Selector.
  • Determine whether to execute a BehaviorTree node with Decorator.

Create a new scene

If you have other scenes open, it will be confusing, so create a new scene.

  • Select File > New Scene from the menu.
  • In the New Scene window, select Basic (URP).
  • Click the Create button

Creating a GameObject with Logic Behavior

Create a GameObject with a Logic Behavior component added to the Scene.

  • Click the + button in Hierarchy.
  • Select Logic Toolkit > Logic Behavior from the menu.
  • Confirm the name by pressing Enter.

Opening the Logic Editor window

Graphs of Logic Behavior components are edited in the Logic Editor window.
If the window is not displayed, select a GameObject and click the “Edit” button in the Inspector to display it.

  • Select the Logic Behavior object
  • Click the Edit button on the Logic Behavior component in the Inspector window.

If the Logic Editor window is already displayed, the graph will switch in conjunction with selecting a GameObject in the Hierarchy window.

Create a Behavior Tree Execute Task node

To execute a Behavior Tree node, we use a component type called CompositeComponent, which is used to execute BehaviorTree nodes.
Here, we will use Behavior Tree Execute, which executes a single BehaviorTree node.

  • Drag the output execute port of the Start node
  • Drop it near the right side to open the node creation menu
  • Select the Scripts tab
  • Select Tasks > Behavior Trees > Composites > Behavior Tree Execute from the list
  • Confirm the node name with the Enter key

About CompositeComponent

A type of TaskComponent, this component is used to execute child Behavior Tree nodes.
For now, just remember it as “something used to execute a Behavior Tree”.

This time I used it in the Task node, but like other TaskComponents, it can also be used in State nodes, etc.

Create a Sequence Behavior Tree node

Create a Behavior Tree node that uses a CompositeComponent called Sequence.

  • Drag the Execute port of Behavior Tree Execute
  • Drop it near the right side to open the node creation menu
  • Select the Scripts tab
  • Select Behavior Trees > Composites > Sequence from the list
  • Confirm the node name with the Enter key

About Behavior Tree Nodes and TaskComponents

We will now explain the Behavior Tree node and TaskComponent we created.

A Behavior Tree node is a node for controlling behavior known as a Behavior Tree.
The node is executed from the destination of the input port (from here on, for convenience, we will call it the parent node. In this case, it is Behavior Tree Execute), and when the execution is complete, the result is returned to the parent node.
The type of processing to be performed and the success or failure of the execution result are defined by the TaskComponent used.

This time we will use Sequence to further execute the Behavior Tree connected from the Children port.
More details are provided in the section About Sequence below.

Decorators allow you to add components that determine whether or not to execute a node
(we’ll use this later).

Services allow you to add components that perform additional processing while the node is running.
(Not used this time)

About Behavior Trees

A tree structure that judges a situation and determines what action to take is called a Behavior Tree.
In the gaming field, it is mainly used to set up the behavior of NPCs.

For example, the following behavior of enemy NPCs is possible:

  • Selector
    • Selector [Condition: When your HP is below 30%]
      • Run away [Condition: If the player is nearby]
      • Sit (natural recovery)
    • Sequence [Condition: Player is within sight]
      • Get closer
      • Attacking
    • Patrol

In this way, we define the tree structure and decide on actions based on situational judgment.

More information about Sequences and Selectors will be provided later.

About Behavior Tree Wire

The Execute port of Behavior Tree Execute and the input behavior tree port of the Behavior Tree node are connected by a Behavior Tree wire.
This Behavior Tree wire is a wire for executing the Behavior Tree node from the parent port.

In a Behavior Tree wire, the child node will always be a Behavior Tree node.
The order in which the child nodes are executed depends on the type of CompositeComponent on the parent side.

Sequenceについて

Sequence executes the nodes connected to the Children port (hereafter referred to as child nodes) in order from top to bottom.
Depending on the result of executing the child node, behavior such as whether the next child node is executed will change.

Child node execution resultBehavior
SuccessExecutes the next child node.
If there is no next child node, terminates execution and returns success to the parent node.
FailureTerminates execution and returns failure to the parent node.

Remember that a Sequence means that all child nodes will be executed in order unless one of them fails.

Create a Behavior Tree node for Wait For Seconds

Behavior Tree nodes can use components other than CompositeComponent.
Let’s use the familiar Wait For Seconds command to create an action that waits for a few seconds using the Behavior Tree node.

  • Drag the Children port of the Sequence
  • Drop it near the right side to open the node creation menu
  • Select the Scripts tab
  • Enter Wait For Seconds in the search field
  • Select Wait For Seconds (Behavior Tree) from the list
  • Confirm the node name with the Enter key

Because Wait For Seconds returns success upon completion, the parent node Sequence will simply execute the next node.
(The next node to be executed will be created later.)

Wait For Seconds setting

Set it to wait 1 second.

  • Set the Seconds field to 1

Create a Behavior Tree node for the Selector

After waiting the number of seconds specified by Wait For Seconds, the next connection to the Children port of the Sequence will be executed.
Let’s add a CompositeComponent called Selector to the child node of Sequence.

  • Drag the Children port of Sequence
  • Drop it near the bottom of the Wait For Seconds node to open the node creation menu
  • Select the Scripts tab
  • Select Behavior Trees > Composites > Selector from the list
  • Confirm the node name with the Enter key

About Selector

The Selector executes the nodes connected to the Children port (hereafter referred to as child nodes) in order from top to bottom.
Depending on the result of executing the child node, its behavior, such as whether to execute the next child node, changes.

Child node execution resultBehavior
SuccessTerminates execution and returns success to the parent node.
FailureExecutes the next child node.
If there is no next child node, terminates execution and returns failure to the parent node.

Remember that a Selector will execute all child nodes in order unless one of them succeeds.

Create a Behavior Tree node for the Idle Task

Create a Behavior Tree node that uses a no-op Idle Task as a child of the Selector.

  • Drag the Children port of Selector
  • Drop it near the right side to open the node creation menu
  • Select the Scripts tab
  • Enter Idle Task in the search field
  • Select Idle Task (Behavior Tree) from the list
  • Confirm the node name with the Enter key

You may wonder why this Idle Task exists since it doesn’t do anything, but it is particularly useful for Behavior Tree nodes.
In Behavior Trees, the general rule is that the end of a node “performs some kind of action”, but there are times when you want to “do nothing”.

Added Condition Evaluate to Decorators

Let’s use a Decorator to determine whether to execute the Idle Task node.
Here, we use Condition Evaluate as the Decorator.

  • Click the + button on the Decorators of the Behavior Tree node that uses the Idle Task.
  • From the Add Node Component menu, select Evaluators > Condition Evaluate.

About Decorator

Decorator is a component that determines whether a Behavior Tree node is executed or not.
In the case of a Decorator for execution determination, if the result of the determination is true, it will be executed, and if the result is false, it will not be executed and a failure will be returned to the parent node.

Abort Flags

Decorator has a flag called Abort Flags.
This flag allows you to interrupt or halt execution midway.

Setting itemsExplanation
NothingDo not interrupt (a determination is made at the start of execution)
SelfIf the result of the check becomes false while the node is running, the execution will be interrupted and a failure will be returned.
Lower PriorityIf a node with a lower priority than the self-node (a node lower than the self-node) is running and the judgment result becomes true, the self-node will interrupt and start execution.
(Can only be used if the parent is a Selector)

Since it is in flag format, it is possible to combine Self and Lower Priority.

Create another Idle Task Behavior Tree node

The created Idle Task is evaluated by Condition Evaluate to determine whether it should be executed, but because the Condition is false, it is not executed and a failure is returned to the parent Selector.
The Selector that receives the failure will execute the next child node.

Therefore, create another Idle Task Behavior Tree node as the next child node to be executed.

  • Drag the Children port of Selector
  • Drop it near the bottom of the existing Idle Task node to open the node creation menu
  • Select the Scripts tab
  • Enter Idle Task in the search field
  • Select Idle Task (Behavior Tree) from the list
  • Confirm the node name with the Enter key

Play and check

Once you’ve done this, your graph will look like this:

Press the play button to see how it works.

  • Check that the Wait For Seconds command waits for 1 second.
  • Check that the second Idle Task node is running.
  • While playing, change the Abort Flags and Conditions in Condition Evaluate to check the operation.
    • Set Abort Flags to Low Priority
    • Turn on the Condition and confirm that the first Idle Task node is executed.
    • Turn off the Condition and confirm that the first Idle Task node continues to run.
    • Confirm that the second Idle Task node is executed when Abort Flags is set to Self (becoming Everything).

Supplement

Sequence and Selector can be used outside of Behavior Tree nodes

Sequence and Selector are also originally TaskComponents, so like Behavior Tree Execute, they can be used outside of Behavior Tree nodes.
For example, Sequence and Selector can be used directly in State nodes.

TaskComponent execution result (success/failure)

TaskComponent has a built-in ability to return the result of execution, whether success or failure.
When you use TaskComponent in a Behavior Tree node, the result of the execution will be returned to the parent node as the result of the Behavior Tree node.

For example, let’s say you’ve created a TaskComponent script that says,
“Task to move to target to attack: Return success when movement is complete. If the target dies during movement, abort movement and return failure.”
When you use this script in a Behavior Tree node, the result will be the execution result of the Behavior Tree node.
By combining the execution result with a Selector or Sequence, you can create behaviors such as “attack if successful” or “return to patrol and search if unsuccessful.”

Of course, there’s no problem with using Decorator to “determine if the target is alive or not.”
Try thinking about which method is easier to implement for various behaviors.

Next time

Next time, we will do “Hierarchical execution flow using Execute Task“.

If you try the trial version and like it, you can purchase it from the Asset Store.