Ejemplo n.º 1
0
EBTNodeResult::Type URandomWander::ExecuteTask(UBehaviorTreeComponent& OwnerComp, uint8* NodeMemory)
{

	UBlackboardComponent* Blackboard = OwnerComp.GetBlackboardComponent();

	if (!GetWorld())
	{
		return EBTNodeResult::Failed;
	}

	FVector NewWaypoint = FVector::ZeroVector;

	FVector CurrentPos = Blackboard->GetOwner()->GetActorLocation();

	NewWaypoint.X = CurrentPos.X + FMath::FRandRange(-5000, 5000);
	NewWaypoint.Y = CurrentPos.Y + FMath::FRandRange(-5000, 5000);

	while (FVector::Dist(NewWaypoint, Blackboard->GetOwner()->GetActorLocation()) <= 2000.f && TraceFromPosition(OutResult, 100.f, ECollisionChannel::ECC_EngineTraceChannel1, NewWaypoint, OwnerComp) == false)
	{
		NewWaypoint.X = CurrentPos.X + FMath::FRandRange(-5000, 5000);
		NewWaypoint.Y = CurrentPos.Y + FMath::FRandRange(-5000, 5000);
	}

	if (!NewWaypoint.IsZero())
	{
		Blackboard->SetValue<UBlackboardKeyType_Vector>(BlackboardKey.GetSelectedKeyID(), NewWaypoint);

		return EBTNodeResult::Succeeded;
	}

	return EBTNodeResult::Failed;
}
Ejemplo n.º 2
0
bool AAIController::ShouldSyncBlackboardWith(const UBlackboardComponent& OtherBlackboardComponent) const
{
    return Blackboard != nullptr
           && Blackboard->GetBlackboardAsset() != nullptr
           && OtherBlackboardComponent.GetBlackboardAsset() != nullptr
           && Blackboard->GetBlackboardAsset()->IsRelatedTo(*OtherBlackboardComponent.GetBlackboardAsset());
}
void UBTDecorator_BlueprintBase::OnBecomeRelevant(UBehaviorTreeComponent& OwnerComp, uint8* NodeMemory)
{
	if (AIOwner != nullptr && ReceiveObserverActivatedImplementations & FBTNodeBPImplementationHelper::AISpecific)
	{
		ReceiveObserverActivatedAI(AIOwner, AIOwner->GetPawn());
	}
	else if (ReceiveObserverActivatedImplementations & FBTNodeBPImplementationHelper::Generic)
	{
		ReceiveObserverActivated(ActorOwner);
	}

	if (GetNeedsTickForConditionChecking())
	{
		// if set up as observer, and has a condition check, we want to check the condition every frame
		// highly inefficient, but hopefully people will use it only for prototyping.
		bNotifyTick = true;
	}

	UBlackboardComponent* BlackboardComp = OwnerComp.GetBlackboardComponent();
	if (BlackboardComp)
	{
		for (int32 NameIndex = 0; NameIndex < ObservedKeyNames.Num(); NameIndex++)
		{
			const FBlackboard::FKey KeyID = BlackboardComp->GetKeyID(ObservedKeyNames[NameIndex]);
			if (KeyID != FBlackboard::InvalidKey)
			{
				BlackboardComp->RegisterObserver(KeyID, this, FOnBlackboardChange::CreateUObject(this, &UBTDecorator_BlueprintBase::OnBlackboardChange));
			}
		}
	}
}
Ejemplo n.º 4
0
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;
}
EBTNodeResult::Type USetNextTargetPoint::ExecuteTask(UBehaviorTreeComponent& OwnerComp, uint8* NodeMemory)
{
	Super::ExecuteTask(OwnerComp, NodeMemory);

	if (!GetWorld())
	{
		return EBTNodeResult::Failed;
	}

	//Set the blackboard to make calling it easier in the future

	UBlackboardComponent* Blackboard = OwnerComp.GetBlackboardComponent();

	//Get the ID of the key that we state in the editor
	uint8 TargetKeyID = Blackboard->GetKeyID(TargetName);

	//Get the AI so we can call a function on it
	UObject* SelfActor = Blackboard->GetValue<UBlackboardKeyType_Object>(TargetKeyID);

	//Cast it to the proper class
	AShadowAnnaCharacter* ShadowAnna = Cast<AShadowAnnaCharacter>(SelfActor);
	if (ShadowAnna)
	{
	//IF the cast works call the function
		//FVector NewTarget = ShadowAnna->SetNextTargetPoint();

		//Blackboard->SetValue<UBlackboardKeyType_Vector>(BlackboardKey.GetSelectedKeyID(), NewTarget);

		return EBTNodeResult::Succeeded;
	}

	return EBTNodeResult::Failed;
}
Ejemplo n.º 6
0
void AMinionController::BeginPlay()
{
	UBlackboardComponent *BBComponent = NewObject<UBlackboardComponent>(this);
	BBComponent->RegisterComponent();

	UBBMinion* BB = NewObject<UBBMinion>(this);
	BBComponent->InitializeBlackboard(*BB);
}
void UBTDecorator_CompareBBEntries::OnCeaseRelevant(UBehaviorTreeComponent& OwnerComp, uint8* NodeMemory)
{
	UBlackboardComponent* BlackboardComp = OwnerComp.GetBlackboardComponent();
	if (BlackboardComp)
	{
		BlackboardComp->UnregisterObserversFrom(this);
	}
}
void UBTDecorator_CompareBBEntries::OnBecomeRelevant(UBehaviorTreeComponent& OwnerComp, uint8* NodeMemory)
{
	UBlackboardComponent* BlackboardComp = OwnerComp.GetBlackboardComponent();
	if (BlackboardComp)
	{
		BlackboardComp->RegisterObserver(BlackboardKeyA.GetSelectedKeyID(), this, FOnBlackboardChange::CreateUObject(this, &UBTDecorator_CompareBBEntries::OnBlackboardChange));
		BlackboardComp->RegisterObserver(BlackboardKeyB.GetSelectedKeyID(), this, FOnBlackboardChange::CreateUObject(this, &UBTDecorator_CompareBBEntries::OnBlackboardChange));
	}
}
void UBTFunctionLibrary::ClearBlackboardValue(UBTNode* NodeOwner, const FBlackboardKeySelector& Key)
{
	check(NodeOwner != NULL);
	UBlackboardComponent* BlackboardComp = GetBlackboard(*NodeOwner);
	if (BlackboardComp != NULL)
	{
		BlackboardComp->ClearValue(Key.SelectedKeyName);
	}
}
Ejemplo n.º 10
0
bool AAIController::InitializeBlackboard(UBlackboardComponent& BlackboardComp, UBlackboardData& BlackboardAsset)
{
	check(BlackboardComp.GetOwner() == this);
	if (BlackboardComp.InitializeBlackboard(BlackboardAsset))
	{
		OnUsingBlackBoard(&BlackboardComp, &BlackboardAsset);
		return true;
	}
	return false;
}
Ejemplo n.º 11
0
void UBTTask_FlyTo::TickPathNavigation(UBehaviorTreeComponent& OwnerComp, FBT_FlyToTarget* MyMemory, float DeltaSeconds)
{
	const auto& queryResults = MyMemory->QueryResults;

	APawn* pawn = OwnerComp.GetAIOwner()->GetPawn();
	
	if (DebugParams.bVisualizePawnAsVoxels)
		NavigationManager->Debug_DrawVoxelCollisionProfile(Cast<UPrimitiveComponent>(pawn->GetRootComponent()));
	
	FVector flightDirection = queryResults.PathSolutionOptimized[MyMemory->solutionTraversalIndex] - pawn->GetActorLocation();

	//auto navigator = Cast<IDonNavigator>(pawn);

	// Add movement input:
	if (MyMemory->bIsANavigator)
	{
		// Customized movement handling for advanced users:
		IDonNavigator::Execute_AddMovementInputCustom(pawn, flightDirection, 1.f);
	}
	else
	{
		// Default movement (handled by Pawn or Character class)
		pawn->AddMovementInput(flightDirection, 1.f);
	}


	FVector test = FVector(10,10,100);
	//test.
		

	// Reached next segment:
	if (flightDirection.Size() <= MinimumProximityRequired)
	{
		// Goal reached?
		if (MyMemory->solutionTraversalIndex == queryResults.PathSolutionOptimized.Num() - 1)
		{
			UBlackboardComponent* blackboard = pawn->GetController()->FindComponentByClass<UBlackboardComponent>();
			blackboard->SetValueAsBool(FlightResultKey.SelectedKeyName, true);
			blackboard->SetValueAsBool(KeyToFlipFlopWhenTaskExits.SelectedKeyName, !blackboard->GetValueAsBool(KeyToFlipFlopWhenTaskExits.SelectedKeyName));

			// Unregister all dynamic collision listeners. We've completed our task and are no longer interested in listening to these:
			NavigationManager->StopListeningToDynamicCollisionsForPath(MyMemory->DynamicCollisionListener, queryResults);

			FinishLatentTask(OwnerComp, EBTNodeResult::Succeeded);

			return;
		}
		else
		{
			MyMemory->solutionTraversalIndex++;
		}
	}
}
void UBTDecorator_CompareBBEntries::OnBlackboardChange(const UBlackboardComponent& Blackboard, FBlackboard::FKey ChangedKeyID)
{
	UBehaviorTreeComponent* BehaviorComp = static_cast<UBehaviorTreeComponent*>(Blackboard.GetBrainComponent());
	if (BehaviorComp && (BlackboardKeyA.GetSelectedKeyID() == ChangedKeyID || BlackboardKeyB.GetSelectedKeyID() == ChangedKeyID))
	{
		BehaviorComp->RequestExecution(this);		
	}
}
Ejemplo n.º 13
0
void UBTDecorator_BlueprintBase::OnBlackboardChange(const UBlackboardComponent& Blackboard, FBlackboard::FKey ChangedKeyID)
{
	UBehaviorTreeComponent* BehaviorComp = (UBehaviorTreeComponent*)Blackboard.GetBrainComponent();
	if (BehaviorComp && GetShouldAbort(*BehaviorComp))
	{
		BehaviorComp->RequestExecution(this);		
	}
}
Ejemplo n.º 14
0
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;
}
void UDistanceToGhost::TickNode(UBehaviorTreeComponent& OwnerComp, uint8* NodeMemory, float DeltaSeconds)
{
	Super::TickNode(OwnerComp, NodeMemory, DeltaSeconds);

	UBlackboardComponent* Blackboard = OwnerComp.GetBlackboardComponent();

	UObject* TargetObject = Blackboard->GetValue<UBlackboardKeyType_Object>(TargetName);

	AGGJ16_AIController* AIController = Cast<AGGJ16_AIController>(Blackboard->GetOwner());

	AGGJ16_Vani* VaniCharacter = Cast<AGGJ16_Vani>(TargetObject);

	if (AIController && VaniCharacter)
	{
		Distance = FVector::Dist(VaniCharacter->GetActorLocation(), AIController->GetPawn()->GetActorLocation());

		Blackboard->SetValue<UBlackboardKeyType_Float>(BlackboardKey.GetSelectedKeyID(), Distance);
	}

	GEngine->AddOnScreenDebugMessage(4, 1, FColor::Blue, TEXT("Ding"));
}
void UCheckDistanceService::TickNode(UBehaviorTreeComponent& OwnerComp, uint8* NodeMemory, float DeltaSeconds)
{
	Super::TickNode(OwnerComp, NodeMemory, DeltaSeconds);

	UBlackboardComponent* Blackboard = OwnerComp.GetBlackboardComponent();

	UObject* TargetObject = Blackboard->GetValue<UBlackboardKeyType_Object>(TargetName);

	AArin* PlayerCharacter = Cast<AArin>(UGameplayStatics::GetPlayerCharacter(GetWorld(), 0));

	ABaseAIController* AIController = Cast<ABaseAIController>(Blackboard->GetOwner());

	if (AIController && PlayerCharacter)
	{
		Distance = FVector::Dist(PlayerCharacter->GetActorLocation(), AIController->GetPawn()->GetActorLocation());

		Blackboard->SetValue<UBlackboardKeyType_Float>(BlackboardKey.GetSelectedKeyID(), Distance);
	}

	GEngine->AddOnScreenDebugMessage(4, 1, FColor::Blue, TEXT("Ding"));
}
Ejemplo n.º 17
0
void UBTDecorator_BlueprintBase::OnCeaseRelevant(UBehaviorTreeComponent& OwnerComp, uint8* NodeMemory)
{
	UBlackboardComponent* BlackboardComp = OwnerComp.GetBlackboardComponent();
	if (BlackboardComp)
	{
		BlackboardComp->UnregisterObserversFrom(this);
	}
		
	if (AIOwner != nullptr && ReceiveObserverDeactivatedImplementations & FBTNodeBPImplementationHelper::AISpecific)
	{
		ReceiveObserverDeactivatedAI(AIOwner, AIOwner->GetPawn());
	}
	else if (ReceiveObserverDeactivatedImplementations & FBTNodeBPImplementationHelper::Generic)
	{
		ReceiveObserverDeactivated(ActorOwner);
	}

	if (GetNeedsTickForConditionChecking() == true && ReceiveTickImplementations == 0)
	{
		// clean up the tick request if no longer "active"
		bNotifyTick = false;
	}
}
EBTNodeResult::Type UBTTask_CloseDialogue::ExecuteTask(UBehaviorTreeComponent& OwnerComp, uint8* NodeMemory)
{
	EBTNodeResult::Type NodeResult = EBTNodeResult::Failed;

	if (!DialogueWidget.IsNone())
	{
		FName KeyName = DialogueWidget.SelectedKeyName;
		UBlackboardComponent* BlackboardComp = OwnerComp.GetBlackboardComponent();
		UUserWidget* Widget = Cast<UUserWidget>(BlackboardComp->GetValueAsObject(KeyName));
		UWidgetComponent* WidgetComp = Cast<UWidgetComponent>(BlackboardComp->GetValueAsObject(KeyName));

		if (!Widget && !WidgetComp)
		{
#if WITH_EDITOR
			FMessageLog("PIE").Error()
				->AddToken(FTextToken::Create(LOCTEXT("InvalidWidgetKey", "Invalid key for Dialogue Widget in ")))
				->AddToken(FUObjectToken::Create((UObject*)OwnerComp.GetCurrentTree()));
#endif
			return EBTNodeResult::Failed;
		}

		if (!Widget && WidgetComp)
		{
			Widget = CreateWidget<UUserWidget>(GetWorld(), WidgetComp->GetWidgetClass());
		}

		APlayerController* PlayerController = Widget->GetOwningPlayer();
		FInputModeGameOnly InputMode;
		PlayerController->SetInputMode(InputMode);

		if (Widget && Widget->IsInViewport())
		{
			Widget->RemoveFromParent();
		}

		switch (MouseOptions)
		{
		case ECloseDialogueCursorOptions::Show:
			PlayerController->bShowMouseCursor = true;
			break;
		default:
			PlayerController->bShowMouseCursor = false;
			break;
		}
	}
	// set camera default values
	if (!PlayerCamera.IsNone())
	{
		FName PlayerCameraKeyName = PlayerCamera.SelectedKeyName;
		UBlackboardComponent* BlackboardComp = OwnerComp.GetBlackboardComponent();
		UCameraComponent* PlayerCameraComp = Cast<UCameraComponent>(BlackboardComp->GetValueAsObject(PlayerCameraKeyName));

		if (PlayerCameraComp)
		{
			PlayerCameraComp->SetWorldLocationAndRotation(DefaultCameraLocation, DefaultCameraRotation);
			UBTTask_ShowPhrases* ShowPhrases = Cast<UBTTask_ShowPhrases>(FirstTaskNode);
			UBTTask_WaitAnswer* WaitAnswer = Cast<UBTTask_WaitAnswer>(FirstTaskNode);
			if (ShowPhrases)
			{
				ShowPhrases->DefaultCameraLocation = FVector(0.f, 0.f, 0.f);
			}
			if (WaitAnswer)
			{
				WaitAnswer->DefaultCameraLocation = FVector(0.f, 0.f, 0.f);
			}
		}
	}

	return NodeResult;
}
Ejemplo n.º 19
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)
	{
		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;
}