The powerup spawner in my game spawns a new powerup five seconds after someone has picked up the last one it spawned. Players are allowed to stand on the powerup’s spawnpoint and receive it immediately once the powerup has spawned. I am detecting when the powerup is picked up by subscribing to its OnDestroyed delegate since it is destroyed upon pickup (handled through OnComponentOverlapBegins). What seems to be happening is that if someone does indeed camp the spawnpoint, the collision will occur and OnDestroyed will be called before I can even subscribe to it in code.
Here’s the code in case I’m not being clear:
FActorSpawnParameters spawnParams;
spawnParams.bNoCollisionFail = true;
spawnParams.bNoFail = true;
spawnParams.Owner = this;
LastSpawnedPowerupActor = GetWorld()->SpawnActor(targetClass, &startPosition, &FRotator::ZeroRotator, spawnParams);
check(LastSpawnedPowerupActor != nullptr);
// at this point, the powerup has already been collided with so OnDestroyed has already been called
// so subscribing to the powerup will do nothing, assuming that the player is camping the powerup spawn
// subscribe to destruction of next object
if (ShouldRandomize)
{
LastSpawnedPowerupActor->OnDestroyed.AddDynamic(this, &APowerupSpawner::RandomizeType);
}
// start the respawn timer on pickup
LastSpawnedPowerupActor->OnDestroyed.AddDynamic(this, &APowerupSpawner::StartRespawnTimer);
// record new spawn
LastSpawnedPowerupType = QueuedPowerupType;
// announce spawn to subscribers
OnPowerupSpawned.Broadcast(LastSpawnedPowerupType);
I was thinking of having it not destroy itself immediately and allowing some time for the spawner to subscribe to the powerup, but does anyone have any other ideas?
EDIT: Looked more into AActor and found that ‘IsPendingKillPending()’ is something I could check immediately afterwards.