Bugfix Crashing After an Enemy is Killed

Handled Death in AI Control Hub
This commit is contained in:
Philip W 2023-10-13 15:47:25 +01:00
parent 280644da78
commit 1746dad55c
6 changed files with 52 additions and 26 deletions

View File

@ -25,7 +25,6 @@ AAICharacter::AAICharacter()
void AAICharacter::BeginPlay() void AAICharacter::BeginPlay()
{ {
Super::BeginPlay(); Super::BeginPlay();
} }
// Called every frame // Called every frame
@ -54,6 +53,28 @@ float AAICharacter::TakeDamage(const float DamageAmount, FDamageEvent const& Dam
CurrentHealth = 0; CurrentHealth = 0;
UE_LOG(LogTemp, Display, TEXT("%s is dead"), *CharacterName.ToString()); UE_LOG(LogTemp, Display, TEXT("%s is dead"), *CharacterName.ToString());
OnDeath();
}
return Super::TakeDamage(DamageAmount, DamageEvent, EventInstigator, DamageCauser);
}
APatrolPath* AAICharacter::GetPatrolPath() const
{
return PatrolPath;
}
void AAICharacter::SetupStimuliSourceComponent()
{
StimuliSourceComponent = CreateDefaultSubobject<UAIPerceptionStimuliSourceComponent>(TEXT("Stimuli Source Component"));
if (IsValid(StimuliSourceComponent))
{
StimuliSourceComponent->RegisterForSense(TSubclassOf<UAISense_Sight>());
StimuliSourceComponent->RegisterWithPerceptionSystem();
}
}
void AAICharacter::OnDeath()
{
/*const AAI_EnemyController* AIController = Cast<AAI_EnemyController>(GetController()); /*const AAI_EnemyController* AIController = Cast<AAI_EnemyController>(GetController());
AIController->GetBrainComponent()->StopLogic(" is dead");*/ AIController->GetBrainComponent()->StopLogic(" is dead");*/
this->Tags.Add(FName("Dead")); this->Tags.Add(FName("Dead"));
@ -80,21 +101,3 @@ float AAICharacter::TakeDamage(const float DamageAmount, FDamageEvent const& Dam
SetLifeSpan(30.0f); SetLifeSpan(30.0f);
} }
return Super::TakeDamage(DamageAmount, DamageEvent, EventInstigator, DamageCauser);
}
APatrolPath* AAICharacter::GetPatrolPath() const
{
return PatrolPath;
}
void AAICharacter::SetupStimuliSourceComponent()
{
StimuliSourceComponent = CreateDefaultSubobject<UAIPerceptionStimuliSourceComponent>(TEXT("Stimuli Source Component"));
if (IsValid(StimuliSourceComponent))
{
StimuliSourceComponent->RegisterForSense(TSubclassOf<UAISense_Sight>());
StimuliSourceComponent->RegisterWithPerceptionSystem();
}
}

View File

@ -35,6 +35,8 @@ protected:
class UAIPerceptionStimuliSourceComponent* StimuliSourceComponent; class UAIPerceptionStimuliSourceComponent* StimuliSourceComponent;
void SetupStimuliSourceComponent(); void SetupStimuliSourceComponent();
virtual void OnDeath();
public: public:
// Called every frame // Called every frame
virtual void Tick(float DeltaTime) override; virtual void Tick(float DeltaTime) override;

View File

@ -74,3 +74,8 @@ void AAIControlHub::SetPlayerLastKnownLocation(FVector Location)
PlayerLastKnownLocation = Location; PlayerLastKnownLocation = Location;
} }
} }
void AAIControlHub::RemoveEnemyActor(AEnemyCharacter* EnemyActor)
{
EnemyActors.Remove(EnemyActor);
}

View File

@ -35,6 +35,7 @@ public:
int GetAlertLevel() const; int GetAlertLevel() const;
void OnAlertLevelChanged(); void OnAlertLevelChanged();
void SetPlayerLastKnownLocation(FVector Location = FVector(0, 0, 0)); void SetPlayerLastKnownLocation(FVector Location = FVector(0, 0, 0));
void RemoveEnemyActor(AEnemyCharacter* EnemyActor);
private: private:
int AlertLevel = 0; int AlertLevel = 0;

View File

@ -23,6 +23,16 @@ void AEnemyCharacter::BeginPlay()
CharacterName = "Enemy"; CharacterName = "Enemy";
} }
void AEnemyCharacter::OnDeath()
{
Super::OnDeath();
DelegatedControlHub->AlertLevelEvent.Remove(AlertLevelDelegateHandle);
DelegatedControlHub->HuntPlayerEvent.Remove(HuntPlayerDelegateHandle);
AlertLevelDelegateHandle.Reset();
HuntPlayerDelegateHandle.Reset();
DelegatedControlHub->RemoveEnemyActor(this);
}
// Called every frame // Called every frame
void AEnemyCharacter::Tick(float DeltaTime) void AEnemyCharacter::Tick(float DeltaTime)
{ {
@ -38,6 +48,8 @@ void AEnemyCharacter::SubscribeToGroupAIEvents(AAIControlHub* ControlHub)
void AEnemyCharacter::SetAlertLevel(const int NewAlertLevel) const void AEnemyCharacter::SetAlertLevel(const int NewAlertLevel) const
{ {
if (!IsValid(this)) return;
if (!IsValid(GetController())) return;
Cast<AAIController>(GetController())->GetBlackboardComponent()->SetValueAsInt("AlertLevel", NewAlertLevel); Cast<AAIController>(GetController())->GetBlackboardComponent()->SetValueAsInt("AlertLevel", NewAlertLevel);
} }
@ -49,6 +61,8 @@ void AEnemyCharacter::SetLocalAlertLevel(int NewAlertLevel) const
void AEnemyCharacter::HuntPlayer(FVector PlayerLastKnownLocation) void AEnemyCharacter::HuntPlayer(FVector PlayerLastKnownLocation)
{ {
if (!IsValid(this)) return;
if (!IsValid(GetController())) return;
SetAlertLevel(2); SetAlertLevel(2);
Cast<AAIController>(GetController())->GetBlackboardComponent()->SetValueAsVector("LastKnownLocation", PlayerLastKnownLocation); Cast<AAIController>(GetController())->GetBlackboardComponent()->SetValueAsVector("LastKnownLocation", PlayerLastKnownLocation);
} }

View File

@ -21,6 +21,7 @@ protected:
// Called when the game starts or when spawned // Called when the game starts or when spawned
virtual void BeginPlay() override; virtual void BeginPlay() override;
class AAIControlHub* DelegatedControlHub; class AAIControlHub* DelegatedControlHub;
virtual void OnDeath() override;
public: public:
// Called every frame // Called every frame