Set values using Property System (Reflection)

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);
}
2 Likes