To use the FInstancedStruct::FindInnerPropertyInstance function to find the value of a property named Type with type TEnumAsByte<EStageDirectionType>, follow these steps in your code:
-
Define your FName for the property name:
FName PropertyName(TEXT("Type"));
-
Declare variables for the output parameters:
const FProperty* OutProperty = nullptr;
const void* OutData = nullptr;
-
Invoke the FindInnerPropertyInstance method:
if (YourInstancedStruct.FindInnerPropertyInstance(PropertyName, OutProperty, OutData))
{
// The property was found
}
else
{
// Handle the case where the property was not found
}
-
Cast the OutData to the expected type, in this case TEnumAsByte<EStageDirectionType>:
if (OutProperty && OutData)
{
if (const FByteProperty* ByteProperty = CastField<FByteProperty>(OutProperty))
{
// `OutData` is a pointer to the beginning of the structure's memory
const uint8* BytePtr = static_cast<const uint8*>(OutData) + ByteProperty->GetOffset_ForInternal();
TEnumAsByte<EStageDirectionType> EnumValue = static_cast<EStageDirectionType>(*BytePtr);
// Use the EnumValue here
}
}
Here is the complete example:
FName PropertyName(TEXT("Type"));
const FProperty* OutProperty = nullptr;
const void* OutData = nullptr;
if (YourInstancedStruct.FindInnerPropertyInstance(PropertyName, OutProperty, OutData))
{
if (const FByteProperty* ByteProperty = CastField<FByteProperty>(OutProperty))
{
const uint8* BytePtr = static_cast<const uint8*>(OutData) + ByteProperty->GetOffset_ForInternal();
TEnumAsByte<EStageDirectionType> EnumValue = static_cast<EStageDirectionType>(*BytePtr);
// Now you can use EnumValue
}
else
{
// The property type was not FByteProperty, handle this case if necessary
}
}
else
{
// Handle the case where the property was not found
}
Explanation:
- Initialize PropertyName: This sets up the
FName object to match the name of the property you're looking for.
- Declare Out Variables:
OutProperty and OutData are declared to hold the output from FindInnerPropertyInstance.
- Call
FindInnerPropertyInstance: This attempts to find the property in the instance and updates OutProperty and OutData if successful.
- Type Check and Cast: It checks if the found property is of the expected type (
FByteProperty) and then retrieves the byte value from the structure's memory.
- Retrieve Value: Finally, it converts the byte value to the expected
TEnumAsByte<EStageDirectionType> and uses it.
This approach ensures that you safely find and cast the property named Type to the correct type and access its value.