Add projectile lifespan to return object to pool if no collision

This commit is contained in:
baz 2025-07-23 00:12:04 +01:00
parent 25efc64ebd
commit 53053d46a5
2 changed files with 29 additions and 10 deletions

View File

@ -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);
}
}
}

View File

@ -38,6 +38,12 @@ public:
UPROPERTY(EditDefaultsOnly, BlueprintReadWrite)
TObjectPtr<UNiagaraComponent> 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();
};