So the compiler does not allow to specify a structure in the TArray as a variable ( TArray<FSomeStruct> ArrayOfStruct; ). Only as a pointer. ( TArray<FSomeStruct *> ArrayOfStruct; ).
So I have 2 options:
Cut/paste definition of struct to Class.h
Find a way how create instance of ustruct for transfer to TArray
No. All includes right. I tried it on new clear project. FD not work for TArray<FStructValue> Array. Only if FStruct include directly to declaration TArray<>.
But if I try use pointers TArray<FStructValue *> - compiler works fine.
So. I have problem. For stable work I need include file with USTRUCT definition directly to file where I will declaration TArray.
Becouse I dont have a way to allocate memory for USTRUCT via unreal framework.
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;
}
No, what you need to do is include the file with the struct definition in the include file that you’re using it in.
You most certainly can have a TArray of UStructs, I do it all the time (mostly using a TMap<SomeKindOfKey, UStruct> and the UStruct contains a TArray<UStruct>). But you must include the proper header file where you need it.