Get started with Behavior Trees
The first thing that you will need is the base node that all of the other nodes will derive from.
NodeBase
is part of Behavior Tree Visualizer. It provides access to the NodeStatus
enum which you will use for the return status codes. It also has other useful properties that you will use later.
You should now have this:
using WUG.BehaviorTreeVisualizer;
public abstract class Node : NodeBase
{
}
Node
needs to have the ability to do two things - Run the logic and Reset the logic. There is a set of base things that will happen for every single node, which will occur in Run
and Reset
. OnRun
and OnReset
will be overrode by each derived node and will contain the custom logic.
Add the following code to Node
:
//Keeps track of the number of times the node has been evaluated in a single 'run'.
public int EvaluationCount;
// Runs the logic for the node
public virtual NodeStatus Run()
{
//Runs the 'custom' logic
NodeStatus nodeStatus = OnRun();
//Increments the tracker for how many times the node has been evaluated this 'run'
EvaluationCount++;
// If the nodeStatus is not Running, then it is Success or Failure and can be Reset
if (nodeStatus != NodeStatus.Running)
{
Reset();
}
//Return the StatusResult.
return nodeStatus;
}
public void Reset()
{
EvaluationCount = 0;
OnReset();
}
protected abstract NodeStatus OnRun();
protected abstract void OnReset();
That is it for the base class! Now it is time to create some composites.