经过前面的文章,已经把行为树中的四种基本类型节点介绍了下。接下来可以整理一下,打印一下整棵行为树。注意点如下:

1.可以把BTBehaviorTree也当作一种节点,这样就可以方便地进行行为树嵌套了

2.可以在BTBehaviorTree中的SetStartTask、BTParentTask中的AddChild,即在设置节点和添加节点时对节点的信息进行打印

以下面这棵行为树为例:

[Unity插件]Lua行为树(六):打印树结构-LMLPHP

BTTask.lua

 BTTask = {};

 local this = BTTask;

 function this:New()
local o = {};
setmetatable(o, self);
self.__index = self;
o.executionStatus = BTTaskStatus.Inactive; --该节点的执行状态
o.root = nil; --根节点
o.parent = nil; --父节点
o.layer = ; --第几层
o.index = ; --父节点下的第几个节点
o.name = "BTTask"; --该节点名字
return o;
end function this:ToString()
local root = "root:";
if (self.root) then
root = root .. self.root.name;
else
root = root .. "nil";
end local parent = "parent:";
if (self.parent) then
parent = parent .. self.parent.name;
else
parent = parent .. "nil";
end local layer = "layer:" .. self.layer;
local index = "index:" .. self.index;
local name = "name:" .. self.name; print(root .. " " .. parent .. " " .. layer .. " " .. index .. " " .. name);
end

BTParentTask.lua

 BTParentTask = BTTask:New();

 local this = BTParentTask;

 function this:New()
local o = {};
setmetatable(o, self);
self.__index = self;
o.currentChildIndex = ; --当前运行到第几个子节点
o.currentChildTask = nil; --当前运行的子节点
o.childTasks = {}; --子节点列表
return o;
end --添加子节点
function this:AddChild(task)
local index = #self.childTasks + ;
task.index = index;
task.layer = self.layer + ;
task.parent = self;
task.root = self.root;
self.childTasks[index] = task; if (BTBehaviorManager.isPrintTaskInfo) then
print(task:ToString());
end
end --是否有子节点
function this:HasChild()
if (#self.childTasks > ) then
return true;
else
return false;
end
end --获取子节点数目
function this:GetChildCount()
return #self.childTasks;
end --获取下一个要执行的子节点
function this:GetNextChild()
if (#self.childTasks >= (self.currentChildIndex + )) then
self.currentChildIndex = self.currentChildIndex + ;
return self.childTasks[self.currentChildIndex];
end
return nil;
end --重置
function this:Reset()
self.currentChildIndex = ;
self.currentChildTask = nil;
end

BTBehaviorTree.lua

 --[[
树的根节点
--]]
BTBehaviorTree = BTTask:New(); local this = BTBehaviorTree; function this:New()
local o = {};
setmetatable(o, self);
self.__index = self;
return o;
end function this:SetStartTask(task)
task.root = self;
task.parent = self;
task.layer = self.layer + ;
self.startTask = task; if (BTBehaviorManager.isPrintTaskInfo) then
print(task:ToString());
end
end function this:OnUpdate()
if (self.startTask) then
return self.startTask:OnUpdate();
end
end

TestBehaviorTree.lua

 TestBehaviorTree = BTBehaviorTree:New();

 local this = TestBehaviorTree;
this.name = "TestBehaviorTree"; function this:New()
local o = {};
setmetatable(o, self);
self.__index = self;
self:Init();
return o;
end function this:Init()
local repeater = BTRepeater:New();
local selector = BTSelector:New();
local sequence = BTSequence:New();
local isNullOrEmpty = BTIsNullOrEmpty:New("");
local log = BTLog:New("This is a empty string!!!");
local log2 = BTLog:New("This is not a empty string"); self:SetStartTask(repeater); repeater:AddChild(selector); selector:AddChild(sequence);
selector:AddChild(log2); sequence:AddChild(isNullOrEmpty);
sequence:AddChild(log);
end

打印如下:

[Unity插件]Lua行为树(六):打印树结构-LMLPHP

05-02 05:44