Reading Struct Attributes in C++

The following works for me :

// dummy test struct
FBlueprintCallableFunctionRedirect TestStruct;
TestStruct.ClassName = TEXT("Foobar");

// get the reflected property
FProperty* Prop = TestStruct.StaticStruct()->FindPropertyByName(FName(TEXT("ClassName")));

// Get value
FString Value = *Prop->ContainerPtrToValuePtr<FString>(&TestStruct);
UE_LOG(LogTemp, Warning, TEXT("%s"), *Value);

The pointer you are looking for is the instance of the struct object.

Here is a better example for your case, coming from a struct property in a UCLASS :

// header :
USTRUCT() struct FMyStruct {
    GENERATED_BODY()
    UPROPERTY() FString MyField;
}
UCLASS() class UMyObject : public UObject {
    GENERATED_BODY()
    UPROPERTY() FMyStruct MyStruct;
}

// cpp function :
MyStruct.MyField = TEXT("Foobar");

// get a pointer to the struct instance
FProperty* StructProp = StaticClass()->FindPropertyByName(FName(TEXT("MyStruct")));
void* StructPtr = StructProp->ContainerPtrToValuePtr<void>(this);

// get the reflected property
FProperty* StrProp = ((FMyStruct*)StructPtr)->StaticStruct()->FindPropertyByName(FName(TEXT("MyField")));

// get value in the struct
FString Value = *(StrProp->ContainerPtrToValuePtr<FString>(StructPtr));
UE_LOG(LogTemp, Warning, TEXT("%s"), *Value);
2 Likes