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. Linux tee命令详解

    Linux tee命令 Linux tee命令用于读取标准输入的数据,并将其内容输出成文件.如果文件指定为"-",则将输入内容复制到标准输出 tee指令会从标准输入设备读取数据,将 ...

  2. redis : 桌面管理工具 redis-desktop-manager使用指南

    概要:一款好用的Redis桌面管理工具,支持命令控制台操作,以及常用,查询key,rename,delete等操作. 下载软件,请点击下面链接,进入下载页,选择对应版本: https://redisd ...

  3. springboot学习心得

    1.mvn package --加载运行一个含有pom.xml的目录并生成target目录2.mvn dependency:tree 显示项目所有依赖的树状结构3.业务委托给了Spring Boot的 ...

  4. 服务发现:Zookeeper vs etcd vs Consul 参考自http://dockone.io/article/667

    服务发现:Zookeeper vs etcd vs Consul [编者的话]本文对比了Zookeeper.etcd和Consul三种服务发现工具,探讨了最佳的服务发现解决方案,仅供参考. 如果使用预 ...

  5. IKAnalyzer 添加扩展词库和自定义词

    原文链接http://blog.csdn.net/whzhaochao/article/details/50130605 IKanalyzer分词器 IK分词器源码位置 http://git.osch ...

  6. QT编写的网页浏览器网页乱码解决方法

    1.如果是本地网页,可以将网页编码改为GB2312 <meta http-equiv="Content-Type" content="text/html; char ...

  7. 【springBoot】之概述

    springboot是什么? springboot不是对spring的增强,而是一个快速使用spring进行开发的框架. 其产生的背景是因为随着动态语言(Scala,Groovy)的流行,Java语言 ...

  8. Mina - 模拟同步请求

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

  9. 【性能测试】使用ab做Http性能测试

    [[TOC]] Http性能测试工具 ab(ApacheBench) 安装: yum install httpd 使用 ab -n 20 -c 1 192.168.35.1:8988/fortest ...

  10. [UE4]修改枪支碰撞体

    一.Simple Collision:显示简单碰撞体,Comlex Collision:显示复杂碰撞体 二.添加简单和复杂碰撞体 三.自动生成复杂精确的碰撞体