Casting UStruct to derrived struct

It’s annoying why you can’t cast UStructs like this and even worse, if you try to get the StaticStruct of a Struct Pointer, it will always return the StaticStruct of the base pointer type.

For example:

FActorRecord Record = FActorRecord();
Record::StaticStruct() // will be FActorRecord (good).

FRecord* RecordPtr = &Record;

RecordPtr::StaticStruct() // will be FRecord (bad).

The best solution I have come up with is to add a UStruct* ActualStaticStruct variable in your base type (in my case FRecord) and initialise this variable for every derived type by doing ActualStaticStruct = StaticStruct() in every constructor.

I’m not entirely sure what the built-in version of StaticStruct() does but it’s definitely undesirable.

2 Likes