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

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

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

以下面这棵行为树为例:

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行为树(六):打印树结构的更多相关文章

  1. [Unity插件]Lua行为树(二):树结构

    参考链接:https://blog.csdn.net/u012740992/article/details/79366251 在行为树中,有四种最基本的节点,其继承结构如下: Action->T ...

  2. [Unity插件]Lua行为树(十二):行为树管理

    之前运行的行为树,都是一颗总树,那么实际上会有很多的总树,因此需要对行为树进行管理. BTBehaviorManager.lua BTBehaviorManager = {}; local this ...

  3. [Unity插件]Lua行为树(七):行为树嵌套

    在上一篇的基础上,可以测试下行为树的嵌套,所谓的行为树嵌套,就是在一棵行为树下的某一个分支,接入另一棵行为树. 以下面这棵行为树为例: TestBehaviorTree2.lua TestBehavi ...

  4. [Unity插件]Lua行为树(一):BehaviorDesigner源码分析

    BehaviorDesigner是Unity上的一款行为树插件,不过这个插件是用C#编写的,编写出来的行为树也是依赖于C#的,不利于热更,所以有必要写一个lua版本的. 首先下载BehaviorDes ...

  5. [Unity插件]Lua行为树(三):组合节点Sequence

    Sequence的继承关系如下: Sequence->Composite->ParentTask->Task 上一篇已经实现了简单版本的ParentTask和Task(基于Behav ...

  6. [Unity插件]Lua行为树(十):通用行为和通用条件节点

    在行为树中,需要扩展的主要是行为节点和条件节点.一般来说,每当要创建一个节点时,就要新建一个节点文件.而对于一些简单的行为节点和条件节点,为了去掉新建文件的过程,可以写一个通用版本的行为节点和条件节点 ...

  7. [Unity插件]Lua行为树(九):条件节点调整

    先看一下之前的条件节点是怎么设计的: BTConditional.lua BTConditional = BTTask:New(); local this = BTConditional; this. ...

  8. [Unity插件]Lua行为树(八):行为节点扩展

    先看一下之前的行为节点是怎么设计的: BTAction.lua BTAction = BTTask:New(); local this = BTAction; this.taskType = BTTa ...

  9. [Unity插件]Lua行为树(四):条件节点和行为节点

    条件节点和行为节点,这两种节点本身的设计比较简单,项目中编写行为树节点一般就是扩展这两种节点,而Decorator和Composite节点只需要使用内置的就足够了. 它们的继承关系如下: Condit ...

随机推荐

  1. [转]Ubuntu python-config

    转自:http://manpages.ubuntu.com/manpages/precise/man1/python-config.1.html recise (1) python-config.1. ...

  2. Ribbon Ping机制

    在负载均衡器中,提供了 Ping 机制,每隔一段时间,会去 Ping 服务器,判断服务器是否存活,该工作由 com.netflix.loadbalancer.IPing 接口的实现类负责,如果单独使用 ...

  3. Fabric Engine2.0的自定义节点功能

    Fabric Engine是一个多用途的引擎,针对maya等软件写节点写功能很方便.尤其是canvas节点编辑面板,提供了大量现有的功能供用户调用,当然这些节点功能都是可被用户编辑修改的,除此之外还提 ...

  4. Maven Gradle 区别

    Maven面临的挑战 软件行业新旧交替的速度之快往往令人咂舌,不用多少时间,你就会发现曾经大红大紫的技术已经成为了昨日黄花,当然,Maven也不会例外.虽然目前它基本上是Java构建的事实标准,但我们 ...

  5. Azure SQL 数据库仓库Data Warehouse (1) 入门

    <Windows Azure Platform 系列文章目录> 在之前的项目中遇到了客户使用SQL数据仓库的场景,在这里记录一下 1.什么是SQL 数据库仓库 (SQL DW) SQL D ...

  6. php访问SQLserver时加载的dll

    php_sqlsrv_55_ts.dll 线程t安全sphp_sqlsrv_55_nts.dll 非n线程t安全s

  7. tomcat源码 StandardService

    在执行StandardServer的initInternal的时候会执行StandardService#init,然后会调到initInternal protected void startInter ...

  8. Linux Docker安装Jenkins

    安装环境 操作系统 centos7.3 安装Docker,安装及配置见 <Docker之Docker介绍及安装配置> 安装Jenkins 下载Jenkins 命令:sudo docker ...

  9. ALGO-13_蓝桥杯_算法训练_拦截导弹(贪心,DP)

    问题描述 某国为了防御敌国的导弹袭击,发展出一种导弹拦截系统.但是这种导弹拦截系统有一个缺陷:虽然它的第一发炮弹能够到达任意的高度,但是以后每一发炮弹都不能高于前一发的高度.某天,雷达捕捉到敌国的导弹 ...

  10. 机器学习笔记——t分布知识点总结

    (原创文章,转载请注明地址:http://www.cnblogs.com/wangkundentisy/p/6539058.html ) 1.t分布式统计分布的一种,同卡方分布(χ2分布).F分布并称 ...