Jacob Maidens
Jacob Maidens

Reputation: 1

Rapidly spawning / destroying actors in UE4

I have created a mirror system which rapidly spawns and destroys actors however whenever i try to spawn multiple at once and then destroy them soon after, my engine completely freezes up and i have to close it via task manager.

Heres my code for iterating through the array of pointers to spawned actors

 for (int index = 0; index < Lasers.Num(); index++)
     {
         GEngine->AddOnScreenDebugMessage(index, 30.f, FColor::Red, FString(Lasers[index]->GetName()));
         if (!Lasers[index]) return;
         if (!Lasers[index]->IsValidLowLevel()) return;
 
         Lasers[index]->K2_DestroyActor();
         Lasers[index] = NULL;
 
     }
     Lasers.Empty();

Heres how i am spawning actors and adding them to the TArray

 Beamref = GetWorld()->SpawnActor<ABeam>(Location, Rotation, SpawnInfo);
 if (Beamref != nullptr)
 {
     Cast<ABeam>(Beamref)->SetEnds(LaserStart, LaserEnd); UE_LOG(LogTemp, Warning, TEXT("Spawned"));
     Lasers.Add(Beamref); UE_LOG(LogTemp, Warning, TEXT("Added"));
 }

The array is a simple TArray

 TArray<ABeam*> Lasers;

Any help is greatly appreciated!!

Upvotes: 0

Views: 1508

Answers (1)

DevilsD
DevilsD

Reputation: 607

Please call AActor::Destroy() to destroy an Actor.

K2_DestroyActor() is the Blueprint exposed event that this function calls so that Blueprint code can respond to an Actor being destroyed.

Upvotes: 1

Related Questions