我跟着Behavior Tree Quick Start Guide,让一切都运转得足够简单。
我现在正在尝试将Follower_AI_CON
控制器从蓝图转换为C ++,但我无法获得对行为树的引用(仅使用C ++)。
我可以通过创建一个父类为AIController
的蓝图并从编辑器中选择我的行为树(下图)来获得引用,但考虑到我不会这样做,这似乎很浪费使用蓝色打印除了那个微小的下拉菜单之外的其他任何东西。
我花了很多时间看似简单的任务。我想我不像我引导人们相信那么聪明! :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);
}
}
答案 0 :(得分:0)
所以你需要在不制作AIController蓝图的情况下指定行为树吗?您有两种选择:
对路径进行硬编码
您可以在代码中硬编码树的路径,然后加载它。但这种方法并不是数据驱动的:
FString Path = "/Game/AIStuff/FollowerBT";
UBehaviorTree* Tree = Cast<UBehaviorTree>(StaticLoadObject(UBehaviorTree::StaticClass(), nullptr, *Path.ToString()));
设置在其他地方
在您拥有蓝图的其他全局类中设置树。我会把它放在一个自定义的GameMode蓝图中然后检索它:
UBehaviorTree* Tree = Cast<AMyGameMode>(GetWorld()->GetAuthGameMode())->MyBehaviorTree;