Download

SpawnActor causing troubles

I have two actors, the first one spawns actors with a method (spawn()) i’ve created, when i call this method from inside the actor (for example in Tick()), it works perfectly. The second one contains a boxcomponent triggering an action when overlapped by something. In my case when something gets into the zone, it calls the method spawn() from my first actor, but unreal freezes everytime and stay frozen forever (no crash report).



/*This method works fine when called from ASpawnActor*/
void ASpawnActor::spawn() {
if (whatToSpawn != NULL) {
    UWorld* world = GetWorld();
    if (world) {
        FActorSpawnParameters params;
        params.Owner = this;
        params.bNoCollisionFail = true;
        FVector loc = GetActorLocation()+offset;
        FRotator rotation = FRotator::ZeroRotator;
        rotation.Yaw = 90.f;
        /*But crashe in the next line when called by  ASpawnerTrigger::OverlapStart()*/
        AActor *a=world->SpawnActor<AActor>(whatToSpawn, loc, rotation, params); 
    }
}
}


void ASpawnerTrigger::OverlapStart()
{

TArray<AActor*> ovAct;
TriggerBox->GetOverlappingActors(ovAct,ASpawnActor::StaticClass());
for (AActor* sa : ovAct) {
    ASpawnActor* spawner = Cast<ASpawnActor>(sa);
    if (spawner!=NULL) {
        spawner->spawn();
    }
  }
}


What is whatToSpawn and where is it set? Can’t see it in your code.

You might have inadvertently created an infinite loop with your game logic.

  1. An ASpawnActor overlaps ASpawnerTrigger which calls OverlapStart()
  2. OverlapStart calls spawner->spawn, which creates a new actor
  3. The new actor overlaps the trigger which calls OverlapStart
  4. (loop back to #2)

Could that be it?