How to convert a struct to UStruct*

I’m trying to write a function that will take an array of user defined structs and write them to .
The UStructToJsonObjectString function requires a UStruct* pointer as its first parameter, but I can’t figure out how to provide that.
I tried UStruct* StructDef(FStructName), but that provides the error:

'bool FJsonObjectConverter::UStructToJsonObjectString(const UStruct *,const void *,FString &,int64,int64,int32)' : cannot convert argument 1 from 'UStruct *(__cdecl *)(FStructName)' to 'const UStruct *'

So how do I find my UStruct*?

This answer has duplicates.

An UStruct is a struct defined in the realm of UE4, just like UClass, AActor or UObject. UStructs are bindable to BP if required and operate within the Garbage collector. The following is a sample struct that should get you started:

USTRUCT()
struct FMyStruct
{
    GENERATED_USTRUCT_BODY()
 
    UPROPERTY()
    FString StringProperty
 
    FMyStruct()
    {
    }
};

Now to your question to how to get a pointer to a struct, if you have your struct a a property in one of your UObjects you just have to reference it:

FMyStruct MyStruct;
*MyStruct; // Pointer to your struct, MyStruct is actualy created in the stack and not on the heap

Some more info about struct can be found in this nice wiki post.

Thank you for answering my question.
Maybe I’m not understanding something, but if I use
FMyStruct MyStruct;
FJsonObjectConverter::UStructToJsonObjectString(*MyStruct, etc.)
I get
cannot convert argument 1 from ‘FMyStruct’ to ‘const UStruct *’
1> No user-defined-conversion operator available that can perform this conversion, or the operator cannot be called

You are using the instance as your first parameter, you might want the FMyStruct::StaticStruct() there.

You can’t use variable types as arguments in C++ (except templates), this function expect UStruct pointer which is struct identfier object in reflection system (same as UClass* for classes). If you made struct exposed to reflection system (have USTRUCT() and GENERATED_USTRUCT_BODY()) engine generates UStruct object for each of such struct and GENERATED_USTRUCT_BODY() generates static function (as in C++ structs can have functions, buut UE4 reflection system does not support them, but they work anyway and engine it self use them) which you can easily get it from type name, like this:

FStructName::StaticStruct()

This will return UStruct* for specific type, ofcorse this won’t work on non-reflected structs, engine is not aware of there existance as any other non-reflected things.

1 Like

StaticStruct :smiley: that was the missing static that I was seeking for hahahah :smiley:

I’m slow :stuck_out_tongue:

hahaha :smiley: nice to catch the same question all together dude :smiley:

Yea, i was suspicious struct has it too just check and indeed GENERATED_USTRUCT_BODY() generates StaticStruct() like classes have StaticClass(). I’m learning myslef by trying to solve other peoples problems :stuck_out_tongue:

Yeah, it’s used a lot by the engine when serializing structs.

Totally awesome. Thank you!