SpawnActor before BeginPlay?

I need to be able to spawn actors from a component that will show up in the editor.

I have a base c++ class that is inherited by a blueprint object. During the Construction phase of the blueprint, it calls the ‘GenerateLayout’ function within my c++ base class. In my ‘GenerateLayout’ function i need to spawn a number of actors. However using ‘GetWorld()->SpawnActor’ does not work, It always returns null.

Is there anyway to spawn actors before ‘BeginPlay’?

make sure that you set the bAllowDuringConstructionScript to true in your FActorSpawnParameters object.

Example:

auto spawnParams = FActorSpawnParameters();
spawnParams.Name = TEXT("Room");
spawnParams.bNoCollisionFail = true;
spawnParams.ObjectFlags = EObjectFlags::RF_Native;
spawnParams.Owner = this;
spawnParams.OverrideLevel = GetWorld()->GetCurrentLevel();
spawnParams.bAllowDuringConstructionScript = true;

auto room = (ARoom*) GetWorld()->SpawnActor<ARoom>(ARoom::StaticClass(), FVector(0), FRotator(0), spawnParams);