How to set values to FProperty (Reflection)

Hi,

How can I set the value of a FProperty if I don’t know what type of FProperty it is?

I get all my data from csv file and then I need to parse it to the correct values of the properties of the class. The editor constructor prompts all the classes that inherit from this master class, so they might have different properties and can be created outside the Plugin. I need to use reflection.

I know I can cast it to the correct FProperty class (FUInt32Property, FStrProperty, FStructProperty…etc.) but then I don´t know how to set the value, it requests some address and/or container.

I search the Engine´s code and I found some examples, but I don’t fully understand how to use it. I think it is because of the property Address and the container.

I couldn’t find much about this topic.

Can anyone help me?

Thanks!

Well, I found out a way, I think that’s the best way, but if someone finds a better way, please let me know.

//FObjectProperty
if (const FObjectProperty* ObjectProperty = CastField<const FObjectProperty>(PropertyHelper.Property))
{
	//get reference
	if (UObject* Object = LoadObject<UObject>(nullptr, *CellValue))
	{
		if (UObject** ValuePtr = ObjectProperty->ContainerPtrToValuePtr<UObject*>(Item))
		{
			*ValuePtr = Object;
		}
	}
}
//FIntProperty
else if (const FIntProperty* IntProperty = CastField<const FIntProperty>(PropertyHelper.Property))
{
	if (int32* ValuePtr = IntProperty->ContainerPtrToValuePtr<int32>(Item))
	{
		*ValuePtr = UKismetStringLibrary::Conv_StringToInt(CellValue);
	}
}
//FFloatProperty
else if (const FFloatProperty* FloatProperty = CastField<const FFloatProperty>(PropertyHelper.Property))
{
	if (float* ValuePtr = FloatProperty->ContainerPtrToValuePtr<float>(Item))
	{
		*ValuePtr = UKismetStringLibrary::Conv_StringToFloat(CellValue);
	}
}
//FStrProperty
else if (const FStrProperty* StrProperty = CastField<const FStrProperty>(PropertyHelper.Property))
{
	if (FString* ValuePtr = StrProperty->ContainerPtrToValuePtr<FString>(Item))
	{
		*ValuePtr = CellValue;
	}
}
//NO MATCHING PROPERTY
else
{
	UE_LOG(LogInventoryItemFactory, Log, TEXT("No Matching Property with the compatible values: '%s'"), *KeyName);
}
1 Like