inherit struct and StaticClass

Hello!
I am trying to use StaticStruct()->ImportText() to recover struct’s property. For example:

USTRUCT()
struct Base{...};

USTRUCT()
struct Derived : public Base{...}; 

TSharedPtr<Base> ptr = MakeShared<Derived>();
ptr->StaticStruct()->ImportText(...)

I found only variable in Base is recovered,variable in Derived is not recovered. But if i use:

StaticCastSharedPtr<Derived>(ptr)->StaticStruct()->ImportText(...)

Result is right. Does it mean that StaticStruct not support polymorphism?
Thank you!

As its name suggests, StaticStruct is static, so it doesn’t support polymorphism indeed.

UStructs are not as flexible as UObjects in terms of casting/rtti.

All UObject subclasses have a UClass* Class member which is you can access via GetClass(), that are automatically added by GENERATED_BODY.

UStructs don’t have this, so there’s no way to figure out the type of a struct at runtime, unless you implement it yourself.
The Engine does something like this with the FDamageEvent structs
image

Each FDamageEvent subclass implements its own ClassID and overrides GetTypeID to make it possible to determine its type at runtime, after which you can safely use static cast.

1 Like