Repeater:重复执行子节点,直到一定次数

特点如下:

1.执行次数可以是无限循环,也可以是固定次数

2.一般来说,子节点的执行返回状态不会影响Repeater节点,但可以设置当子节点返回失败时,结束执行Repeater节点

Repeater的继承关系如下:

Repeater->Decorator->ParentTask->Task

因为继承装饰节点,所以其子节点只能有一个

Repeater.cs

 namespace BehaviorDesigner.Runtime.Tasks
{
[TaskDescription(@"The repeater task will repeat execution of its child task until the child task has been run a specified number of times. " +
"It has the option of continuing to execute the child task even if the child task returns a failure.")]
[HelpURL("http://www.opsive.com/assets/BehaviorDesigner/documentation.php?id=37")]
[TaskIcon("{SkinColor}RepeaterIcon.png")]
public class Repeater : Decorator
{
[Tooltip("The number of times to repeat the execution of its child task")]
public SharedInt count = ;
[Tooltip("Allows the repeater to repeat forever")]
public SharedBool repeatForever;
[Tooltip("Should the task return if the child task returns a failure")]
public SharedBool endOnFailure; // The number of times the child task has been run.
private int executionCount = ;
// The status of the child after it has finished running.
private TaskStatus executionStatus = TaskStatus.Inactive; public override bool CanExecute()
{
// Continue executing until we've reached the count or the child task returned failure and we should stop on a failure.
return (repeatForever.Value || executionCount < count.Value) && (!endOnFailure.Value || (endOnFailure.Value && executionStatus != TaskStatus.Failure));
} public override void OnChildExecuted(TaskStatus childStatus)
{
// The child task has finished execution. Increase the execution count and update the execution status.
executionCount++;
executionStatus = childStatus;
} public override void OnEnd()
{
// Reset the variables back to their starting values.
executionCount = ;
executionStatus = TaskStatus.Inactive;
} public override void OnReset()
{
// Reset the public properties back to their original values.
count = ;
endOnFailure = true;
}
}
}

BTDecorator.lua

 BTDecorator = BTParentTask:New();

 local this = BTDecorator;

 function this:New()
local o = {};
setmetatable(o, self);
self.__index = self;
return o;
end function this:GetChild()
if (self:GetChildCount() ~= ) then
return nil;
end
return self.childTasks[];
end

BTRepeater.lua

 BTRepeater = BTDecorator:New();

 local this = BTRepeater;

 function this:New(count, endOnFailure)
local o = {};
setmetatable(o, self);
self.__index = self;
o.executionStatus = BTTaskStatus.Inactive;
o.count = count or -; --执行次数,-1为循环执行
o.endOnFailure = endOnFailure or false; --子节点返回Failure时,是否结束
o.executionCount = ; --当前执行次数
return o;
end function this:OnUpdate()
if (not self.currentChildTask) then
self.currentChildTask = self:GetChild();
if (not self.currentChildTask) then
return BTTaskStatus.Failure;
end
end local canExecute = false;
if (((self.count == -) or (self.executionCount < self.count)) and
((not self.endOnFailure) or (self.endOnFailure and self.executionStatus ~= BTTaskStatus.Failure))) then
canExecute = true;
end if (canExecute) then
self.executionStatus = self.currentChildTask:OnUpdate();
self.executionCount = self.executionCount + ;
end if (self.endOnFailure and self.executionStatus == BTTaskStatus.Failure) then
self:Reset();
return BTTaskStatus.Success;
elseif (self.executionCount == self.count) then
self:Reset();
return BTTaskStatus.Success;
else
return BTTaskStatus.Running;
end
end function this:Reset()
self.executionStatus = BTTaskStatus.Inactive;
self.executionCount = ;
BTParentTask.Reset(self);
end

测试:

 TestBehaviorTree = BTBehaviorTree:New();

 local this = TestBehaviorTree;

 function this:New()
local o = {};
setmetatable(o, self);
self.__index = self;
this:Init();
return o;
end function this:Init()
--1.一定的执行次数
local repeater = BTRepeater:New();
local log = BTLog:New("This is a BTRepeater test");
repeater:AddChild(log); --2.循环执行
--[[local repeater = BTRepeater:New();
local log = BTLog:New("This is a BTRepeater test");
repeater:AddChild(log);--]] --3.endOnFailure false
--[[local repeater = BTRepeater:New(3);
local sequence = BTSequence:New();
local log = BTLog:New("First Child");
local isNullOrEmpty = BTIsNullOrEmpty:New("123");
local log2 = BTLog:New("This is a empty string");
sequence:AddChild(log);
sequence:AddChild(isNullOrEmpty);
sequence:AddChild(log2);
repeater:AddChild(sequence);--]] --4.endOnFailure true
--[[local repeater = BTRepeater:New(3, true);
local sequence = BTSequence:New();
local log = BTLog:New("First Child");
local isNullOrEmpty = BTIsNullOrEmpty:New("123");
local log2 = BTLog:New("This is a empty string");
sequence:AddChild(log);
sequence:AddChild(isNullOrEmpty);
sequence:AddChild(log2);
repeater:AddChild(sequence);--]] this:PushTask(repeater);
end

第一个输出:

第二个输出(循环执行):

第三个输出:

第四个输出:

[Unity插件]Lua行为树(五):装饰节点Repeater的更多相关文章

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

随机推荐

  1. RedHat6.5上安装Hadoop单机

    版本号:RedHat6.5   JDK1.8   Hadoop2.7.3 hadoop  说明:从版本2开始加入了Yarn这个资源管理器,Yarn并不需要单独安装.只要在机器上安装了JDK就可以直接安 ...

  2. R基于Bayes理论实现中文人员特性的性别判定

    参见 基于中文人员特征的性别判定方法  理论,告诉一个名字,来猜猜是男是女,多多少少有点算命的味道.此命题是一种有监督的学习方法,从标注好的训练数据学习到一个预测模型,然后对未标注的数据进行预测. 1 ...

  3. 字符串CRC校验

    字符串CRC校验 <pre name="code" class="python"><span style="font-family: ...

  4. 模拟a标签click,弹出新页面

    $("<a>").attr("href", url).attr("target", "_blank")[0] ...

  5. win7下python2.6如何安装setuptools和pip

    1. 下载 setuptools-0.6c9.tar.gz 下载地址:http://pypi.python.org/packages/source/s/setuptools/setuptools-0. ...

  6. java设计模式-Iterator

    Iterator模式 主要是用在容器的遍历上,其他的地方都不怎么用:理解一下,会用了就可以了:   1.背景 请动手自己写一个可以动态添加对象的容器: 代码: ArrayList.java(是自己实现 ...

  7. 峰Redis学习(6)Redis 数据结构(sorted-set的操作)

    第六节:Redis 数据结构之sorted-set 类型 存储Sorted-Set Sorted-Set和Set的区别   Sorted-Set中的成员在集合中的位置是有序的   存储Sorted-s ...

  8. 信息安全-加密:AES 加密

    ylbtech-信息安全-加密:AES 加密 高级加密标准(英语:Advanced Encryption Standard,缩写:AES),在密码学中又称Rijndael加密法,是美国联邦政府采用的一 ...

  9. Java-Runoob-高级教程-实例-方法:04. Java 实例 – 斐波那契数列

    ylbtech-Java-Runoob-高级教程-实例-方法:04. Java 实例 – 斐波那契数列 1.返回顶部 1. Java 实例 - 斐波那契数列  Java 实例 斐波那契数列指的是这样一 ...

  10. 学习笔记之GenFu

    Everybody was GenFu Fighting - GenFu http://genfu.io/ GenFu is a test and prototype data generation ...