DeanWang

  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

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 面板中显示行为树的属性。在该面板中可设置行为树的黑板资源。

 

posted on 2016-12-20 15:44  DeanWang  阅读(883)  评论(0编辑  收藏  举报