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