BehaviorTree

The file “behaviortree.lua” contains the behaviour tree class and the nodes used with it. It is strongly related to brains, as every brain has a behaviour tree.

The behaviour tree has a root node, which acts as the “stem” of the tree. Usually a priority node is used for that.

The nodes can have four states: “READY”,”RUNNING”,”FAILED” and “SUCCESS”. Ready means that the brain will try (visit) that node (when it gets to it, of course). Running means the brain waits for the node to finish. Failed means the brain carries on to the next node, and success means the brain stops there.

A node can have a name and child nodes. It is possible for nodes to sleep and interact with their parents or children. However, all of that is optional, it depends on the kind of node whether it’s used.

The following basic nodes exist:

DecoratorNode

Constructor arguments: name, child

This is a plain behaviour node that can have only one child, nothing more than decoration.

NotDecorator

Constructor arguments: name, child

This node fails if the child succeeds and vica versa. It’s essentially like a “not” in Lua.

FailIfRunningDecorator

Constructor arguments: name, child

This node is almost like the DecoratorNode, except that if the child is running, the node fails.

ConditionNode

Constructor arguments: fn, name

This note runs “fn” when visited, and only if the function returns true the node has success. Else, it fails.

ConditionWaitNode

Const. arg.: fn, name

The same as previous node, but rather than failing, it runs until the function returns true.

ActionNode

Const. arg.: action, name

This node is similiar to the ConditionNode, except that after running the function “action”, it will always be successful.

WaitNode

Const. arg.: time

This node sleeps for “time” seconds whenever it gets visited, and upon waking, it has success. It’s essentially a delay node.

SequenceNode

Const. arg.: children

This node goes through the table “children” in the order they’re added and visits them. Only when all children are successful the SequenceNode is successful

SelectorNode

Const. arg.: children

This node is similiar to SequenceNode, but has success as soon as a child node has success. Only if none of the children succeed, this node fails.

LoopNode

Const. arg.: children, maxreps

This is a sequenceNode that doesn’t immediately succeed, but only if its children succeed “maxreps” times. This effectively means that the same sequence is run several times rather than once.

RandomNode

Const. arg.: children

This is a weird one. It picks a random child and only if that child fails, it picks the next one. Only if all children fail, the RandomNode fails.

PriorityNode

Const. arg.: children, period

This is usually the root node of every behaviour tree.

The node has a period that must have passed before it does anything again. When visited and the period has passed, it goes through the children in order.  If a child has success or is running, the node takes that state and only resets the remain children.

ParallelNode

Const. arg.: children, name

This node runs all children simultaneously, and succeeds if all stopped running without failing.

ParallelNodeAny

This is a parallelNode that succeeds when any child stops running without failing.

EventNode

Const. arg.: inst, event, child, priority

This node attaches an event listener for “event” to the instance “inst”. If that event fires, the node triggers and wakes the parent priority node. Upon getting visited then, the child gets visited.

WhileNode

Const. arg.: cond, name, node

This node is literally just a parallel node with a condition node in it. If the function “cond” returns true, the behaviour tree stops at the parallel node and lets the other node run.

IfNode

Const. arg.: cond, name, node

This node is literally just a sequence node with a condition node in it. If the function “cond” returns true, the node continues to the other node.