I am trying to set the value of an UProperty in code.
void SetValue( UStrProperty* uProperty, FString value )
{
UObject* parent = uProperty->GetOuter();
int32 index = uProperty->GetOffset_ForInternal();
uProperty->SetPropertyValue_InContainer( parent, value , index );
}
When the last line is being run, I get an error, “Access violation writing location”. Anyone has any idea how to set value of the when given just the UProperty and the new value?
Are you actually intending to write to the parent UObject? Your previous question was asking about structs.
SetPropertyValue_InContainer does the pointer adjustment internally (based on the offset of the property it was called on) so you shouldn’t be doing it yourself when calling that version. The index argument is for when dealing with fixed-size array properties (those with an ArrayDim > 1).
Yes, I have the struct memory stored in my class in a FStructOnScope variable and I need to change the value of UProperties in this struct based on the data supplied.
Then you’re doomed. You can’t do anything with a UProperty on its own since you also need the instance to operate on, and the property can’t tell you that since it’s tied to the type, not the instance.
What you’re doing when you ask for the parent of the property is to get the UStruct or UClass that owns that property (the type), not an instance of that type.