O'Reilly logo

Learning Game AI Programming with Lua by David Young

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Creating a behavior tree object

Creating a behavior tree object is simple, as it primarily consists of a root node, evaluation function, and update function:

BehaviorTree.lua:

require "BehaviorTreeNode"

BehaviorTree = {};

local _EvaluateSelector;
local _EvaluateSequence;

function BehaviorTree.SetNode(self, node)
    tree.node_ = node;
end

function BehaviorTree.new()
    local tree = {};
    
    -- The BehaviorTree's data members.
    tree.currentNode_ = nil;
    tree.node_ = nil;
    
    return tree;
end

Behavior tree helper functions

Four primary evaluators are used to process the behavior tree structure: selector evaluation, sequence evaluation, actual node evaluation, and finally, a continue evaluation function that continues where a sequence's child finishes:

BehaviorTree.lua ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required