From 53053d46a5f8f8b58958fede40b1571ece6b98c2 Mon Sep 17 00:00:00 2001 From: baz Date: Wed, 23 Jul 2025 00:12:04 +0100 Subject: [PATCH] Add projectile lifespan to return object to pool if no collision --- Source/vampires/Projectile.cpp | 30 ++++++++++++++++++++---------- Source/vampires/Projectile.h | 9 +++++++++ 2 files changed, 29 insertions(+), 10 deletions(-) diff --git a/Source/vampires/Projectile.cpp b/Source/vampires/Projectile.cpp index 6fa06cd..8e6c98a 100644 --- a/Source/vampires/Projectile.cpp +++ b/Source/vampires/Projectile.cpp @@ -56,6 +56,11 @@ void AProjectile::SetActorHiddenInGame(bool bNewHidden) if (bNewHidden) { ResetData_Implementation(); + GetWorldTimerManager().ClearTimer(ProjectileLifetimeTimerHandle); + } + else + { + GetWorldTimerManager().SetTimer(ProjectileLifetimeTimerHandle, this, &AProjectile::ReturnProjectileToPool, ProjectileLifespan, true); } } @@ -111,17 +116,22 @@ void AProjectile::OnProjectileBeginOverlap(UPrimitiveComponent* OverlappedCompon if (RemainingDamagableEnemies == 0) { - AGameModeBase* gamemode = UGameplayStatics::GetGameMode(GetWorld()); - - if (UKismetSystemLibrary::DoesImplementInterface(gamemode, UPools::StaticClass())) - { - if (AObjectPoolManager* objectPoolManager = - IPools::Execute_GetProjectileObjectPoolManager(gamemode)) - { - objectPoolManager->ReturnObject(this); - } - } + ReturnProjectileToPool(); } } } } + +void AProjectile::ReturnProjectileToPool() +{ + AGameModeBase* gamemode = UGameplayStatics::GetGameMode(GetWorld()); + + if (UKismetSystemLibrary::DoesImplementInterface(gamemode, UPools::StaticClass())) + { + if (AObjectPoolManager* objectPoolManager = + IPools::Execute_GetProjectileObjectPoolManager(gamemode)) + { + objectPoolManager->ReturnObject(this); + } + } +} diff --git a/Source/vampires/Projectile.h b/Source/vampires/Projectile.h index 7fb754a..349880b 100644 --- a/Source/vampires/Projectile.h +++ b/Source/vampires/Projectile.h @@ -38,6 +38,12 @@ public: UPROPERTY(EditDefaultsOnly, BlueprintReadWrite) TObjectPtr NiagaraRibbonComponent = nullptr; + UPROPERTY(EditDefaultsOnly, BlueprintReadWrite) + float ProjectileLifespan = 15.0f; + +private: + FTimerHandle ProjectileLifetimeTimerHandle; + // Sets default values for this actor's properties AProjectile(); @@ -59,4 +65,7 @@ private: void OnProjectileBeginOverlap(UPrimitiveComponent* OverlappedComponent, AActor* OtherActor, UPrimitiveComponent* OtherComp, int32 OtherBodyIndex, bool bFromSweep, const FHitResult& SweepResult); + + UFUNCTION() + void ReturnProjectileToPool(); };