基于C++代码的UE4学习(三十三)——创建Blackboard和BehaviorTree

 

对于创建AI,需要两个东西,一个是BehaviorTree,一个是Blackboard。

BehaviorTree是管理AI行为的,例如寻找、攻击、行走等,Blackboard是管理数据的,例如AI的目标,行走范围,速度等。

首先创建这两个对象。

 

 

 

起两个好记的名字即可。

打开行为树(BehaviorTree),加载Blackboard。

 

 

 

再选择到Blackboard上,创建一个变量,取名为Target,变量类型为Object,base设置为Actor。

 

 

 创建一个继承自AIController类的C++类。

 

 

1 // Fill out your copyright notice in the Description page of Project Settings.
 2 
 3 #pragma once
 4 
 5 #include "CoreMinimal.h"
 6 #include "AIController.h"
 7 #include "BehaviorTree\BehaviorTreeComponent.h"
 8 #include "BehaviorTree\BlackboardComponent.h"
 9 
14 
15 #include "BehaviorTree\Blackboard\BlackboardKeyAllTypes.h"
16 #include "MyAIController.generated.h"
17 
18 /**
19  * 
20  */
21 UCLASS()
22 class AI_FIRST_API AMyAIController : public AAIController
23 {
24     GENERATED_BODY()
25 
26 public:
27     AMyAIController();
28     void OnPossess(APawn* possesPawn);
29 
30 
31 private:
32     class UBehaviorTreeComponent* MyTree;
33     class UBlackboardComponent* myBoard;
34     FBlackboard::FKey target;
35 };

 

 

UBehaviorTreeComponent与UBlackboardComponent将用于读取AI中的行为树和黑板对象。

FBlackboard::FKey定义的变量target是用于链接黑板上刚才定义的变量Target的。

 

 

1 // Fill out your copyright notice in the Description page of Project Settings.
 2 
 3 
 4 #include "MyAIController.h"
 5 #include "MyEnemy.h"
 6 
 7 AMyAIController::AMyAIController() {
 8     MyTree = CreateDefaultSubobject<UBehaviorTreeComponent>("MyTree");
 9     myBoard = CreateDefaultSubobject<UBlackboardComponent>("myBoard");
10 }
11 
17 
18 void AMyAIController::OnPossess(APawn* possesPawn) {  //之前的Possess方法已经废弃,查过API之后,用OnPossess方法替代。
19     Super::OnPossess(possesPawn);
20 
21     AMyEnemy* enemy = Cast<AMyEnemy>(possesPawn); //将控制器控制的AI转为AMyEnemy类。
22 
23     if (enemy && enemy->settingTree) {
24         myBoard->InitializeBlackboard(*enemy->settingTree->BlackboardAsset); //初始化AI中的黑板,黑板是在行为树中的。
25         target = myBoard->GetKeyID("Target");  //获得黑板上的变量
26         MyTree->StartTree(*enemy->settingTree);  //开始运行行为树
27     }
28 }

 

 

 1 // Fill out your copyright notice in the Description page of Project Settings.
 2 
 3 #pragma once
 4 
 5 #include "CoreMinimal.h"
 6 
11 
12 #include "GameFramework/Character.h"
13 #include "BehaviorTree/BehaviorTree.h"
14 #include "MyEnemy.generated.h"
15 
16 UCLASS()
17 class AI_FIRST_API AMyEnemy : public ACharacter
18 {
19     GENERATED_BODY()
20 
21 public:
22     // Sets default values for this character's properties
23     AMyEnemy();
24 
25 protected:
26     // Called when the game starts or when spawned
27     virtual void BeginPlay() override;
28 
29 public:    
30     // Called every frame
31     virtual void Tick(float DeltaTime) override;
32 
33     // Called to bind functionality to input
34     virtual void SetupPlayerInputComponent(class UInputComponent* PlayerInputComponent) override;
35 public:
36     UPROPERTY(EditAnywhere, BlueprintReadWrite)
37     class UBehaviorTree* settingTree;
38 };

 

上面是创建的AI类,也就是敌人类,继承与Character类,起名为AMyEnemy类。

 

 1 // Fill out your copyright notice in the Description page of Project Settings.
 2 
 3 
 4 #include "MyEnemy.h"

10 AMyEnemy::AMyEnemy()
11 {
12      // Set this character to call Tick() every frame.  You can turn this off to improve performance if you don't need it.
13     PrimaryActorTick.bCanEverTick = true;
14 
15 }
16 
17 // Called when the game starts or when spawned
18 void AMyEnemy::BeginPlay()
19 {
20     Super::BeginPlay();
21     
22 }
23 
24 // Called every frame
25 void AMyEnemy::Tick(float DeltaTime)
26 {
27     Super::Tick(DeltaTime);
28 
29 }
30 
31 // Called to bind functionality to input
32 void AMyEnemy::SetupPlayerInputComponent(UInputComponent* PlayerInputComponent)
33 {
34     Super::SetupPlayerInputComponent(PlayerInputComponent);
35 
36 }

 

 

在AMyEnemy蓝图中加载行为树,编译运行。

 

 

 

效果如下:

 

 

  • 2
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值