I have a c++ class (let call this ParentClass) with a WidgetComponent attached. From this widgetcomponent I want to have access to class variables and values using Reflection.
I created a Blueprint class that inherits from ParentClass.
I managed to list all the FProperties this way
for (TFieldIterator<FProperty> PropIt(BindedObject->GetClass(), EFieldIteratorFlags::ExcludeSuper); PropIt; ++PropIt)
{
FProperty* Property = *PropIt;
//PropertiesNames.Add(Property->GetName());
UE_LOG(LogTemp, Warning, TEXT("Property Name: %s"), *Property->GetName());
AddPropertyToCanvas(Property, BindedObject);
}
Then I do something retrieving the values
void UInfoReflectionWidget::AddPropertyToCanvas(FProperty* Property, UObject* ContainerObject)
{
if(Cast<FFloatProperty>(Property))
{
AddFloatPropertyToCanvas(Property, ContainerObject);
}
else if(Cast<FIntProperty>(Property))
{
AddIntPropertyToCanvas(Property, ContainerObject);
}
else if (Cast<FBoolProperty>(Property))
{
AddBoolPropertyToCanvas(Property, ContainerObject);
}
else if (Cast<FStructProperty>(Property))
{
AddStructPropertyToCanvas(Property, ContainerObject);
}
}
For example for float case:
void UInfoReflectionWidget::AddFloatPropertyToCanvas(FProperty* Property, UObject* ContainerObject)
{
FFloatProperty* FloatProperty = Cast<FFloatProperty>(Property);
float* Value = FloatProperty->ContainerPtrToValuePtr<float>(ContainerObject);
}
The problem arises with the UStruct case. How can I recursively inspect each struct?
So far I arrived here, but it crashes, seems the UObject reference to be invalid. How can I access the UStruct member values?
void UInfoReflectionWidget::AddStructPropertyToCanvas(FProperty* Property, UObject* ContainerObject)
{
FStructProperty* StructProperty = Cast<FStructProperty>(Property);
UObject* Value = StructProperty->ContainerPtrToValuePtr<UObject>(ContainerObject);
for (TFieldIterator<FProperty> PropIt(StructProperty->Struct, EFieldIteratorFlags::ExcludeSuper); PropIt; ++PropIt)
{
FProperty* PropertyPtr = *PropIt;
UE_LOG(LogTemp, Warning, TEXT("Property Name: %s"), *(PropertyPtr->GetDisplayNameText().ToString())); //Works properly
AddPropertyToCanvas(PropertyPtr, Value); // Leads the engine to crash
}
}