【问题标题】:Retrieve Behavior Tree without using Blue Print在不使用 Blue Print 的情况下检索行为树
【发布时间】: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


    【解决方案1】:

    所以你需要在不制作 AIController 蓝图的情况下指定行为树?你有两个选择:

    硬编码路径

    您可以在代码中硬编码树的路径,然后加载它。但这种方法不是很受数据驱动:

    FString Path = "/Game/AIStuff/FollowerBT";
    UBehaviorTree* Tree = Cast<UBehaviorTree>(StaticLoadObject(UBehaviorTree::StaticClass(), nullptr, *Path.ToString()));
    

    设置其他地方

    在您有蓝图的其他全局类中设置树。我会将其放入自定义 GameMode 蓝图中,然后检索它:

    UBehaviorTree* Tree = Cast<AMyGameMode>(GetWorld()->GetAuthGameMode())->MyBehaviorTree;
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-04-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-11-13
      • 1970-01-01
      相关资源
      最近更新 更多