Hello!
I have an actor (a c++ class) which places random stuff to a map.
In construction method I call World->SpawnActor
to generate objects. Than, I store pointers of the generated objects in an array so that I can remove them whenever construction method called again:
void ALevelGeneratorActor::OnConstruction(const FTransform& Transform)
{
DeleteOldActors();
SpawnNewActors();
}
void ALevelGeneratorActor::SpawnNewActors()
{
...
SpawnedActor* Actor = World->SpawnActor<SpawnedActor>(...);
SpawnedActors.Add(Actor);
...
}
void ALevelGeneratorActor::DeleteOldActors()
{
for (auto* Actor : SpawnedActors) {
Actor->Destroy();
}
SpawnedActors.Empty();
}
This works fine while editing and playing.
However, when I save the project and than reload a map, generated objects are still present while SpawnedActors
array is empty. Thus, running construction script again will not destroy old actors.
Now, I have feeling that I do it wrong. The question is, how do I spawn random actors at editing time ritht and/or how do I reconstruct SpawnedActors
array after level reload?
(note that I cant spawn those actors in OnBeginPlay
because those spawned actors affect navmesh and lighting so I need them in editor).