I need to essentially clone actors in runtime (passing on all variable data primarily). I am aware this isn’t possible via blueprints, and my C++ knowledge is waning here. I have done research and determined that SpawnActor via C++ takes in a parameter called Template, that will copy an input actor’s properties onto the newly spawned actor. I am looking at creating a blueprint function library here, and to create a node that will do this for me.
My blueprint facilitates many different actors. It would be extremely tedious and time consuming to pass every variable, for every individual class. It’s connected to a data table with other 50 actors with their own unique properties that I don’t feel like casting too lol
That is not the issue here, the blueprint is very modular. What you are suggesting is manually passing variables over to another instance of the same actor, which sounds pretty static. A modular blueprint is one that can pass all variables of any actor, which is what I’m trying to accomplish.
For anyone who has this similiar problem, here is the code:
UCLASS()
class YOUR_API UBlueprintLibrary_CloneActor : public UBlueprintFunctionLibrary
{
GENERATED_BODY()
public:
UFUNCTION(BlueprintCallable, Category = "Cloning")
static AActor* CloneActor(AActor* OriginalActor)
{
if (!OriginalActor) return nullptr;
// Create a new instance of the actor
AActor* NewActor = UGameplayStatics::BeginDeferredActorSpawnFromClass(
OriginalActor->GetClass(), OriginalActor->GetTransform());
// Copy over the properties and components from the original actor
NewActor->CopyRuntimeProperties(OriginalActor);
TArray<UActorComponent*> Components;
OriginalActor->GetComponents(Components);
for (auto Component : Components)
{
UActorComponent* NewComponent = NewActor->AddComponent(Component->GetClass());
NewComponent->CopyPropertiesFromComponent(Component);
}
// Finish spawning the new actor
UGameplayStatics::FinishSpawningActor(NewActor, OriginalActor->GetTransform());
return NewActor;
}
};
Thanks a bunch for posting your solution OP, exactly what I was looking for.
For anyone that’s actually considering this option, don’t. It’s a very bad idea, one that’s going to create a cascade of headaches for yourself in the future any time you need to modify that base actor.