0

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!!

  • Multiple like 10, or like 10 million? – Rotem Dec 01 '20 at 18:54
  • What does the Beam actor do? and just how many of an item are we talking about spawning and destroying? – Eric Blade Dec 08 '20 at 03:58
  • If you're using vs, then you could pause execution and debug from there. Freezing indicates something is holding up the main thread. It may be an infinite loop of some sort, or you may just be executing a horrendously slow operation. – George Dec 08 '20 at 22:56
  • The beam is just a cylinder that acts as a visual for a laser beam, it spawns like 1 every 0.1 seconds then deletes it 0.1 seconds later. – Jacob Maidens Dec 09 '20 at 05:28

1 Answers1

1

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.

DevilsD
  • 545
  • 1
  • 5
  • 12