Game Development Reference
In-Depth Information
Selector evaluation
As selectors only return false if all child nodes have executed without returning true, we
can iterate over all children and return the first positive result we get back. We need to re-
turn two values, the evaluation result as well as an action node if one is found. To do this,
we'll return a table containing both values.
As our behavior tree can be of any arbitrary depth, we will recursively evaluate both select-
ors and sequences till we have a return result:
BehaviorTree.lua :
_EvaluateSelector = function(self, node, deltaTimeInMillis)
-- Try and evaluate all children. Returns the first
child
-- that can execute. If no child can successfully
execute the
-- selector fails.
for index = 1, #node.children_ do
local child = node:GetChild(index);
if (child.type_ == BehaviorTreeNode.Type.ACTION) then
-- Execute all Actions, since Actions cannot
fail.
return { node = child, result = true};
elseif (child.type_ ==
BehaviorTreeNode.Type.CONDITION) then
-- Conditions are only valid within sequences,
if one
-- is encountered in a selector the tree is
malformed.
assert(false);
return { result = false };
elseif (child.type_ ==
BehaviorTreeNode.Type.SELECTOR) then
-- Recursively evaluate child selectors.
Search WWH ::




Custom Search