How create new instance of USTRUCT via C++

Pointers to USTRUCT() are not supported by reflection, so you will run into endless problems with garbage collection. USTRUCTS() are not designed to have pointers to them exposed or stored except in very specific cases (in C++ only), where it might make some sense. Generally however they are passed around by reference or as copies.

The pointer version compiles because the size of a pointer is known at compile time - but the size of the struct isn’t unless it’s declared first. This is why forward declarations only work for pointers.

You need to either declare the USTRUCT() in the same file you’re declaring TArray<FMyStruct> - or you need to put it into a separate header of it’s own and include it. It has to be declared before you can use it.



USTRUCT()
struct FSomeStruct
{
    GENERATED_BODY()
};

UCLASS()
class USomeClassThatUsesTheStruct : public UObject
{
    GENERATED_BODY()
public:
    TArray<FSomeStruct> Structs;
}


1 Like