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.
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.
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?
Quick side note, if you are duplicating a component you have to call RegisterComponent() after duplicating or else it will not be visible in the world since duplicating the component does not automatically create its physical state.