Download

How create new instance of USTRUCT via C++

Hi, and again - sorry for my bad English =)

I have:



USTRUCT()
struct FSomeStruct
{
GENERATED_BODY()

PRUPERTY()
float bloat;
}


In some class I need create *TArray <FSomeStruct > ArrayOfStruct;

And I need add new element to Array.

How I must initialize new struct object? NewObject<>() not for structs. **new **crash the Engine =)

How do I need to allocate memory for ustruct for transfer to Array?

You typically don’t use pointers to structs, they’re usually supposed to be created in-place.



TArray<FSomeStruct> ArrayOfStruct;

ArrayOfStruct[0] = FSomeStruct();


@TheJamsh
I have 3th file with definition USTRUCT() struct FSomeStruct
.

In class.h I have only FD:
struct FSomeStruct;

Only on class.cpp I have
#include “StructList.h”

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:

  1. Cut/paste definition of struct to Class.h
  2. Find a way how create instance of ustruct for transfer to TArray

The 3rd option, and what you probably should do, is fix your files to #include what you need.

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.

Is sadly.

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.

Bump!
Maybe we can’t pass around as normal pointers. What about TSharedPtr?

Something like this