Clone/Copy/Duplicate actor using blueprints?

Sorry from necroposting but Google’s results for “Clone Actor Blueprint” points here so the answer is more useful here.

Is true you can’t clone an actor in runtime using blueprint but isn’t too hard to create a custom blueprint who do this work:

  1. In the editor go on File/Add C++Class and select “Blueprint Function Library” from the menu.

  2. Rename it and click on “Create Class”

…] wait compiler and Visual Studio

  1. Open the file NAMEOFYOURLIBRARY.h (if you can’t find it in Visual Studio look in the Source folder of your project)

  2. Copy this code:


#pragma once
#include "CoreMinimal.h"
#include "Kismet/BlueprintFunctionLibrary.h"
#include "NAMEOFYOURLIBRARY.generated.h"

UCLASS()
class NAMEOFYOURPROJECT_API UNAMEOFYOURLIBRARY : public UBlueprintFunctionLibrary // Note: replace the project name and put a U before the name of your library
{
    GENERATED_BODY()

        UFUNCTION(BlueprintCallable, Category = "ActorFuncions", meta = (WorldContext = WorldContextObject))
        static AActor* CloneActor(AActor* InputActor);

};

  1. Find and open the NAMEOFYOURLIBRARY.cpp fine (same of point 3)

  2. Copy this code:


#include "NAMEOFYOURLIBRARY.h"
#include "Runtime/Engine/Classes/Engine/World.h"

AActor* UNAMEOFYOURLIBRARY::CloneActor(AActor* InputActor) // Note: put a U before the name of your library
{
    UWorld * World = InputActor->GetWorld();
    UE_LOG(LogTemp, Log, TEXT("Actor Duplication"));
    FActorSpawnParameters params;
    params.Template = InputActor;

    UClass * ItemClass = InputActor->GetClass();
    AActor* const SpawnedActor = World->SpawnActor<AActor>(ItemClass, params);
    return SpawnedActor;
}

  1. Compile (from the solutions at the right, or from the compiling menu in the menu bar or MAYBE from the editor, not 100% sure)

NOTE: replace NAMEOFYOURLIBRARY with the name you give to your library in point 2, where there’s UNAMEOFYOURLIBRARY put a U in front of it. Ex: MyCPPLib -> UMyCPPLib

You can now call anywhere a node called “Clone Actor” who take in input an actor and clone it (giving you the reference to the cloned actor in output).

P.S: I’m not a C++ programmer, I only need that node and I HAD to find a way, and I wanted tho share the solution.

5 Likes