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. Spring Boot 配置详解

    Spring Boot 针对常用的开发场景提供了一系列自动化配置来减少原本复杂而又几乎很少改动的模板配置内容,但是,我们还是需要了解如何在Spring Boot中修改这些自动化的配置,以应对一些特殊场 ...

  2. java集合之List源码解析

    List是java重要的数据结构之一,我们经常接触到的有ArrayList.Vector和LinkedList三种,他们都继承来自java.util.Collection接口,类图如下 接下来,我们对 ...

  3. awk 文本处理工具

    awk: 强大的文本处理工具,擅长对日志文件进行分析: 不仅用于Linux,也是任何环境中现在的功能最强大的数据处理引擎: 语法说明: awk '{pattern + action}' {filena ...

  4. Windows Phone Splash Screen

    Why to use splash screen? Typically, you should use a splash screen in your app only if your app is ...

  5. java容器Container和组件Component之GUI

    GUI全称Graphical User Interfaces,意为图形用户户界面,又称为图形用户接口,GUI指的就是采用图形方式显示的计算机操作用户界面,打个比方吧,我们点击QQ图标,就会弹出一个QQ ...

  6. C++进阶--公有继承的二元性

    //########################################################################### /* * 公有继承的两元性 * * - 接口 ...

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

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

  8. R read.table 一个问题的解决

    Warning message: In scan(file, what, nmax, sep, dec, quote, skip, nlines, na.strings, : EOF within q ...

  9. MyBatis 接口的使用

    为了简化MyBatis的使用MyBatis的使用,MyBatis提供了接口方式自动化生成调用过程,可以大大简化MyBatis的开发 开发映射文件: <select id="queryO ...

  10. 学习笔记之机器学习实战 (Machine Learning in Action)

    机器学习实战 (豆瓣) https://book.douban.com/subject/24703171/ 机器学习是人工智能研究领域中一个极其重要的研究方向,在现今的大数据时代背景下,捕获数据并从中 ...