Setting value of UProperty

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.

Method 1

bool String( const char* str, SizeType length, bool ) override
{
	UObject* parent	= uProperty->GetOuter();
	int32 index		= uProperty->GetOffset_ForInternal();
	 
	UStrProperty* strProperty		= Cast<UStrProperty>( uProperty);
	strProperty->SetPropertyValue_InContainer( parent , FString( str ), index ); // Exception thrown (vcruntime140.dll) in UE4Editor.exe: 0xC0000005: Access violation writing location
}

Method 2

bool String( const char* str, SizeType length, bool ) override
{
	UObject* parent	= uProperty->GetOuter();
	int32 index		= uProperty->GetOffset_ForInternal();
    	
	void* valuePtr = uProperty->ContainerPtrToValuePtr<void>( parent ); // fails check in UnrealType.h, line 344, check(GetOuter()->IsA(UClass::StaticClass()));
	strProperty->SetPropertyValue( valuePtr, FString( str ) );
}

I need to override this function from a third party library and in this class, only the pointer to the UProperty is known.

only the pointer to the UProperty is known.

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.