Is there a way to clone / duplicate an Actor in C++?

Hello, i’m trying to create a copy of an actor in C++, is that possible without setting all the values manually? Thanks :slight_smile:

SpawnActor has a parameter named Template. If you pass an actor in that, the newly spawned actor will copy the properties from the Template. See FActorSpawnParameters for documentation.

Thank you for the answer :slight_smile: Tell me something, does that only copy variables that are set as UPROPERTY?

Yeah, it can only know about the ones that are UPROPs since those are known via the reflection system

I apologize for another question, but i’m not able to replicate Cloned items, they only show in the server, could it be that he copies the same netguid or something like that? Here is how i’m cloning:

class AItem * AItem::Clone()
{
	FActorSpawnParameters Parameters;

	Parameters.Template = this;

	class AItem * Item = GetWorld()->SpawnActor<class AItem>(GetClass(), Parameters);

	Item->SetOwner(GetOwner());

	return Item;
}

If i comment Parameters.Template = this; it replicates well.

Any suggestions? :slight_smile:

That sounds like a bug. I’ll get that filed

Thank you, how can i keep track of the progress of the fix?

If the original actor is replicating, it may be an issue of the new actor not getting put on the replication list inside SpawnActor.

You can try two fixes.

Add this to end of UWorld::SpawnActor:

if (Actor->RemoteRole != ROLE_None)
{
	AddNetworkActor(this);
}

Or after your SpawnActor call, call SetReplicates with false and then true (more of a temp fix).

We can look into adding the first (more long term fix) if this is your issue. We’re definitely interested if the first option fixes your issue (as this will let us know if we have an internal issue, please do let us know!)

Thank you for the follow up, i will try it :slight_smile:

Sorry for the delay, i was only able to test this now. I don’t know if it’s because i updated to 4.6, but doing the previous code now crashes when initializing the skeletal mesh on:

void UActorComponent::InitializeComponent()
{
	check(bRegistered);
	check(!bHasBeenInitialized); // Crashes here
	bHasBeenInitialized = true;
}

Also interested in this, I’ve made a custom BP node to clone actors but it gives me the same bHasBeenInitialized error.

Any news regarding this issue? Thank you :slight_smile:

Has anyone figured out a workaround for this? Thanks!

Anyone? does this mean that as of now it’s simply not possible to duplicate an aactor using C++?