Modifying Struct Property changes a different Struct Property instead (C++ Editor Plugin)

Hello! I am trying to create an Editor plugin that creates several Light Actors and sets their LightColor property. Using this Q&A answer as reference, I created this code to get the FStructProperty representing the LightColor and set its properties. I want to just set the R property and leave the others.

// objectProp = Member struct property I want to modify. bp = Light Actor
if (const FStructProperty* colorValues = CastField<FStructProperty>(objectProp))
{
	if (const UScriptStruct* colorStruct = colorValues->Struct)
	{
		void* structSettingsAddr = objectProp->ContainerPtrToValuePtr<void>(bp);
		if (FProperty* colorProp = colorStruct->FindPropertyByName(FName("R")))
		{
			if (FByteProperty* colorByteProp = CastField<FByteProperty>(colorProp))
			{
				// Set only R value to 128 - Verified this is called using UE_LOG
				colorByteProp->SetPropertyValue(structSettingsAddr, 128);
				FEditPropertyChain EditChain;
				EditChain.AddHead(colorProp);
				EditChain.SetActiveMemberPropertyNode(objectProp);
				EditChain.SetActivePropertyNode(colorProp);
				FPropertyChangedEvent EditPropertyChangeEvent(colorProp, EPropertyChangeType::ValueSet);
				FPropertyChangedChainEvent EditChangeChainEvent(EditChain, EditPropertyChangeEvent);
				bp->PostEditChangeChainProperty(EditChangeChainEvent);
			}
		}
	}
}

What I expect is several Light Actors created with LightColor property having R = 128 and G = B = 255. Instead, I get R = G = 255 and B = 128! Here is a screenshot, this is the same for all Light Actors created:

image

What is causing this? How can I prevent this issue?

I think I figured out the cause for this. Looking at the UE source code, it appears that SetPropertyValue() actually just directly uses the address provided to it rather than manipulating the address to get the inner property address. Therefore, every call to SetPropertyValue() was setting the value at the struct address which apparently = B for LinearColor property.
Changing the logic to use FColor* structSettingsAddr = objectProp->ContainerPtrToValuePtr<FColor>(bp); solved the issue.

1 Like