This occurs because the 'AttachParent' property is a UPROPERTY, and as a result when we create a new instance of a USceneComponent subtype, any USceneComponent subtypes it owns are treated as instanced default subobjects initialized from the contents of the CDO after they're created in the owning class constructor. This is handled in FObjectInitializer::InitSubobjectProperties(), where we call InitProperties() on each subobject instance, giving it the default subobject from the CDO as the default object. This in turn leads into the following code path in InitProperties(), where we then overwrite the value set at construction time with the value that's stored within the CDO for all properties defined on the subobject's class:
if (Class->GetDefaultObject(false) != DefaultData) { QUICK_SCOPE_CYCLE_COUNTER(STAT_InitProperties_FromTemplate); for (UProperty* P = Class->PropertyLink; P; P = P->PropertyLinkNext) { P->CopyCompleteValue_InContainer(Obj, DefaultData); } }
The workaround for now would be to defer the attachment into the OnRegister() method override, impelmented on the owning scene component class.
(Note: This issue is currently "hidden" by [Link Removed] in 4.14. Once that is fixed, this will resurface in that version. However, this should currently occur in prior versions.)
1. Create a nested scene component subobject case such as the one described in [Link Removed].
2. Add the following code to MySceneComp's C++ ctor:
NestedSceneComp->SetupAttachment(this);
3. Recompile in Visual Studio and wait for the UE4 editor to hot-reload the updated class.
4. Back in the UE4 editor, create a new Blueprint class based on Actor.
5. Drag an instance of the new Blueprint class into the scene and select it.
6. In the Actor details view, click on the "Add Component" drop-down and attempt to choose "MySceneComp" to add it.
7. An ensure should result:"Template Mismatch during attachment. Attaching instanced component to template component."
Expected result: There is no ensure() message and this situation does not occur.
There's no existing public thread on this issue, so head over to Questions & Answers just mention UE-38697 in the post.
9 |
Component | UE - Gameplay |
---|---|
Affects Versions | 4.12 |
Created | Nov 16, 2016 |
---|---|
Resolved | Nov 28, 2016 |
Updated | Apr 27, 2018 |