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

例如说,给Sequence节点插入一个不断返回Running的行为节点,那么就会造成后面的子节点无法执行,而对于Parallel来说,是不会存在这种阻塞情况的。

Parallel.cs

 namespace BehaviorDesigner.Runtime.Tasks
{
[TaskDescription("Similar to the sequence task, the parallel task will run each child task until a child task returns failure. " +
"The difference is that the parallel task will run all of its children tasks simultaneously versus running each task one at a time. " +
"Like the sequence class, the parallel task will return success once all of its children tasks have return success. " +
"If one tasks returns failure the parallel task will end all of the child tasks and return failure.")]
[HelpURL("http://www.opsive.com/assets/BehaviorDesigner/documentation.php?id=27")]
[TaskIcon("{SkinColor}ParallelIcon.png")]
public class Parallel : Composite
{
// The index of the child that is currently running or is about to run.
private int currentChildIndex;
// The task status of every child task.
private TaskStatus[] executionStatus; public override void OnAwake()
{
// Create a new task status array that will hold the execution status of all of the children tasks.
executionStatus = new TaskStatus[children.Count];
} public override void OnChildStarted(int childIndex)
{
// One of the children has started to run. Increment the child index and set the current task status of that child to running.
currentChildIndex++;
executionStatus[childIndex] = TaskStatus.Running;
} public override bool CanRunParallelChildren()
{
// This task can run parallel children.
return true;
} public override int CurrentChildIndex()
{
return currentChildIndex;
} public override bool CanExecute()
{
// We can continue executing if we have more children that haven't been started yet.
return currentChildIndex < children.Count;
} public override void OnChildExecuted(int childIndex, TaskStatus childStatus)
{
// One of the children has finished running. Set the task status.
executionStatus[childIndex] = childStatus;
} public override TaskStatus OverrideStatus(TaskStatus status)
{
// Assume all of the children have finished executing. Loop through the execution status of every child and check to see if any tasks are currently running
// or failed. If a task is still running then all of the children are not done executing and the parallel task should continue to return a task status of running.
// If a task failed then return failure. The Behavior Manager will stop all of the children tasks. If no child task is running or has failed then the parallel
// task succeeded and it will return success.
bool childrenComplete = true;
for (int i = ; i < executionStatus.Length; ++i) {
if (executionStatus[i] == TaskStatus.Running) {
childrenComplete = false;
} else if (executionStatus[i] == TaskStatus.Failure) {
return TaskStatus.Failure;
}
}
return (childrenComplete ? TaskStatus.Success : TaskStatus.Running);
} public override void OnConditionalAbort(int childIndex)
{
// Start from the beginning on an abort
currentChildIndex = ;
for (int i = ; i < executionStatus.Length; ++i) {
executionStatus[i] = TaskStatus.Inactive;
}
} public override void OnEnd()
{
// Reset the execution status and the child index back to their starting values.
for (int i = ; i < executionStatus.Length; ++i) {
executionStatus[i] = TaskStatus.Inactive;
}
currentChildIndex = ;
}
}
}

BTParallel.lua

 BTParallel = BTComposite:New();

 local this = BTParallel;
this.name = "BTParallel"; function this:New()
local o = {};
setmetatable(o, self);
self.__index = self;
o.childTasks = {};
o.executionStatus = {};
return o;
end function this:OnUpdate()
if (not self:HasChild()) then
return BTTaskStatus.Failure;
end for i=,#self.childTasks do
local childTask = self.childTasks[i];
if (not self.executionStatus[i]) then --第一次执行
self.executionStatus[i] = childTask:OnUpdate();
if (self.executionStatus[i] == BTTaskStatus.Failure) then
return BTTaskStatus.Failure;
end
elseif (self.executionStatus[i] == BTTaskStatus.Running) then --第二次以及以后执行
self.executionStatus[i] = childTask:OnUpdate();
if (self.executionStatus[i] == BTTaskStatus.Failure) then
return BTTaskStatus.Failure;
end
end
end local childrenComplete = true;
for i=,#self.executionStatus do
if (self.executionStatus[i] == BTTaskStatus.Running) then
childrenComplete = false;
break;
end
end
if (childrenComplete) then
return BTTaskStatus.Success;
else
return BTTaskStatus.Running;
end
end

测试如下:

1.BTSequence和BTParallel的对比

BTSequence:

 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 sequence = BTSequence:New();
local action = self:GetBTActionUniversal();
local log = BTLog:New("This is log!!!");
log.name = "log"; self:SetStartTask(sequence); sequence:AddChild(action);
sequence:AddChild(log);
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

BTParallel:

 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 log = BTLog:New("This is log!!!");
log.name = "log"; self:SetStartTask(parallel); parallel:AddChild(action);
parallel:AddChild(log);
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

2.BTParallel中返回失败中断执行的情况

 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.Failure;
end);
return universal;
end

[Unity插件]Lua行为树(十一):组合节点Parallel的更多相关文章

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

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

  2. [Unity插件]Lua行为树(十三):装饰节点完善

    之前介绍了组合节点中三大常用的节点:BTSequence.BTSelector和BTParallel,一般来说,这三种就够用了,可以满足很多的需求. 接下来可以完善一下装饰节点,增加几种新的节点. 1 ...

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

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

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

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

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

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

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

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

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

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

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

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

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

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

随机推荐

  1. 100M双绞线接头的标准接法

     双绞线接头(RJ45)针脚号码定义

  2. GetClass与RegisterClass的应用一例

    利用GetClass与RegisterClass可以实现根据字符串来实例化具体的子类,这对于某些需要动态配置程序的场合是很有用的.其他的应用如子窗体切换,算法替换等都能得到应用. unit Examp ...

  3. Weka训练模型的存取

    因为WEKA中所有分类器都实现了Serializable,所以只需要用java的ObjectOutputStream就可以实现了. /** * 存储model * * @param model * 训 ...

  4. Ubuntu PPA软件源

    PPA,其英文全称为 Personal Package Archives,即个人软件包档案.是 Ubuntu Launchpad 网站提供的一项源服务,允许个人用户上传软件源代码,通过 Launchp ...

  5. Azure ARM (15) 根据现有VHD文件,创建ARM VM

    <Windows Azure Platform 系列文章目录> 在很多时候,我们需要根据现有VHD文件,创建ARM VM.在这里笔者简单介绍一下相关的Azure PowerShell 这里 ...

  6. git命令的简单使用

    Gitbash初始化设置 Gitbash安装成功后要配置email和name,否则commit的时候会报错: 运行 git config --global user.email "你的ema ...

  7. linux找不到动态链接库 .so文件的解决方法

    linux找不到动态链接库 .so文件的解决方法 如果使用自己手动生成的动态链接库.so文件,但是这个.so文件,没有加入库文件搜索路劲中,程序运行时可能会出现找不到动态链接库的情形. 可以通过ldd ...

  8. Mina - 模拟同步请求

    这篇博客主要就铺代码吧,Mina的一些基础知识可以参考: http://www.cnblogs.com/huangfox/p/3458272.html 场景假设: 1.客户端发送用户信息,服务端根据用 ...

  9. PyQt4 对多个按钮进行同样的外观设置

    实现的效果: 正常状态下:黑底(背景色),白字(前景色),圆角,向外凸起: 鼠标停留:背景和前景都反色: 鼠标按下:背景色变为淡蓝色,向内凹陷:      class MyStyleSheet: @s ...

  10. PyQt—QTableWidget中的checkBox状态判断

    一.QTableWidget实现checkBox效果 利用QTableWidgetItem对象的CheckState属性,既能显示QCheckBox,又能读取状态 table = QtGui.QTab ...