I had went through the AI and behavior trees documents in unreal website. But i didn’t really comprehend the concepts introduced there. So i made a very very simple project to see whether the AI module will work.
I did steps below but in the end, it didn’t work…
1.Create two classes: AIController and Character.
2.Add “AIModule” in my XXX.Build.cs.
3.Add a property of class UBehaviorTree to my Character and set a BehaviorTree to it in the UnrealEditor.
4.Add property of UBehaviorTreeComponent and UBlackboradComponent to my AIController, and initialized them:
AMyAIController::AMyAIController()
{
this->bbc = CreateDefaultSubobject<UBlackboardComponent>("bb");
this->btc = CreateDefaultSubobject<UBehaviorTreeComponent>("bt");
}
5.Overriden the Possess() method to initialize the Blackborad of Character, set a simple location to it and StartTree.
void AMyAIController::Possess(APawn* in)
{
auto ai = Cast<AMyAICharacter>(in);
if (ai != nullptr)
{
if (ai->bt->BlackboardAsset)
{
this->bbc->InitializeBlackboard(*(ai->bt->BlackboardAsset));
this->bbc->SetValueAsVector(FName("TargetLocation"), FVector(-10, 20, 448));
this->bbc->SetValueAsObject(FName("SelfActor"), ai);
}
this->btc->StartTree(*(ai->bt));
}
}
6.Compiled the code, create BehaviorTree and Blackbroad in content browser and set corresponding attributes to them.
7.Set AIContorllerClass to Character.
7.Add a NaviMeshBoundVolume, and the land true green.
8.Dragged a Character to level and play.
9.I can see the engine passed through all the nodes in BehaviorTree, but my character just stood still and did nothing…
I think i must miss some points, so it did’t work. But i can find where i’m wrong.