From f84a711b4fddfaaab37e1e3a7aaa469a4649f97e Mon Sep 17 00:00:00 2001 From: Caleb Buhungiro Date: Sun, 14 Sep 2025 23:22:37 +0800 Subject: [PATCH] setup AI Perception and forgatting behavior from Blackboard --- Config/DefaultEngine.ini | 3 +++ Content/Maps/GameLevel.umap | 2 +- Source/Crunch/Private/AI/CAIController.cpp | 31 ++++++++++++++++------ Source/Crunch/Public/AI/CAIController.h | 3 +++ 4 files changed, 30 insertions(+), 9 deletions(-) diff --git a/Config/DefaultEngine.ini b/Config/DefaultEngine.ini index 2d98a8d..6d7e948 100644 --- a/Config/DefaultEngine.ini +++ b/Config/DefaultEngine.ini @@ -102,3 +102,6 @@ CategorySlot7=Seven CategorySlot8=Eight CategorySlot9=Nine +[/Script/AIModule.AISystem] +bForgetStaleActors=True + diff --git a/Content/Maps/GameLevel.umap b/Content/Maps/GameLevel.umap index 7032e57..5f34360 100644 --- a/Content/Maps/GameLevel.umap +++ b/Content/Maps/GameLevel.umap @@ -1,3 +1,3 @@ version https://git-lfs.github.com/spec/v1 -oid sha256:02ccadf71b7c79c2b6749152d7e3e259b8d622bf327cdbb19e6727d8b719cd20 +oid sha256:e38df942e70c04976ec4fdfbc60ee8437d3460fe94e3b9e623499234fdabbbff size 15358735 diff --git a/Source/Crunch/Private/AI/CAIController.cpp b/Source/Crunch/Private/AI/CAIController.cpp index 4f12204..9523d34 100644 --- a/Source/Crunch/Private/AI/CAIController.cpp +++ b/Source/Crunch/Private/AI/CAIController.cpp @@ -26,6 +26,7 @@ ACAIController::ACAIController() AIPerceptionComponent->ConfigureSense(*SightConfig); AIPerceptionComponent->OnTargetPerceptionUpdated.AddDynamic(this, &ThisClass::TargetPerceptionUpdated); + AIPerceptionComponent->OnTargetPerceptionForgotten.AddDynamic(this, &ACAIController::TargetForgotten); } void ACAIController::OnPossess(APawn* InPawn) @@ -52,18 +53,22 @@ void ACAIController::Tick(float DeltaTime) void ACAIController::TargetPerceptionUpdated(AActor* TargetActor, FAIStimulus Stimulus) { - UE_LOG(LogTemp, Warning, TEXT("Saw you")) - // GEngine->AddOnScreenDebugMessage( - // -1, 12.f, FColor::Blue, - // FString::Printf(TEXT("saw you"))); - if (Stimulus.WasSuccessfullySensed()) { if (!GetCurrentTarget()) SetCurrentTarget(TargetActor); } else { - if (GetCurrentTarget() == TargetActor) SetCurrentTarget(nullptr); + } +} + +void ACAIController::TargetForgotten(AActor* ForgottenActor) +{ + if (!ForgottenActor) return; + + if (GetCurrentTarget() == ForgottenActor) + { + SetCurrentTarget(GetNextPerceivedActor()); } } @@ -71,8 +76,7 @@ const UObject* ACAIController::GetCurrentTarget() const { if (const auto BlackboardComponent{GetBlackboardComponent()}) { - const auto Target{BlackboardComponent->GetValueAsObject(BlackboardTargetName)}; - return Target; + return BlackboardComponent->GetValueAsObject(BlackboardTargetName); } return nullptr; } @@ -85,3 +89,14 @@ void ACAIController::SetCurrentTarget(AActor* NewTarget) if (NewTarget) BlackboardComponent->SetValueAsObject(BlackboardTargetName, NewTarget); else BlackboardComponent->ClearValue(BlackboardTargetName); } + +AActor* ACAIController::GetNextPerceivedActor() const +{ + if (PerceptionComponent) + { + TArray Actors; + AIPerceptionComponent->GetPerceivedHostileActors(Actors); + if (Actors.Num() != 0) return Actors[0]; + } + return nullptr; +} diff --git a/Source/Crunch/Public/AI/CAIController.h b/Source/Crunch/Public/AI/CAIController.h index a0d4b9b..8d203f9 100644 --- a/Source/Crunch/Public/AI/CAIController.h +++ b/Source/Crunch/Public/AI/CAIController.h @@ -28,6 +28,8 @@ private: FName BlackboardTargetName {"Target"}; UFUNCTION() void TargetPerceptionUpdated(AActor* TargetActor, FAIStimulus Stimulus); + UFUNCTION() + void TargetForgotten(AActor* ForgottenActor); UPROPERTY(EditDefaultsOnly, Category="AI Behavior") UBehaviorTree* BehaviorTree; @@ -40,4 +42,5 @@ private: UFUNCTION() const UObject* GetCurrentTarget() const ; void SetCurrentTarget(AActor* NewTarget); + AActor * GetNextPerceivedActor() const; };