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) if (bNewHidden)
{ {
ResetData_Implementation(); ResetData_Implementation();
GetWorldTimerManager().ClearTimer(ProjectileLifetimeTimerHandle);
}
else
{
GetWorldTimerManager().SetTimer(ProjectileLifetimeTimerHandle, this, &AProjectile::ReturnProjectileToPool, ProjectileLifespan, true);
} }
} }
@ -111,6 +116,14 @@ void AProjectile::OnProjectileBeginOverlap(UPrimitiveComponent* OverlappedCompon
if (RemainingDamagableEnemies == 0) if (RemainingDamagableEnemies == 0)
{ {
ReturnProjectileToPool();
}
}
}
}
void AProjectile::ReturnProjectileToPool()
{
AGameModeBase* gamemode = UGameplayStatics::GetGameMode(GetWorld()); AGameModeBase* gamemode = UGameplayStatics::GetGameMode(GetWorld());
if (UKismetSystemLibrary::DoesImplementInterface(gamemode, UPools::StaticClass())) if (UKismetSystemLibrary::DoesImplementInterface(gamemode, UPools::StaticClass()))
@ -121,7 +134,4 @@ void AProjectile::OnProjectileBeginOverlap(UPrimitiveComponent* OverlappedCompon
objectPoolManager->ReturnObject(this); objectPoolManager->ReturnObject(this);
} }
} }
}
}
}
} }

View File

@ -38,6 +38,12 @@ public:
UPROPERTY(EditDefaultsOnly, BlueprintReadWrite) UPROPERTY(EditDefaultsOnly, BlueprintReadWrite)
TObjectPtr<UNiagaraComponent> NiagaraRibbonComponent = nullptr; TObjectPtr<UNiagaraComponent> NiagaraRibbonComponent = nullptr;
UPROPERTY(EditDefaultsOnly, BlueprintReadWrite)
float ProjectileLifespan = 15.0f;
private:
FTimerHandle ProjectileLifetimeTimerHandle;
// Sets default values for this actor's properties // Sets default values for this actor's properties
AProjectile(); AProjectile();
@ -59,4 +65,7 @@ private:
void OnProjectileBeginOverlap(UPrimitiveComponent* OverlappedComponent, AActor* OtherActor, void OnProjectileBeginOverlap(UPrimitiveComponent* OverlappedComponent, AActor* OtherActor,
UPrimitiveComponent* OtherComp, int32 OtherBodyIndex, bool bFromSweep, UPrimitiveComponent* OtherComp, int32 OtherBodyIndex, bool bFromSweep,
const FHitResult& SweepResult); const FHitResult& SweepResult);
UFUNCTION()
void ReturnProjectileToPool();
}; };