void AMinionController::BeginPlay() { UBlackboardComponent *BBComponent = NewObject<UBlackboardComponent>(this); BBComponent->RegisterComponent(); UBBMinion* BB = NewObject<UBBMinion>(this); BBComponent->InitializeBlackboard(*BB); }
bool AAIController::InitializeBlackboard(UBlackboardComponent& BlackboardComp, UBlackboardData& BlackboardAsset) { check(BlackboardComp.GetOwner() == this); if (BlackboardComp.InitializeBlackboard(BlackboardAsset)) { OnUsingBlackBoard(&BlackboardComp, &BlackboardAsset); return true; } return false; }
bool AAIController::InitializeBlackboard(UBlackboardComponent& BlackboardComp, UBlackboardData& BlackboardAsset) { check(BlackboardComp.GetOwner() == this); if (BlackboardComp.InitializeBlackboard(BlackboardAsset)) { // find the "self" key and set it to our pawn const FBlackboard::FKey SelfKey = BlackboardAsset.GetKeyID(FBlackboard::KeySelf); if (SelfKey != FBlackboard::InvalidKey) { BlackboardComp.SetValue<UBlackboardKeyType_Object>(SelfKey, GetPawn()); } OnUsingBlackBoard(&BlackboardComp, &BlackboardAsset); return true; } return false; }
bool AAIController::RunBehaviorTree(UBehaviorTree* BTAsset) { // @todo: find BrainComponent and see if it's BehaviorTreeComponent // Also check if BTAsset requires BlackBoardComponent, and if so // check if BB type is accepted by BTAsset. // Spawn BehaviorTreeComponent if none present. // Spawn BlackBoardComponent if none present, but fail if one is present but is not of compatible class if (BTAsset == NULL) { UE_VLOG(this, LogBehaviorTree, Warning, TEXT("RunBehaviorTree: Unable to run NULL behavior tree")); return false; } bool bSuccess = true; bool bShouldInitializeBlackboard = false; // see if need a blackboard component at all UBlackboardComponent* BlackboardComp = NULL; if (BTAsset->BlackboardAsset) { BlackboardComp = FindComponentByClass<UBlackboardComponent>(); if (BlackboardComp == NULL) { BlackboardComp = ConstructObject<UBlackboardComponent>(UBlackboardComponent::StaticClass(), this, TEXT("BlackboardComponent")); if (BlackboardComp != NULL) { BlackboardComp->InitializeBlackboard(BTAsset->BlackboardAsset); BlackboardComp->RegisterComponent(); bShouldInitializeBlackboard = true; } } else if (BlackboardComp->GetBlackboardAsset() == NULL) { BlackboardComp->InitializeBlackboard(BTAsset->BlackboardAsset); } else if (BlackboardComp->GetBlackboardAsset() != BTAsset->BlackboardAsset) { bSuccess = false; UE_VLOG(this, LogBehaviorTree, Log, TEXT("RunBehaviorTree: BTAsset %s requires blackboard %s while already has %s instantiated"), *GetNameSafe(BTAsset), *GetNameSafe(BTAsset->BlackboardAsset), *GetNameSafe(BlackboardComp->GetBlackboardAsset()) ); } } if (bSuccess) { UBehaviorTreeComponent* BTComp = Cast<UBehaviorTreeComponent>(BrainComponent); if (BTComp == NULL) { UE_VLOG(this, LogBehaviorTree, Log, TEXT("RunBehaviorTree: spawning BehaviorTreeComponent..")); BrainComponent = BTComp = ConstructObject<UBehaviorTreeComponent>(UBehaviorTreeComponent::StaticClass(), this, TEXT("BTComponent")); BrainComponent->RegisterComponent(); if (BrainComponent->bWantsInitializeComponent) { // make sure that newly created component is initialized before running BT // both blackboard and BT to must exist before calling it! BrainComponent->InitializeComponent(); } } if (bShouldInitializeBlackboard && BlackboardComp && BlackboardComp->bWantsInitializeComponent) { // make sure that newly created component is initialized before running BT // both blackboard and BT to must exist before calling it! BlackboardComp->InitializeComponent(); } check(BTComp != NULL); BTComp->StartTree(BTAsset, EBTExecutionMode::Looped); } return bSuccess; }