How to set Behavior Tree in C++

Hmm I ran into same problem and here are 2 solutions, first you must do:

AIControllerClass = AMyAI_Controller::StaticClass();

then you can do it with constructor:

struct FConstructorStatics
	{
		ConstructorHelpers::FObjectFinderOptional<UBehaviorTree> Beh_Tree;
		FConstructorStatics()
			: Beh_Tree(TEXT("/Game/Blueprints/MyAI_Beh_Tree"))
		{
		}
	};
	static FConstructorStatics ConstructorStatics;
	MyAIBehavior = ConstructorStatics.Beh_Tree.Get();

or this way without constructor:

FString Path = "/Game/Blueprints/MyAI_Beh_Tree";
MyAIBehavior = Cast<UBehaviorTree>(StaticLoadObject(UBehaviorTree::StaticClass(), nullptr, *Path));

Don’t forget to include:

#include "BehaviorTree/BehaviorTree.h"

and if you do it using constructor of course also:

#include "UObject/ConstructorHelpers.h"
1 Like