Download

My object pooler instantiates my entities but my spawn function only works once

I’ve been stuck on this issue for a while so here’s what’s happening, I load all my enemies into a pool which happens perfectly ok but every time spawn is called it works fine but it seems that enemies within the first 2 seconds actually become active but the rest don’t, almost like they’ve been deleted from the scene.

The tough issue is that this code did work fine for me before and spawned incorrectly but I’m not sure when it began to behave like this. I’ve noticed in the editor that characters will be spawned in then deleted which may be responsible. Sorry if I’m quite vague I’m really not 100% what’s going on. Here’s what I do -

I begin by loading all my enemy entities into my pool



void UObject_Pool::BeginPlay()
{

Super::BeginPlay();

if ( PooledObjectSubclass != NULL ) {

  UWorld* const World = GetWorld();

    if ( World ) {

      for (int i = 0; i < PoolSize; i++) {

        AEnemyCharacter* PoolableActor = GetWorld()->SpawnActor<AEnemyCharacter>(PooledObjectSubclass, FVector().ZeroVector, FRotator().ZeroRotator);

        PoolableActor->SetActive(false);

        Pool.Add(PoolableActor);

      UE_LOG(LogTemp, Warning, TEXT("Item Added"));
      }
    }
}


And then I spawn them in my scene by calling SetTimer() on BeginPlay which calls my spawn function below which reruns itself with a timer each call -



void ASpawnEnemies::Spawn()
{
  AEnemyCharacter* PoolableActor = ObjectPooler->GetPooledObject();

    if (PoolableActor == nullptr)
    {
      UE_LOG(LogTemp, Warning, TEXT("Cannot spawn actor"));

      GetWorldTimerManager().SetTimer(SpawnCooldownTimer, this, &ASpawnEnemies::Spawn, SpawnCooldown, false);

      return;
    }

if (GetWorld()->GetFirstPlayerController()->GetPawn()) {

  PoolableActor->SetActorLocation(SpawnLocation);

  PoolableActor->SetActive(true);

  PoolableActor->SetActorRotation(GetWorld()->GetFirstPlayerController()->GetPawn()->GetActorRotation());

  PoolableActor->CanBeHit = true;

  UE_LOG(LogTemp, Warning, TEXT("Spawned"));

  GetWorldTimerManager().SetTimer(SpawnCooldownTimer, this, &ASpawnEnemies::Spawn, SpawnCooldown, false);

  }

}


And finally here’s some functions I use to activate and deactivate the entities



void AEnemyCharacter::SetActive(bool InActive)
{
    Active = InActive;
    SetActorHiddenInGame(!InActive);
}

bool AEnemyCharacter::IsActive()
{
    return Active;
}