Hello!
I’ve been soaking in this issue for a while and it’s finally time to kneel at the alter of the most high forums.
I have not tested this for other structs besides FVector - that being said. . . .
If I have a Uobject that has a member MyVector and I want to change the Y or Z member in MyVector - it will only ever change X. (Reading this value back by also yields the X value).
This is how I’m currently trying to achieve this process - based on code from this thread
float UPropertyUtilFunctionLib::SetObjectPropertyByName(UObject* ObjectWithVector, const FName& PropName, const float& NewValue)
{
FName VectorMemberName = FName("Y");
// Get property representing struct
FProperty* Prop = ObjectWithVector->GetClass()->FindPropertyByName(PropName);
// Ensure ObjectWithVector actually has a myVector member
if (Prop)
{
// Get struct address
void* StructAddress = Prop->ContainerPtrToValuePtr<void>(ObjectWithVector);
// Ensure MyVector really is a vector
if (FStructProperty* StructProp = CastField<FStructProperty>(Prop))
{
// We'll get struct properties through a UScriptStruct, not a UObject
// ScriptStructs are just templates and don't hold any of your data
UScriptStruct* ScriptStruct = StructProp->Struct;
// Get the vector's "X" property
FProperty* ChildProp = ScriptStruct->FindPropertyByName(VectorMemberName);
// Cast to FloatProperty
if (FFloatProperty* ChildFloatProp = CastField<FFloatProperty>(ChildProp))
{
// Get
float OldValue = ChildFloatProp->GetFloatingPointPropertyValue(StructAddress);
// Set
ChildFloatProp->SetFloatingPointPropertyValue(StructAddress, NewValue);
return OldValue;
}
}
}
return 0;
}
On 4.27.2 this code will always change the X value not the Y value - I’m purposefully avoiding checking tags and calling object->PostEdit() to keep the code to a minimum. But I have tested with yielding the same result.