Download

Clone/Copy/Duplicate actor using blueprints?

Is there are a way to make a copy of an actor using a blueprint?
Spawning a new actor and copying parameters one by one from another actor is a tedious and error prone approach. Simply because “copy_parameters” part of blueprint have to be adjusted every-time I make changes to blueprint variables.
The same issue is with exposing them to SpawnActor function as they had to be linked one by one.

I found this post:
https://answers.unrealengine.com/questions/136562/is-there-a-way-to-clone-duplicate-an-actor-in-c.html
but this works only in C++. The “Template” parameter doesn’t seam to be exposed to SpawnActorFromClass node…

Am I missing something?

Have you tried, I think it is called, Make a copy of this, and it makes a child BP.

Edit: Have you thought about making custom events or interfaces, or would that not work?

Narg

Nope, don’t see anything like that in blueprint editor.

Unfortunately, interfaces won’t really help here.

It’s weird that template parameter is not exposed to us.

Make a base BP, Right click on it in content browser and the choice at the top is “Create a Blueprint based on this”

1 child BP

Narg

I don’t want to copy a blueprint - I want to copy an actor using blueprints.

Can you give a little more detail about what you are trying to achieve. Just to help us along a little.

There may be a completely different way around if we had a little more detail.

Narg

I have an actor somewhere in the level, with some specific set of properties. I want to make a copy of that actor, with exactly the same properties. I can’t just copy/paste it in the editor as it needs to be done dynamically.
In C++ you can do this: “SpawnActor has a parameter named Template. If you pass an actor in that, the newly spawned actor will copy the properties from the Template.”. But this parameter is not exposed in “Spawn Actor from Class” node, so I’m looking at how else it can be done.

Hello,
Maybe a structure is what you need : https://forums.unrealengine.com/showthread.php?57809-How-do-Blueprint-Structures-actually-work .

I’ve considered structures, but this option is very limited. Placing all of the variables of a blueprint into a structure limits the possibility of inheriting this blueprint as any new parameters had to be placed in some extra structure.
Adding a structure just to pass all parameters creates a spaghetti in Blueprint which have to be maintained after any changes to a set of parameters that Actor needs to have.

Well maybe is C++ is the only way.

sometimes, BP are ugly. But with comments and collapsed nodes. It can be easy to follow.

Narg

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 time

  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.

Thank you for sharing!

Hi,
I’m not sure if it’s the same question asked in this post, but I appreciate any help.
i have one actor and a target point. What I want to do is, when I provide a new target point, I want to duplicate my actor in the new target position with the same properties . I am going to create many of these duplicates. My actor is a sphere mask, and has simple material attributes. Is there any way to do such a thing in either blueprints or c++? This actor is going to be static actor with no movement.
i can’t predefine these actors as my target points are random

@Iohoilpotere - I tried your interesting code and, although it compiled in visual studio (2017) without a complaint (alongside a couple of other functions in my functionlibrary) I am afraid that when I ran it as a blueprint, it crashed the engine 4.22 editor. I fed it pawns and simple actors but nothing made it happy. I appreciate that you make no claims to be a programmer but I wonder if you would have any clues at all?

Nice function. I’m going to suggest one improvement to the above line:



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

I removed the WorldContext, as you don’t need that since you’re not using it, and instead added DeterminesOutputType, which means that the output of the node will be the same type as the input, and so you won’t have to cast it after cloning.

This is a great improvement!