[Unity插件]Lua行为树(八):行为节点扩展
先看一下之前的行为节点是怎么设计的:
BTAction.lua
BTAction = BTTask:New(); local this = BTAction;
this.taskType = BTTaskType.Action; function this:New()
local o = {};
setmetatable(o, self);
self.__index = self;
return o;
end
BTLog.lua
--[[
参考BehaviorDesigner-Action-Log
--]]
BTLog = BTAction:New(); local this = BTLog;
this.name = "BTLog"; function this:New(text)
local o = {};
setmetatable(o, self);
self.__index = self;
o.text = text;
return o;
end function this:OnUpdate()
print(self.text);
return BTTaskStatus.Success;
end
由上可见,行为节点的具体逻辑都是放在OnUpdate中的,那么问题来了,如果想在OnUpdate前执行一段逻辑,OnUpdate后也执行一段逻辑,类似于状态机那样,那么就需要对行为节点进行扩展。
BTAction.lua
BTAction = BTTask:New(); local this = BTAction;
this.taskType = BTTaskType.Action;
this.isEnter = false; function this:New()
local o = {};
setmetatable(o, self);
self.__index = self;
return o;
end function this:OnUpdate()
local status;
if (not self.isEnter) then
self.isEnter = true;
self:Enter();
end
if (self.isEnter) then
status = self:Execute();
if (status ~= BTTaskStatus.Running) then
self:Exit();
self.isEnter = false;
end
end
return status;
end function this:Enter()
print("BTAction:Enter");
end function this:Execute()
print("BTAction:Execute");
return BTTaskStatus.Success;
end function this:Exit()
print("BTAction:Exit");
end
BTLog.lua
--[[
参考BehaviorDesigner-Action-Log
--]]
BTLog = BTAction:New(); local this = BTLog;
this.name = "BTLog"; function this:New(text)
local o = {};
setmetatable(o, self);
self.__index = self;
o.text = text;
return o;
end function this:Enter()
print(self.name .. ":Enter");
end function this:Execute()
print(self.text);
return BTTaskStatus.Success;
end function this:Exit()
print(self.name .. ":Exit");
end
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 sequence = BTSequence:New();
local log = BTLog:New("This is log!!!");
local log2 = BTLog:New("This is log2!!!");
log.name = "log";
log2.name = "log2"; self:SetStartTask(sequence); sequence:AddChild(log);
sequence:AddChild(log2);
end
打印如下:

[Unity插件]Lua行为树(八):行为节点扩展的更多相关文章
- [Unity插件]Lua行为树(七):行为树嵌套
在上一篇的基础上,可以测试下行为树的嵌套,所谓的行为树嵌套,就是在一棵行为树下的某一个分支,接入另一棵行为树. 以下面这棵行为树为例: TestBehaviorTree2.lua TestBehavi ...
- [Unity插件]Lua行为树(六):打印树结构
经过前面的文章,已经把行为树中的四种基本类型节点介绍了下.接下来可以整理一下,打印一下整棵行为树.注意点如下: 1.可以把BTBehaviorTree也当作一种节点,这样就可以方便地进行行为树嵌套了 ...
- [Unity插件]Lua行为树(二):树结构
参考链接:https://blog.csdn.net/u012740992/article/details/79366251 在行为树中,有四种最基本的节点,其继承结构如下: Action->T ...
- [Unity插件]Lua行为树(四):条件节点和行为节点
条件节点和行为节点,这两种节点本身的设计比较简单,项目中编写行为树节点一般就是扩展这两种节点,而Decorator和Composite节点只需要使用内置的就足够了. 它们的继承关系如下: Condit ...
- [Unity插件]Lua行为树(三):组合节点Sequence
Sequence的继承关系如下: Sequence->Composite->ParentTask->Task 上一篇已经实现了简单版本的ParentTask和Task(基于Behav ...
- [Unity插件]Lua行为树(十一):组合节点Parallel
Parallel节点类似Sequence节点,不同在于Parallel会每帧执行所有的节点.当所有节点返回成功时返回成功,当其中一个节点返回失败时,返回失败并且结束所有的子节点运行. 例如说,给Seq ...
- [Unity插件]Lua行为树(十):通用行为和通用条件节点
在行为树中,需要扩展的主要是行为节点和条件节点.一般来说,每当要创建一个节点时,就要新建一个节点文件.而对于一些简单的行为节点和条件节点,为了去掉新建文件的过程,可以写一个通用版本的行为节点和条件节点 ...
- [Unity插件]Lua行为树(九):条件节点调整
先看一下之前的条件节点是怎么设计的: BTConditional.lua BTConditional = BTTask:New(); local this = BTConditional; this. ...
- [Unity插件]Lua行为树(五):装饰节点Repeater
Repeater:重复执行子节点,直到一定次数 特点如下: 1.执行次数可以是无限循环,也可以是固定次数 2.一般来说,子节点的执行返回状态不会影响Repeater节点,但可以设置当子节点返回失败时, ...
随机推荐
- 阅读<Video Test Pattern Generator v7.0>笔记
阅读<Video Test Pattern Generator v7.0>笔记 1.数据宽度的问题 TotalDataWidth的计算公式: 疑问:为什么TotalDataWidth后面需 ...
- python 中变量引用问题
普通变量,如a=10,str="fdaf",它们在函数内的值是不会被带到函数外的,除非在函数内加上global,而引用是惰性原则,从最近的同名父级同名变量引用值 其它变量如列表,字 ...
- PrintWriter中的write与println方法居然就是这些区别
为什么循环中分别用write方法和println方法效果一样呢? import java.io.*; public class WriteLog { private BufferedReader bf ...
- Javascript中变量提升的问题(五)
一.函数声明变量提升 函数声明具有变量提升的问题,所以在函数被声明之前就可以访问. console.log(getValue()); function getValue() { return 'a ...
- vuex状态管理2
在vuex的官网https://vuex.vuejs.org中,提到的核心概念一共有5个,分别是State.Getter.Mutation.Action和Module,在上一篇随笔中,我们主要用到其中 ...
- NIO框架之MINA源码解析(四):粘包与断包处理及编码与解码
1.粘包与段包 粘包:指TCP协议中,发送方发送的若干包数据到接收方接收时粘成一包,从接收缓冲区看,后一包数据的头紧接着前一包数据的尾.造成的可能原因: 发送端需要等缓冲区满才发送出去,造成粘包 接收 ...
- [转][C#]枚举的遍历Enum
// 加载所有颜色 //foreach (Color item in Enum.GetValues(typeof(Color))) foreach (var item in typeof(Color) ...
- 廖雪峰Java5集合-2List-1使用List
1.List定义 List是一种有序链表: List内部按照元素的先后顺序存放 每个元素都可以通过索引确定自己的位置 类似数组,但大小可变 //List<E>是一种有序链表: //* Li ...
- centos7+apache+svn配置 踩坑,注意权限问题。apache应用目录checkout应用 必须用这个命令:svn co file:///home/svn/test/ test ,通过svn add * &&commit 及任意修改都是不行的
阅读帮助 命令提示符 [root@server-002 ~]# 表示当前服务root用户执行的命令 [svn@server-002 ~]$ 表示普通用户svn执行的命令 [root@localhost ...
- MFC+mongodb+nodejs 数据库的读取与写入操作
首先通过nodejs和mongodb建立后端服务器 一.在windows平台下启动mongodb服务器 1.进入mongodb的安装目录,并进去bin目录启动mongod 2.在d盘建立mongodb ...