【发布时间】:2016-03-27 14:53:15
【问题描述】:
我关注Behavior Tree Quick Start Guide 并让一切正常工作。
我现在正尝试将 Follower_AI_CON 控制器从 Blue Print 转换为 C++,但我无法获得对行为树的引用(仅使用 C++)。
我可以通过创建一个父类为 AIController 的 Blue Print 并从编辑器中选择我的行为树(下图)来获得参考,但考虑到我不会使用除了那个微小的下拉菜单之外的任何东西的蓝图。
我在看似简单的任务上花费了令人沮丧的几个小时。我想我不像我让人们相信的那么聪明! :D
我的基本 .h / .cpp 文件如下所示:
.h
UCLASS()
class THIRDPERSON_API AAIController : public AAIController
{
GENERATED_BODY()
ATPAIController(const class FObjectInitializer& ObjectInitializer);
public:
virtual void Possess(class APawn* InPawn) override;
// Reference to the AI's blackboard component.
UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "AI")
UBlackboardComponent* BlackboardComponent;
// The Behavior Tree Component used by this AI.
UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "AI")
UBehaviorTreeComponent* BehaviorTreeComponent;
UPROPERTY(EditDefaultsOnly, Category = "AI")
class UBehaviorTree* BehaviorTree;
};
.cpp
AAIController::AAIController(const FObjectInitializer& ObjectInitializer) : Super(ObjectInitializer)
{
BlackboardComponent = ObjectInitializer.CreateDefaultSubobject<UBlackboardComponent>(this, TEXT("EnemyAIBlackboard"));
BehaviorTreeComponent = ObjectInitializer.CreateDefaultSubobject<UBehaviorTreeComponent>(this, TEXT("EnemyAIBehaviorTree"));
}
void AAIController::Possess(APawn* InPawn)
{
Super::Possess(InPawn);
if (BehaviorTree)
{
BlackboardComponent->InitializeBlackboard(*BehaviorTree->BlackboardAsset);
BehaviorTreeComponent->StartTree(*BehaviorTree, EBTExecutionMode::Looped);
}
}
【问题讨论】:
标签: c++ unreal-engine4 behavior-tree