Get a reference of a BehaviorTree created inside the Editor

Hi Guys,

I need to get the instance of a BehaviorTree created in the Editor (using Content Browser/Misscellaneous/BehaviorTree) in my AIController class.

I created the Behavior Tree inside the editor to see the Workflow and setup graphically but i like to get a instance of this behavior tree inside my AIController extended c++ class. To do a RunBehaviorTree with this reference. How i can do this, how i can get a reference of a BehaviorTree created inside the Editor ??

I know, i can do this in AIController blueprint connecting the Begin Play node to Run behavior Tree node and select the corresponding Asset, but i like to do this in Cpp. My problem is getting the BehaviorTree instance.

I trying using

static ConstructorHelpers::FObjectFinder BehaviorTreeBPClass(TEXT(“BehaviorTree’/Game/MyBehaviorTree.MyBehaviorTree’”));

inside AIController constructor but when i run this the editor show me this Message in the log tab.

Error /Game/MyBehaviorTree: Can’t find file for asset. /Script/BehaviorTreeEditor

Info Failed to load /Script/BehaviorTreeEditor.BehaviorTreeGraph Referenced by MyBehaviorTree Property /Script/AIModule.BehaviorTree:BTGraph

Info Failed to load /Script/BehaviorTreeEditor.BehaviorTreeGraph Referenced by MyBehaviorTree Property /Script/Engine.Blueprint:EditedDocumentInfo.EditedObject

The BehaviorTree for this test only have the Root node.

Thanks and sorry for my bad english

What UE4 version are you using? In general something like this should work:

static ConstructorHelpers::FObjectFinder<UBehaviorTree> BasicTree(TEXT("/Game/AI/BehaviorTrees/DefaultBT"));
BehaviorTree = BasicTree.Object;

Cheers,

–mieszko

The solution suggested by MieszkoZ works only inside a constructor, if you are outside a constructor you should use something like:

UClass* myAIController = StaticLoadClass(UObject::StaticClass(), NULL, TEXT("/Game/Characters/AI/Follower_AI_Controller.Follower_AI_Controller_C"), NULL, LOAD_None, NULL);

Please note there is no “Blueprint’” at the start and there is an additional “_C” at the end.