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;
}
};