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()
{
Super::BeginPlay();
}
// Called every frame
@ -54,31 +53,7 @@ float AAICharacter::TakeDamage(const float DamageAmount, FDamageEvent const& Dam
CurrentHealth = 0;
UE_LOG(LogTemp, Display, TEXT("%s is dead"), *CharacterName.ToString());
/*const AAI_EnemyController* AIController = Cast<AAI_EnemyController>(GetController());
AIController->GetBrainComponent()->StopLogic(" is dead");*/
this->Tags.Add(FName("Dead"));
//Ragdoll
DetachFromControllerPendingDestroy();
UCapsuleComponent* CapsuleComp = GetCapsuleComponent();
CapsuleComp->SetCollisionEnabled(ECollisionEnabled::NoCollision);
CapsuleComp->SetCollisionResponseToAllChannels(ECR_Ignore);
GetMesh()->SetCollisionProfileName(TEXT("Ragdoll"));
SetActorEnableCollision(true);
GetMesh()->SetAllBodiesSimulatePhysics(true);
GetMesh()->SetSimulatePhysics(true);
GetMesh()->WakeAllRigidBodies();
GetMesh()->bBlendPhysics = true;
if (UCharacterMovementComponent* CharacterComp = Cast<UCharacterMovementComponent>(GetMovementComponent()))
{
CharacterComp->StopMovementImmediately();
CharacterComp->DisableMovement();
CharacterComp->SetComponentTickEnabled(false);
}
SetLifeSpan(30.0f);
OnDeath();
}
return Super::TakeDamage(DamageAmount, DamageEvent, EventInstigator, DamageCauser);
}
@ -98,3 +73,31 @@ void AAICharacter::SetupStimuliSourceComponent()
}
}
void AAICharacter::OnDeath()
{
/*const AAI_EnemyController* AIController = Cast<AAI_EnemyController>(GetController());
AIController->GetBrainComponent()->StopLogic(" is dead");*/
this->Tags.Add(FName("Dead"));
//Ragdoll
DetachFromControllerPendingDestroy();
UCapsuleComponent* CapsuleComp = GetCapsuleComponent();
CapsuleComp->SetCollisionEnabled(ECollisionEnabled::NoCollision);
CapsuleComp->SetCollisionResponseToAllChannels(ECR_Ignore);
GetMesh()->SetCollisionProfileName(TEXT("Ragdoll"));
SetActorEnableCollision(true);
GetMesh()->SetAllBodiesSimulatePhysics(true);
GetMesh()->SetSimulatePhysics(true);
GetMesh()->WakeAllRigidBodies();
GetMesh()->bBlendPhysics = true;
if (UCharacterMovementComponent* CharacterComp = Cast<UCharacterMovementComponent>(GetMovementComponent()))
{
CharacterComp->StopMovementImmediately();
CharacterComp->DisableMovement();
CharacterComp->SetComponentTickEnabled(false);
}
SetLifeSpan(30.0f);
}

View File

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

View File

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

View File

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

View File

@ -23,6 +23,16 @@ void AEnemyCharacter::BeginPlay()
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
void AEnemyCharacter::Tick(float DeltaTime)
{
@ -38,6 +48,8 @@ void AEnemyCharacter::SubscribeToGroupAIEvents(AAIControlHub* ControlHub)
void AEnemyCharacter::SetAlertLevel(const int NewAlertLevel) const
{
if (!IsValid(this)) return;
if (!IsValid(GetController())) return;
Cast<AAIController>(GetController())->GetBlackboardComponent()->SetValueAsInt("AlertLevel", NewAlertLevel);
}
@ -49,6 +61,8 @@ void AEnemyCharacter::SetLocalAlertLevel(int NewAlertLevel) const
void AEnemyCharacter::HuntPlayer(FVector PlayerLastKnownLocation)
{
if (!IsValid(this)) return;
if (!IsValid(GetController())) return;
SetAlertLevel(2);
Cast<AAIController>(GetController())->GetBlackboardComponent()->SetValueAsVector("LastKnownLocation", PlayerLastKnownLocation);
}

View File

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