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 = Blackboard;
    if (BTAsset->BlackboardAsset && (Blackboard == nullptr || Blackboard->IsCompatibleWith(BTAsset->BlackboardAsset) == false))
    {
        bSuccess = UseBlackboard(BTAsset->BlackboardAsset, BlackboardComp);
    }

    if (bSuccess)
    {
        UBehaviorTreeComponent* BTComp = Cast<UBehaviorTreeComponent>(BrainComponent);
        if (BTComp == NULL)
        {
            UE_VLOG(this, LogBehaviorTree, Log, TEXT("RunBehaviorTree: spawning BehaviorTreeComponent.."));

            BTComp = NewObject<UBehaviorTreeComponent>(this, TEXT("BTComponent"));
            BTComp->RegisterComponent();
        }

        // make sure BrainComponent points at the newly created BT component
        BrainComponent = BTComp;

        check(BTComp != NULL);
        BTComp->StartTree(*BTAsset, EBTExecutionMode::Looped);
    }

    return bSuccess;
}
示例#2
0
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)
	{
		bSuccess = UseBlackboard(BTAsset->BlackboardAsset);
		BlackboardComp = FindComponentByClass<UBlackboardComponent>();
	}

	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();
		}

		check(BTComp != NULL);
		BTComp->StartTree(*BTAsset, EBTExecutionMode::Looped);
	}

	return bSuccess;
}