[Unity插件]Lua行为树(六):打印树结构
经过前面的文章,已经把行为树中的四种基本类型节点介绍了下。接下来可以整理一下,打印一下整棵行为树。注意点如下:
1.可以把BTBehaviorTree也当作一种节点,这样就可以方便地进行行为树嵌套了
2.可以在BTBehaviorTree中的SetStartTask、BTParentTask中的AddChild,即在设置节点和添加节点时对节点的信息进行打印
以下面这棵行为树为例:

BTTask.lua
BTTask = {};
local this = BTTask;
function this:New()
local o = {};
setmetatable(o, self);
self.__index = self;
o.executionStatus = BTTaskStatus.Inactive; --该节点的执行状态
o.root = nil; --根节点
o.parent = nil; --父节点
o.layer = ; --第几层
o.index = ; --父节点下的第几个节点
o.name = "BTTask"; --该节点名字
return o;
end
function this:ToString()
local root = "root:";
if (self.root) then
root = root .. self.root.name;
else
root = root .. "nil";
end
local parent = "parent:";
if (self.parent) then
parent = parent .. self.parent.name;
else
parent = parent .. "nil";
end
local layer = "layer:" .. self.layer;
local index = "index:" .. self.index;
local name = "name:" .. self.name;
print(root .. " " .. parent .. " " .. layer .. " " .. index .. " " .. name);
end
BTParentTask.lua
BTParentTask = BTTask:New(); local this = BTParentTask; function this:New()
local o = {};
setmetatable(o, self);
self.__index = self;
o.currentChildIndex = ; --当前运行到第几个子节点
o.currentChildTask = nil; --当前运行的子节点
o.childTasks = {}; --子节点列表
return o;
end --添加子节点
function this:AddChild(task)
local index = #self.childTasks + ;
task.index = index;
task.layer = self.layer + ;
task.parent = self;
task.root = self.root;
self.childTasks[index] = task; if (BTBehaviorManager.isPrintTaskInfo) then
print(task:ToString());
end
end --是否有子节点
function this:HasChild()
if (#self.childTasks > ) then
return true;
else
return false;
end
end --获取子节点数目
function this:GetChildCount()
return #self.childTasks;
end --获取下一个要执行的子节点
function this:GetNextChild()
if (#self.childTasks >= (self.currentChildIndex + )) then
self.currentChildIndex = self.currentChildIndex + ;
return self.childTasks[self.currentChildIndex];
end
return nil;
end --重置
function this:Reset()
self.currentChildIndex = ;
self.currentChildTask = nil;
end
BTBehaviorTree.lua
--[[
树的根节点
--]]
BTBehaviorTree = BTTask:New(); local this = BTBehaviorTree; function this:New()
local o = {};
setmetatable(o, self);
self.__index = self;
return o;
end function this:SetStartTask(task)
task.root = self;
task.parent = self;
task.layer = self.layer + ;
self.startTask = task; if (BTBehaviorManager.isPrintTaskInfo) then
print(task:ToString());
end
end function this:OnUpdate()
if (self.startTask) then
return self.startTask:OnUpdate();
end
end
TestBehaviorTree.lua
TestBehaviorTree = BTBehaviorTree:New(); local this = TestBehaviorTree;
this.name = "TestBehaviorTree"; function this:New()
local o = {};
setmetatable(o, self);
self.__index = self;
self:Init();
return o;
end function this:Init()
local repeater = BTRepeater:New();
local selector = BTSelector:New();
local sequence = BTSequence:New();
local isNullOrEmpty = BTIsNullOrEmpty:New("");
local log = BTLog:New("This is a empty string!!!");
local log2 = BTLog:New("This is not a empty string"); self:SetStartTask(repeater); repeater:AddChild(selector); selector:AddChild(sequence);
selector:AddChild(log2); sequence:AddChild(isNullOrEmpty);
sequence:AddChild(log);
end
打印如下:

[Unity插件]Lua行为树(六):打印树结构的更多相关文章
- [Unity插件]Lua行为树(二):树结构
参考链接:https://blog.csdn.net/u012740992/article/details/79366251 在行为树中,有四种最基本的节点,其继承结构如下: Action->T ...
- [Unity插件]Lua行为树(十二):行为树管理
之前运行的行为树,都是一颗总树,那么实际上会有很多的总树,因此需要对行为树进行管理. BTBehaviorManager.lua BTBehaviorManager = {}; local this ...
- [Unity插件]Lua行为树(七):行为树嵌套
在上一篇的基础上,可以测试下行为树的嵌套,所谓的行为树嵌套,就是在一棵行为树下的某一个分支,接入另一棵行为树. 以下面这棵行为树为例: TestBehaviorTree2.lua TestBehavi ...
- [Unity插件]Lua行为树(一):BehaviorDesigner源码分析
BehaviorDesigner是Unity上的一款行为树插件,不过这个插件是用C#编写的,编写出来的行为树也是依赖于C#的,不利于热更,所以有必要写一个lua版本的. 首先下载BehaviorDes ...
- [Unity插件]Lua行为树(三):组合节点Sequence
Sequence的继承关系如下: Sequence->Composite->ParentTask->Task 上一篇已经实现了简单版本的ParentTask和Task(基于Behav ...
- [Unity插件]Lua行为树(十):通用行为和通用条件节点
在行为树中,需要扩展的主要是行为节点和条件节点.一般来说,每当要创建一个节点时,就要新建一个节点文件.而对于一些简单的行为节点和条件节点,为了去掉新建文件的过程,可以写一个通用版本的行为节点和条件节点 ...
- [Unity插件]Lua行为树(九):条件节点调整
先看一下之前的条件节点是怎么设计的: BTConditional.lua BTConditional = BTTask:New(); local this = BTConditional; this. ...
- [Unity插件]Lua行为树(八):行为节点扩展
先看一下之前的行为节点是怎么设计的: BTAction.lua BTAction = BTTask:New(); local this = BTAction; this.taskType = BTTa ...
- [Unity插件]Lua行为树(四):条件节点和行为节点
条件节点和行为节点,这两种节点本身的设计比较简单,项目中编写行为树节点一般就是扩展这两种节点,而Decorator和Composite节点只需要使用内置的就足够了. 它们的继承关系如下: Condit ...
随机推荐
- C#之实现Scoket心跳机制
C#之实现Scoket心跳机制 标签: UnityC#TCPSocket心跳 2017-05-17 09:58 1716人阅读 评论(0) 收藏 举报 分类: Unity(134) C#(6) ...
- 关于此实现不是 Windows 平台 FIPS 验证的加密算法的一部分。
注册表进入如下路径中 HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Lsa\FipsAlgorithmPolicy 将 enable设置为0 ...
- centos 7 免密登录
本文转载自:https://www.cnblogs.com/hobinly/p/6039844.html 环境示例 Centos7 192.168.1.101 master Centos7 192. ...
- 1049.(*) Counting Ones
题意:题目大意:给出一个数字n,求1~n的所有数字里面出现1的个数 思路:转自(柳婼 の blog)遍历数字的低位到高位,设now为当前位的数字,left为now左边的所有数字构成的数字,right是 ...
- HashMap的自定义实现
一.背景: HashMap到底是怎么实现的? 一对一对的存放,通过key找value:map的键不能重复:自己怎么实现呢? 代码: Wife.java 辅助类 package com.cy.co ...
- slot内容分发
vue实现了一套内容分发的API,这套API基于当前的web components规范草案,将<slot>元素作为承载分发内容的出口. 在前面的父子组件中,我们提到过,在vue中,组件实例 ...
- hive中的表
一.内部表与外部表的比较 Hive表概念和关系型数据库表概念差不多.在Hive里表会和HDFS的一个目录相对应,这个目录会存放表的数据.目录默认是/usr/hive/warehouse/. 比如你在h ...
- 廖雪峰Java2面向对象编程-3继承和多态-1继承
1.继承 继承是一种代码复用的方式. Student与Person有相同部分的代码. Student可以从Person继承,这样Student获得了Person的所有功能,只需要编写新增的功能即可.通 ...
- linux采集CPU温度并上传数据到云平台判断是否需要beep
如果要beep肯定要apt install beep的 但光安装好还不够,需要执行模块加载 /sbin/modprobe pcspkr 再写一个bash脚本 data=$(/usr/bin/senso ...
- C语言强化——学生管理系统
系统模块设计 a.预处理模块 系统在启动时会根据配置文件里的内容去相应文件里去加载账户信息和学生信息. b.登陆模块 输入用户名和密码,输密码的时候用"*" 代表用户当前输入的内容 ...