How can I duplicate the actor selected on the editor?

I’m trying to make a plugin so that when you have some actors selected and press a button, a new level is opened and the actors selected on the previous level get instantiated into this new level. I’ve been trying but right now I’m only able to instantiate an empty “StaticMeshActor” into the new level for each object I had selected, what am I missing?

void FRenderSceneModule::PluginButtonClicked()
{
	USelection* SelectedActors = GEditor->GetSelectedActors();

	if (!SelectedActors)
	{
		GEngine->AddOnScreenDebugMessage(-1, 15.0f, FColor::Red, "No Selected Actors");
	}

	TArray<AActor*> StoredActors;

	// Iterate through selected actors
	for (FSelectionIterator It(*SelectedActors); It; ++It)
	{
		AActor* Actor = Cast<AActor>(*It);

		if (Actor)
		{
			StoredActors.Add(Actor);
		}
	}

	FString MapPath = FPaths::ProjectContentDir() + "/StarterContent/Maps/Advanced_Lighting.umap";

	if (FEditorFileUtils::LoadMap(MapPath, true, true))
	{
		GEngine->AddOnScreenDebugMessage(-1, 15.0f, FColor::Green, "Map Loaded");
		UWorld* EditorWorld = GEditor->GetEditorWorldContext().World();

		for (int i = 0; i < StoredActors.Num(); i++)
		{
			///With this line I get an empty "StaticMeshActor" On the outliner
			AActor* SpawnedActor = EditorWorld->SpawnActor<AActor>(StoredActors[i]->GetClass(), FVector::ZeroVector, FRotator::ZeroRotator);

			if (SpawnedActor)
			{
				GEngine->AddOnScreenDebugMessage(-1, 15.0f, FColor::Green, "Actor spawned");
			}
		}
	}
}

Hi JayGlob, there a helpful method in UUnrealEdEngine to copy selected actors


and a method to paste the actors just copied

2 Likes

GEditor->edactCopySelected(GEditor->GetEditorWorldContext().World(), nullptr);
and
GEditor->edactPasteSelected(GEditor->GetEditorWorldContext().World(), false, false, true, nullptr);

Did exactly what I needed. Thanks!