Does DuplicateObject() perform a shallow or deep copy?

I’m making use of DuplicateObject() in my C++ code to duplicate a UObject which has a TArray of pointers to another UObject type I’ve created.

Does DuplicateObject() perform a shallow copy (Simply copies the pointers to point to the same objects) or deep copy (Copies each of the objects that the TArray is pointing to)?

i got same question, im using pointer to duplicate object, but when object that i duplicated got cleaned, my duplicated object pointer points to nowhere so engine crashes.

It makes a shallow copy.

If a SubObject in a marked UProperty of the Target Object you called DuplicateObject() on, has the Target Object in their Outer Chain (It’s the first parameter when you call NewObject()), then DuplicateObject() will create a deep copy of that SubObject and assign it to the same property of the Duplicated Object.

This is recursive operation and will apply to children of SubObjects (and so forth) that have the target in their outer chain.

However, if a SubObject doesn’t have the Target Object in its Outer Chain, it will only copy the reference (IE: Shallow Copy) and skip traversing that SubObject for more duplicate targets.

The Outer Object is essentially who owns each Object for Serialization purposes, and DuplicateObject() uses this information for making the deep / shallow decision.

5 Likes