Web: https://docs.unrealengine.com/latest/INT/Engine/AI/BehaviorTrees/index.html

Test project: D:\EngineStudy\Unreal\4.14\TestProject\HowTo_BehaviorTree

Behavior tree主要就是用来实现AI的一套系统,比如角落爬动的蟑螂,攻击主角的NPC等AI;

creating a NavMesh, creating an AI Controller, creating a Character that will be controlled by that AI Controller, and creating all the parts necessary for a simple Behavior Tree;

The Blackboard asset stores all of the data needed for a specific instance of an AI controller that will be referenced by the Behavior Tree. In this specific case, we'll be storing an Actor to Follow (TargetToFollow), the AI's starting location (HomeLocation), and the last know location of the Actor we're following (TargetLocation).

怎样创建一个有自己行为的NPC?(本笔记按照以上网页中demo总结,实现内容是一个能够追赶主角的npc)

  1. 如果是一个空白场景,而不是这个demo中说的template “Blueprint Top Down”, 那么还是需要一个”Nav Mesh Bounds Volume”来框住整个角色的运动范围; 按照官网的说法:” The NavMesh in Unreal has a number of functions that work well with Behavior Trees, and while it may not be necessary depending on the game you are making, it will help in a number of game types.”看来这种类型的actor具有的很多接口应该对于结合behavior tree模块是非常管用的;
  2. 创建一个Character为基类的class blueprint, 代表这种/个NPC;
  3. 创建一个AIController类型(AIModule.AIController)的class blueprint;
  4. 创建一个blackboard资源;
  5. 创建一个Behavior trees类型的资源;
  6. 完善1中创建的character类型的blueprint, 填充数据:所需要的animation blueprint(需要提前建好,或者是不使用blueprint anim而是用animation asset); 所需要的哪个AI Controller Class(填进去2中创建的class blueprint即可);所需要的mesh; npc行走的速度等数据;
  7. 完善3中创建的blackboard: 黑板是用来存储一些AI controller需要的数据的,这些数据会被Behavior tree引用; 比如创建一个跟着主角跑的NPC,那么这个黑板中就应该存储变量:主角actor,主角位置,NPC自身位置,这些数据也都是有类型的,object,float或者vector等;
  8. 完善2中创建的AI controller: npc是具有AI行为的,所以要有自己的ai controller; AI Controller会在3中的黑板中写数据,并且运行behavior tree;

    AI Controller的完善可以分为两部分,一部分是完善黑板部分,一部分是运行behavior tree部分(因为这两部分都归ai controller管理):

    8.1. 完善黑板相关内容: 一般都是begin play的event接下来设定使用哪个黑板,利用函数”Use Blackboard”来设定,这样AI Controller就和黑板关联了起来; 为黑板中的变量写入数据可以使用”Set Value as Vector”函数(这个函数需要一个参数,表示为哪个变量来设定参数,我们需要传入一个类型为”Name”的属于[创建于]AI Controller[面板]的variables,这种Name类型,猜测UE4会根据这个名字来找到真正的变量; 同样blackboard com有类似的函数Get Values as Vector/Int …); 引用黑板可以使用接口”Get Blackboard”(传入self); 引用npc位置信息可以使用接口”Get Controller Pawn”和(接下来调用)”GetActorLocation”;

    8.2. 运行behavior tree: 8.1的下一步可以调用函数”Run Behavior Tree”函数,该函数需要一个BTAsset的参数,可以传入4中我们创建的资源;这样4中的behavior tree资源就和AI Controller关联了起来,同样使用哪个behavior tree的资源也确定了; 如果现在运行PIE,那么调用的就会是这个behavior tree,但因为我们还没有配置过4中的behavior tree资源,所以应该没什么东西;

    8步骤配置完成后,在AI controller的class blueprint的EventGraph里面就会看到如下节点调用:

    

9. 把2中创建7中完善的AIController class blueprint拖到场景中创建一个这个class blueprint的实例; 这时候PIE会发现拖入场景中的那个NPC并不会追逐主角,这是因为他的behavior tree我们还没有配置;我们之前只是把npc的位置写入到了blackboard中而已, 并且将behavior tree,blackboard和AI Controller发生了关联,确定了NPC的character 类型的class blueprint使用这个AI Controller;

10.配置behavior tree:

10.1. 关于behavior tree里面会有两个比较重要的节点”Selector”和”Sequence”,官网对于二者的介绍是这样的:” The Selector node will run through its children, from left to right, until one of them succeeds, at which point it will fail back up the tree. While the Sequence node will run through its children from left to right until one of them fails, at which point it will fail back up the tree.”, 都是从左到右遍历子节点,一个是遇到成功一个就返回上层,一个是遇到一个失败就返回上层;

另外还有一个是”SimpleParallel”: The Simple Parallel node allows a single main task node to be executed along side of a full tree. When the main task finishes, the setting in Finish Mode dictates if the node should finish immediately, aborting the secondary tree, or if it should delay for the secondary tree to finish.

这三种统称为”Composite” 节点, 官方对其说明为: Composite Nodes define the root of a branch and the base rules for how that branch is executed. They can have Decorators applied to them to modify entry into their branch or even cancel out mid execution. Also, they can have Services attached to them that will only be active if the children of the Composite are being executed.

其中对于Decorator: Decorator, also known as conditionals in other Behavior Tree systems, are attached to either a Composite or a Task node and define whether or not a branch in the tree, or even a single node, can be executed.

对于Services: Services attach to Composite nodes, and will execute at their defined frequency as long as their branch is being executed. These are often used to make checks and to update the Blackboard. These take the place of traditional Parallel nodes in other Behavior Tree systems

10.2. 在behavior tree的details 面板里面可以设定,当前behavior tree关联使用哪个blackboard;

10.3. 我们创建的blueprint class[资源]都可以成为一种类型来使用, 比如我们在2中创建一个AIController的class blueprint,名字是”Follower_AI_CON”,那么我们在别的一些blueprint里面添加成员变量的时候,就可以找到并且添加一个类型是”Follower_AI_CON_C”的变量,比如该变量取名为”AI_CON_Ref”;

10.4. Services attach to Composite nodes, and will execute at their defined frequency as long as their branch is being executed. These are often used to make checks and to update the Blackboard. These take the place of traditional Parallel nodes in other Behavior Tree systems

10.5. Decorator, also known as conditionals in other Behavior Tree systems, are attached to either a Composite or a Task node and define whether or not a branch in the tree, or even a single node, can be executed.

10.6. Tasks are nodes that "do" things, like move an AI, or adjust Blackboard values. They can have Decorators attached to them.

10.7. 一个behavior tree可以由Selector和Sequence以及Simple Parallel这三种Composite组成,另外blackboard,Serivces以及Decorator可以attach到这些composite上,而task可以作为最末端叶子节点attach到这些composite; 不同的节点有不同的逻辑功能,整个behavior tree利用他们完成AI的整体逻辑;

10.8. 创建一个Services, 它内部的成员变量如果是可见的(眼睛icon是张开的),那么在behavior tree里面使用这个service后再在这个service的detail面板里面就会有default category下面就会出现之前的成员变量让你去设定值,这个值应该是和black board关联的地方;

10.9. Blackboard节点在behavior tree里面存在的作用就用于检测是否指定的blackboard key被设定了; “The Blackboard node will check to see if a value is set on the given Blackboard Key”;

所以官方的例子中,解析:

最上层的root下面链接一个Services(ArgoCheck),这个Services主要用于对blackboard里面的key变量进行设定值,由于service本身是有频率的,所以它在root下的调用就不是逐帧tick的,那么也就不是逐帧去设定每个blackboard里的key的,对于其逻辑,它内部对于是否在这一波(因为有调用的Interval,这个demo是0.5秒调一次)调用中设定TargetToFollow(blackboard中一个key变量)的值是不一定的;所以接下来这个Selector(包含Service ArgoCheck)左边连接了一个Selector,右边连接了一个Sequence;

而这左右两边的这两个节点开始就包含了一个blackboard节点,其判断条件就是TargetFollow是否被设定,左边是TargetFollow被设定,右边是TargetFollow没有被设定,从而走左边还是走右边,所以是这两个节点的上层那个Selector里的ArgoCheck Service通过是否在这一波调用中设定一个blackboard中的key变量来控制下面节点的走势;

https://docs.unrealengine.com/latest/INT/Engine/AI/BehaviorTrees/NodeReference/index.html

10.10.  Behavior tree构建思路: 如果。。。就。。。 否则。。。就。。。  采用Selector来区分开两个分支,两套逻辑;

先。。。 然后。。。 采用sequence进行序列行为执行;

如官网的追赶主角的例子:整体行为是,如果发现palyer,就追击,否则就(这之前逻辑使用Selector节点,也就是顶层的那个selector节点)已经追到就先等待,然后(这个先。。。然后。。。 就使用sequence节点)回到AI自己初始点;至于追击,休息,回到原点这些具体行为就利用task节点;

节点类型

描述

Composite

这种节点定义一个分支的根以及该分支如何被执行的基本规则

Task

这种节点是行为树的叶子,实际“执行”操作,不含输出连接。

Decorator

即为条件语句。这种节点附着于其他节点,决定着树中的一个分支,甚至单个节点是否能被执行。

Services

这种节点附着在 Composite 节点上,只要其分支节点被执行,它们便将按所定义的频率执行。它们常用于检查和更新黑板。它们以行为树系统的形态取代了传统平行节点。

Root

Root 节点十分独特,它是行为树的“根”。它只拥有一个连接,无法被 Decorators 或 Services 附着。Root 节点没有其自身属性,但选中后会在 Details 面板中显示行为树的属性。在该面板中可设置行为树的黑板资源。

《Note --- Unreal 4 --- behavior tree》的更多相关文章

  1. 《Note --- Unreal --- MemPro (CONTINUE... ...)》

    Mem pro 是一个主要集成内存泄露检测的工具,其具有自身的源码和GUI,在GUI中利用"Launch" button进行加载自己待检测的application,目前支持的平台为 ...

  2. 《Note --- Unreal 4 --- Sample analyze --- StrategyGame(continue...)》

    ---------------------------------------------------------------------------------------------------- ...

  3. 《Note --- Unreal 4 --- PersonaHowToMovement》

    https://docs.unrealengine.com/latest/CHN/Gameplay/HowTo/CharacterMovement/index.html 这里的demo是按照一些per ...

  4. 《Note --- Unreal 4 --- B project --- Second UV issue》

    Second uv 可以通过editor来生成: 这部分内容都是在staticMeshEditor这个文件夹下面的代码里: 关于UI的相应机制,有个文件UICommandList.cpp例如我点击st ...

  5. 《Note --- Unreal 4 --- matinee》

    https://docs.unrealengine.com/latest/CHN/Engine/Matinee/index.html https://docs.unrealengine.com/lat ...

  6. 《小白的CFD之旅》招募写手

    <小白的CFD之旅>系列招募写手. 由于工作繁忙,<小白的CFD之旅>系列更新缓慢,现招募志愿者写手.这是一个分享平台,欢迎各位愿意分享自己CFD学习经历的朋友们. <小 ...

  7. 20145212 实验三《敏捷开发与XP实践》

    20145212 实验三<敏捷开发与XP实践> 实验内容 使用git上传代码 与20145223同学一组,使用git相互更改代码 同组实验报告链接:http://www.cnblogs.c ...

  8. 《Linux内核设计与实现》读书笔记(十七)- 设备与模块

    本章主要讨论与linux的设备驱动和设备管理的相关的4个内核成分,设备类型,模块,内核对象,sysfs. 主要内容: 设备类型 内核模块 内核对象 sysfs 总结 1. 设备类型 linux中主要由 ...

  9. 《Linux内核设计与实现》读书笔记(十一)- 定时器和时间管理【转】

    转自:http://www.cnblogs.com/wang_yb/archive/2013/05/10/3070373.html 系统中有很多与时间相关的程序(比如定期执行的任务,某一时间执行的任务 ...

随机推荐

  1. Apache thrift RPC 双向通信

    在上一篇介绍Apache thrift 安装和使用,写了一个简单的demo,讲解thrift服务的发布和客户端调用,但只是单向的客户端发送消息,服务端接收消息.而客户端却得不到服务器的响应. 在不涉及 ...

  2. 设计C/S架构应用程序的并发功能

    C/S架构的ERP.CRM程序有的是以并发点(Concurrency)来销售,并发点是指同时在线人数.并发数量大时,理论上程序的运行速度会慢,软件供应商(vendor)也以控制并发的上限以解决客户对系 ...

  3. VC 中与字符串相关的宏 _T、TEXT,_TEXT、L 的作用

    CSDN原博文:http://blog.csdn.net/houkai363/article/details/8134787 遇到了:不能将参数 1 从“const char [5]”转换为“LPCT ...

  4. python网络爬虫 新浪博客篇

    上次写了一个爬世纪佳缘的爬虫之后,今天再接再厉又写了一个新浪博客的爬虫.写完之后,我想了一会儿,要不要在博客园里面写个帖子记录一下,因为我觉得这份代码的含金量确实太低,有点炒冷饭的嫌疑,就是把上次的代 ...

  5. 使用WordPress搭建自己的博客

    突然间发现自己在阿里上有一个免费的虚拟云空间,好像是什么时候阿里云搞活动赠送的.看了看还有不少时间,就决定自己搭建一个博客系统.说到搭建自己的博客,第一时间就想到WordPress,这个用起来应该是最 ...

  6. 实验:Oracle直接拷贝物理存储文件迁移

    实验目的:Oracle直接拷贝物理文件迁移,生产库有类似施工需求,故在实验环境简单验证一下. 实验环境: A主机:192.168.1.200 Solaris10 + Oracle 11.2.0.1 B ...

  7. FFmpeg数据结构:AVPacket解析

    本文主要从以下几个方面对AVPacket做解析: AVPacket在FFmpeg中的作用 字段说明 AVPacket中的内存管理 AVPacket相关函数的说明 结合AVPacket队列说明下AVPa ...

  8. SSE指令集学习:Compiler Intrinsic

    大多数的函数是在库中,Intrinsic Function却内嵌在编译器中(built in to the compiler). 1. Intrinsic Function Intrinsic Fun ...

  9. 代码的坏味道(8)——被拒绝的馈赠(Refused Bequest)

    坏味道--被拒绝的馈赠(Refused Bequest) 特征 子类仅仅使用父类中的部分方法和属性.其他来自父类的馈赠成为了累赘. 问题原因 有些人仅仅是想重用超类中的部分代码而创建了子类.但实际上超 ...

  10. Django--自定义用户认证

    Django自带的用户认证 以前都是用Django自带的用户认证,用户名字段一对一关系对应Django--User表(其实它也是继承了abstractbaseuser). 1 2 3 from dja ...