I’m making a system to replace actors at runtime, the actors are all blueprints that should be spawnable/undo/redoable in the editor.
When using Undo on blueprint actors derived from AActor where if the DefaultSceneRoot component is on the blueprint, the undo doesn’t work (the previous actor is empty and the component list just says [unnamed]).
Here’s my setup:
- Create a new blueprint class, derive from Actor in the /Content folder, named
BP_Cube
. - Put a static mesh with a cube as a child of
DefaultSceneRoot
component - Drag this actor in the world, name it “FindMe”.
- Spawn that actor inside a
BeginTransaction/EndTransaction
using c++ (Here’s my spawning code).
#if WITH_EDITOR
GEditor->BeginTransaction(NSLOCTEXT("ExampleTestForForums", "SwapMeshTransaction",
"SwapMesh"));
#endif
UWorld* World = GetWorld();
const UBlueprint* NewCube = LoadObject<UBlueprint>(this, TEXT("/Game/BP_Cube"));
TArray<AActor*> AllActors;
UGameplayStatics::GetAllActorsOfClass(World, AActor::StaticClass(), AllActors);
AActor* FindMeActor = nullptr;
for (AActor* Actor : AllActors)
{
if (Actor->GetActorLabel().Equals("FindMe"))
{
FindMeActor = Cast<AActor>(Actor);
FindMeActor->Modify();
}
}
// Replace the FindMeActor with the new one
AActor* NewCubeActor = World->SpawnActor<AActor>(NewCube->GeneratedClass, FindMeActor->GetActorTransform());
NewCubeActor->SetActorLabel(TEXT("FindMe"));
FindMeActor->Destroy();
#if WITH_EDITOR
GEditor->EndTransaction();
#endif
- Note, the old FindMe cube is gone, and a new one has taken its place.
- Undo.
- Notice the previous FindMe cube actor is now back, however the component reads
[unnamed]
and there is no mesh anymore for this actor.
This isn’t a problem if you make the StaticMeshComponent on BP_Cube the root component by dragging it on top of the DefaultSceneRoot, the original actor works great.
What am I doing wrong?
Thanks!