Suppose I have a UObject class with a TArray saved as UPROPERTY. Is there a way to create TArray dynamically and then set Objects to a newly created array (see code below)?
UCLASS(BlueprintType, Blueprintable)
class SOME_API UClassA : public UObject
{
GENERATED_BODY()
public:
UPROPERTY(EditAnywhere, BlueprintReadWrite)
TArray<UObject*> Objects;
};
NewObject seems to raise a compilation error when trying to create TArray
UClassA* ObjA = NewObject<UclassA>();
TArray<UObject*>* NewArray = NewObject<TArray<UObject*>>(); // this raises compilation error
ObjA->Objects = *NewArray;
Error:
Error C2039 : 'StaticClass': is not a member of 'TArray<UObject *,FDefaultAllocator>'
Reference C2039 : see declaration of 'TArray<UObject *,FDefaultAllocator>'
Reference C2039 : see reference to function template instantiation 'T *NewObject<TArray<UObject *,FDefaultAllocator>>(UObject *)' being compiled
with
[
T=TArray<UObject *,FDefaultAllocator>
]
Can I create TArray with simple c++ new will this work? Will the Garbage Collector keep track of new array instead of the Objects array created in the constructor?
Edit:
I intend to create both TArray and contained UObjects dynamically. And I want the UE Garbage Collector to see them. This is meant the to be the only place they are stored.
You may use
AddorEmplaceto add elements to theTArrayifUClassAinherits fromUObject(which should also have avirtualdestructor):If you also want to create a single
TArraydynamically, you could store it in astd::unique_ptr: