之前运行的行为树,都是一颗总树,那么实际上会有很多的总树,因此需要对行为树进行管理。

BTBehaviorManager.lua

 BTBehaviorManager = {};

 local this = BTBehaviorManager;
this.isEachFrameCall = false;--是否是每帧调用
this.printTreeStr = "";
this.trees = {}; function this.RunTree()
if (isEachFrameCall) then
else
while (true) do
local isAllFinish = this.OnUpdate();
if (isAllFinish) then
break;
end
end
end
end function this.OnUpdate()
local count = ; for i=,#this.trees do
local tree = this.trees[i];
if (tree.executionStatus == BTTaskStatus.Inactive) then --第一次执行
print("第一次执行:" .. tree.name);
tree:OnUpdate();
elseif (tree.executionStatus == BTTaskStatus.Running) then --第二次以及以后执行
print("第二次以及以后执行:" .. tree.name);
tree:OnUpdate();
else
count = count + ;
end
end if (count == #this.trees) then
return true;
else
return false;
end
end --添加行为树
function this.AddTree(task)
table.insert(this.trees, task);
end --深度优先,打印树结构
function this.PrintTree(task)
this.printTreeStr = "";
this.AddToPrintTreeStr(task);
print(this.printTreeStr);
end function this.AddToPrintTreeStr(task)
local taskType = task.taskType; this.printTreeStr = this.printTreeStr .. task:ToString() .. "\n"; if (taskType == BTTaskType.Root) then
this.AddToPrintTreeStr(task.startTask);
elseif (taskType == BTTaskType.Composite or taskType == BTTaskType.Decorator) then
for i=,#task.childTasks do
this.AddToPrintTreeStr(task.childTasks[i]);
end
else end
end

说明:

1.因为是运行在Sublime环境下的,所以这里使用while循环模拟每帧调用

2.关于AddTree和OnUpdate是否会冲突的问题。AddTree会增加trees的长度,但是在lua中for的三个表达式在循环开始前一次性求值,以后不再进行求值。比如#this.trees这个表达式只会在循环开始前执行一次,其结果用在后面的循环中。也就是说,如果在for循环过程中执行了AddTree方法,新增的元素会添加在后面,不会影响for循环的。

测试:

1.多行为树执行

TestBehaviorTree.lua

 TestBehaviorTree = BTBehaviorTree:New();

 local this = TestBehaviorTree;
this.name = "TestBehaviorTree"; function this:New()
local o = {};
setmetatable(o, self);
self.__index = self;
o:Init();
return o;
end function this:Init()
local parallel = BTParallel:New();
local action = self:GetBTActionUniversal();
local action2 = self:GetBTActionUniversal2(); self:SetStartTask(parallel); parallel:AddChild(action);
parallel:AddChild(action2);
end function this:GetBTActionUniversal()
local count = ;
local a = function ()
if (count <= ) then
count = count + ;
print("");
return BTTaskStatus.Running;
else
return BTTaskStatus.Success;
end
end
local universal = BTActionUniversal:New(nil, a);
return universal;
end function this:GetBTActionUniversal2()
local universal = BTActionUniversal:New(nil, function ()
return BTTaskStatus.Success;
end);
return universal;
end

TestBehaviorTree2.lua

 TestBehaviorTree2 = BTBehaviorTree:New();

 local this = TestBehaviorTree2;
this.name = "TestBehaviorTree2"; function this:New()
local o = {};
setmetatable(o, self);
self.__index = self;
o:Init();
return o;
end function this:Init()
local repeater = BTRepeater:New();
local sequence = BTSequence:New();
local log = BTLog:New("This is a other tree!!!");
local log2 = BTLog:New("This is a other tree 2!!!"); self:SetStartTask(repeater); repeater:AddChild(sequence); sequence:AddChild(log);
sequence:AddChild(log2);
end

TestMain.lua

 require "BehaviorTree/Core/Init"
require "BehaviorTree/Test/TestBehaviorTree"
require "BehaviorTree/Test/TestBehaviorTree2" local tree = TestBehaviorTree:New();
local tree2 = TestBehaviorTree2:New();
tree.name = "tree";
tree2.name = "tree2";
BTBehaviorManager.AddTree(tree);
BTBehaviorManager.AddTree(tree2);
BTBehaviorManager.RunTree();

输出如下:

2.for循环中添加行为树

TestBehaviorTree3.lua

 TestBehaviorTree3 = BTBehaviorTree:New();

 local this = TestBehaviorTree3;
this.name = "TestBehaviorTree3"; function this:New()
local o = {};
setmetatable(o, self);
self.__index = self;
o:Init();
return o;
end function this:Init()
local repeater = BTRepeater:New();
local log = BTLog:New("This is TestBehaviorTree3 tree!!!"); self:SetStartTask(repeater); repeater:AddChild(log);
end

BTBehaviorManager.lua

 BTBehaviorManager = {};

 local this = BTBehaviorManager;
this.isEachFrameCall = false;--是否是每帧调用
this.printTreeStr = "";
this.trees = {}; function this.RunTree()
if (isEachFrameCall) then
else
while (true) do
local isAllFinish = this.OnUpdate();
if (isAllFinish) then
break;
end
end
end
end local temp = false; function this.OnUpdate()
local count = ; for i=,#this.trees do
if (not temp) then
temp = true;
require "BehaviorTree/Test/TestBehaviorTree3"
local tree3 = TestBehaviorTree3:New();
tree3.name = "tree3";
this.AddTree(tree3);
end local tree = this.trees[i];
if (tree.executionStatus == BTTaskStatus.Inactive) then --第一次执行
print("第一次执行:" .. tree.name);
tree:OnUpdate();
elseif (tree.executionStatus == BTTaskStatus.Running) then --第二次以及以后执行
print("第二次以及以后执行:" .. tree.name);
tree:OnUpdate();
else
count = count + ;
end
end print("------------------------------"); if (count == #this.trees) then
return true;
else
return false;
end
end --添加行为树
function this.AddTree(task)
table.insert(this.trees, task);
end --深度优先,打印树结构
function this.PrintTree(task)
this.printTreeStr = "";
this.AddToPrintTreeStr(task);
print(this.printTreeStr);
end function this.AddToPrintTreeStr(task)
local taskType = task.taskType; this.printTreeStr = this.printTreeStr .. task:ToString() .. "\n"; if (taskType == BTTaskType.Root) then
this.AddToPrintTreeStr(task.startTask);
elseif (taskType == BTTaskType.Composite or taskType == BTTaskType.Decorator) then
for i=,#task.childTasks do
this.AddToPrintTreeStr(task.childTasks[i]);
end
else end
end

输出如下。可以看到,在第一帧时,添加了行为树tree3,但是并不影响tree和tree2的执行,而在第二帧以及以后的帧时,tree3也开始执行了。

[Unity插件]Lua行为树(十二):行为树管理的更多相关文章

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

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

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

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

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

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

  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行为树(六):打印树结构

    经过前面的文章,已经把行为树中的四种基本类型节点介绍了下.接下来可以整理一下,打印一下整棵行为树.注意点如下: 1.可以把BTBehaviorTree也当作一种节点,这样就可以方便地进行行为树嵌套了 ...

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

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

  8. [Unity插件]Lua行为树(十一):组合节点Parallel

    Parallel节点类似Sequence节点,不同在于Parallel会每帧执行所有的节点.当所有节点返回成功时返回成功,当其中一个节点返回失败时,返回失败并且结束所有的子节点运行. 例如说,给Seq ...

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

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

随机推荐

  1. 单机RedHat6.5+JDK1.8+Hadoop2.7.3+Spark2.1.1+zookeeper3.4.6+kafka2.11+flume1.6环境搭建步骤

    1.RHEL 6.5系统安装配置图解教程(rhel-server-6.5) 2.在Linux下安装JDK图文解析 3.RedHat6.5上安装Hadoop单机 4.RedHat6.5安装Spark单机 ...

  2. mwan3多wan叠加成功

    参考: http://www.right.com.cn/forum/forum.php?mod=viewthread&tid=147109&highlight=mwan3 wan口为路 ...

  3. phpmailer使用qq邮箱、163邮箱成功发送邮件实例代码

    以前使用qq邮箱.163服务器发送邮件,帐号直接使用密码,现在不行了,得使用授权码,简单记录下 1.首先开通POP3/SMTP服务,qq邮箱——帐号——设置,找到POP3/SMTP点开启,输入短信会有 ...

  4. 转:使用 Go-Ethereum 1.7.2搭建以太坊私有链

    使用 Go-Ethereum 1.7.2搭建以太坊私有链 目录 [toc] 1.什么是Ethereum(以太坊) 以太坊(Ethereum)并不是一个机构,而是一款能够在区块链上实现智能合约.开源的底 ...

  5. Gradle详细解析***

    前言 对于Android工程师来说编译/打包等问题立即就成痛点了.一个APP有多个版本,Release版.Debug版.Test版.甚至针对不同APP Store都有不同的版本.在以前ROM的环境下, ...

  6. MSSQL 2008 密钥

    sql server2008 r2 密钥 Developer: PTTFM-X467G-P7RH2-3Q6CG-4DMYBEnterprise: JD8Y6-HQG69-P9H84-XDTPG-34M ...

  7. googletest--测试控制

    有时候如果某个测试出现了异常,但是我们想继续其他的测试怎么办. 最简单的方法就是,在测试的名字前加上"DISABLED_",如下面的例子所示: // Test with fixtu ...

  8. 【异常处理】Springboot对Controller层方法进行统一异常处理

    Controller层方法,进行统一异常处理 提供两种不同的方案,如下: 方案1:使用 @@ControllerAdvice (或@RestControllerAdvice), @ExceptionH ...

  9. firefox驱动的下载地址

    https://www.seleniumhq.org/download/

  10. hadoop长时间运行后,stop-all.sh报错

    报错现象: hadoop在stop-all.sh的时候依据的是datanode上的mapred和dfs进程号. 而默认的进程号保存在/tmp下,linux默认会每 隔一段时间(一般是一个月或者7天左右 ...