bool AAIController::UseBlackboard(UBlackboardData* BlackboardAsset) { if (BlackboardAsset == NULL) { UE_VLOG(this, LogBehaviorTree, Log, TEXT("UseBlackboard: trying to use NULL Blackboard asset. Ignoring")); return false; } bool bSuccess = true; UBlackboardComponent* BlackboardComp = FindComponentByClass<UBlackboardComponent>(); if (BlackboardComp == NULL) { BlackboardComp = NewObject<UBlackboardComponent>(this, TEXT("BlackboardComponent")); if (BlackboardComp != NULL) { InitializeBlackboard(*BlackboardComp, *BlackboardAsset); BlackboardComp->RegisterComponent(); } } else if (BlackboardComp->GetBlackboardAsset() == NULL) { InitializeBlackboard(*BlackboardComp, *BlackboardAsset); } else if (BlackboardComp->GetBlackboardAsset() != BlackboardAsset) { UE_VLOG(this, LogBehaviorTree, Log, TEXT("UseBlackboard: requested blackboard %s while already has %s instantiated. Forcing new BB.") , *GetNameSafe(BlackboardAsset), *GetNameSafe(BlackboardComp->GetBlackboardAsset())); InitializeBlackboard(*BlackboardComp, *BlackboardAsset); } return bSuccess; }
void AMinionController::BeginPlay() { UBlackboardComponent *BBComponent = NewObject<UBlackboardComponent>(this); BBComponent->RegisterComponent(); UBBMinion* BB = NewObject<UBBMinion>(this); BBComponent->InitializeBlackboard(*BB); }
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; }