Duplicate UObject into another one

Hello,

At one point in my code I have two UPROPERTY variables of type UCharacterStats* (Which extends UObject). I am initializing one of them with default constructor:

BaseCharStats = NewObject<UCharacterStats>();

Then I set some params of this object. In my next statement I would like to initialize the second variable of the same type, so that it would have the same params already set (it would be a duplicate of BaseCharStats). I assume that if I do it this way:

CharStats = BaseCharStats;

I would just make the CharStats point to the same BaseCharStats object, as these are pointers. Am I right, or do you override ‘=’ operator somewhere in the engine code? If that is true, how do I safely copy all data of BaseCharStats into the CharStats?

P.S.: I tried using NewNamedObject, assuming its third parameter, i.e. template, is the object from which the new object would copy the data, but I was getting assertion errors upon engine startup with that.

2 Likes

Hi Lordink,

to duplicate an object use DuplicateObject template function from UObjectGlobals.h.

Thanks,

Jarek

2 Likes

Am I using it right? I found out that I’d have to pass a NULL as a second parameter for it to work. What is this Outer param?

CharacterStats = DuplicateObject(BaseCharacterStats, NULL);
1 Like

Every UObject has it’s Outer object (besides UPackage’s). You can get Outer from BaseCharacterStats object (GetOuter()), so they’re both inside the same hierarchy. However in such case you need to make sure, the name will be unique.

Passing nullptr as second character, your making special transient package it’s outer.

Hi

After some days searching the WWW I’ve found out that your answer is the only ONE. Neither other UE4 users nor official documentation explain how to use DuplicateObject function.

I ask your help because I got problem with duplicating objects. After executing this function I have absolutely equal copy of the source object, but with one difference - new object is invisible, doesn’t collide with other objects and gravity doesn’t work for that object. All parameters are the same: simulate physics, enable gravity, visible and anyway, as I said, it’s invisible and useless. Also it is no reaction if I am changing parameters on that object.

I’ve tried to copy simple AActor object, AStaticMeshActor and blueprint “complex” objects. All with one result.

Would you please help me and show any example of using DuplicateObject? Maybe it is necessary to do some other steps after calling DuplicateObject function?

Thank you,

1 Like

My code:

.h

UPROPERTY(EditAnywhere)

AActor *BulletTemplate = nullptr;

.cpp

std::wstring name = std::wstring(L"Bullet_") + std::to_wstring(itemNum++);

UObject *outer = BulletTemplate->GetOuter();

AStaticMeshActor *bullet = DuplicateObject(Cast(BulletTemplate), outer, name.c_str());

You can also use ConstructObject with the original object as the Template. That’s all DuplicateObject does

Better use MakeUniqueObjectName(Outer, Class); to generate Unique names.

1 Like