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- 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:
but this works only in C++. The “Template” parameter doesn’t seam to be exposed to SpawnActorFromClass node…
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.
I’ve considered structures, but this option is very limited. Placing 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 parameters creates a spaghetti in Blueprint which have to be maintained after any changes to a set of parameters that Actor needs to have.
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:
In the editor go on File/Add C++Class and select “Blueprint Function Library” from the menu.
Rename it and click on “Create Class”
…] wait compiler and Visual Studio
Open the file NAMEOFYOURLIBRARY.h (if you can’t find it in Visual Studio look in the Source folder of your project)
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);
};
Find and open the NAMEOFYOURLIBRARY.cpp fine (same of point 3)
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;
}
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.
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 ?
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.
Does anyone use this within a world partition map?
The Clone Actor works great in a normal map.
With world partition enabled I can clone an actor too, but as soon as I save the map and open it again, the actor disappears:
Saving dialog
Empty Package - that’s wrong it should be Type Blueprint like the original actor:
After saving and opening the map again, the cloned actor just has vanished.
Any ideas?
edit:
I can workaround this by using the Duplicate Actor function of the Editor Actor Subsystem and deleting the the cloned actor afterwards. Not a nice solution, but it does the job.