Menu

BHTreeBuilder

Si Dunford (Scaremonger)

Creating a Behavior Tree

A behavior tree can be created in code or by loading a textual definition file. The latter has the advantage that no code needs to be changed in order to adjust the size or properties of the tree and is very useful when creating and testing your tree.

In Code

'# Create the root node (Must be a control)
Global root:Control = New Selector

'# Create the tree
Global tree:BHTree = New BHTree.Create( root )

'# Add children to the root node.
root.addchild( New Movement )
root.addchild( New ChooseDestination )

'# Dump the tree for debug purposes
tree.dump()

Using the Behavior Builder
The code to build a tree is very simple.

Global tree:BHTree = New BHTree.Load( definition$ )
If Not tree Then End

The definition can be defined in your code, loaded from an external file or INC-BIN'd into your application for example:

'# Definition as a string
local definition$ = "Selector( ShootEnemy() Movement() )"

'# Load a Definition from disk
local definition$ = LoadString("sampleTree.def")

'# Load a Definition using incbin
Incbin "sampleTree.def"
local definition$ = LoadString("incbin::sampleTree.def")

The BHTree.Load() returns NULL if there are any errors in your definition; to debug it turn on debugging and the parser will dump the errors into the compiler output screen.

Definition File Syntax

Each Node is defined by it's classname, nodename, parameters and children like this:

    CLASSNAME [NODENAME] {PARAMETERS} (CHILDREN)

Classname is any Blitzmax Type descended from Control or Trait.

Definition Parameter Description Example
CLASSNAME Required Any valid Control or Trait Type Selector
NODENAME Optional Name of the node within square brackets "[root]"
PARAMETERS Optional Comma separated values passed to the Type.set() method. "{param1,param2}
CHILDREN Optional Children of the node "()"

For example, a root node (Of type Selector) with two children (A Movement Type and a ShootEnemy Type) may be defined like this:

Selector(
    ShootEnemy()
    Movement()
)

MongoDB Logo MongoDB