Hello.
Could anyone help me understand how creation of components work?
Let’s say I have following component. Its purpose is to attach InstancedMeshComponent as child to Actor.
// WallComponent.h
class ARCHITECTURE_API UWallComponent : public USceneComponent
{
// basic stuff
protected:
UPROPERTY(EditAnywhere, BlueprintReadOnly, Category = TestComp, meta = (AllowPrivateAccess = "true"))
UInstancedStaticMeshComponent* _instances;
}
// WallComponent.cpp
UWallComponent::UWallComponent() { UE_LOG(LogTemp, Warning, TEXT("UWallComponent constructor %s starts"), *name);
auto name = this->GetName();
name += FString("-wall");
_instances = CreateDefaultSubobject<UInstancedStaticMeshComponent>(*name);
_instances->SetupAttachment(this);
_instances->CreationMethod = EComponentCreationMethod::SimpleConstructionScript;
auto owner = GetOwner();
if (owner) {
[INDENT=2]UE_LOG(LogTemp, Warning, TEXT("UWallComponent (%s): owner exists (%s)"), *this->GetName(), *owner->GetName());
UE_LOG(LogTemp, Warning, TEXT("UWallComponent (%s): _instances: (%s)"), *this->GetName(), *_instances->GetName()); // name is Wall-wall[/INDENT]
}
}
void UWallComponent::OnRegister()
{ Super::OnRegister();
if (_instances) {
[INDENT=2]UE_LOG(LogTemp, Warning, TEXT("UWallComponent (%s): OnRegister _instances: %s"), *GetName(), *_instances->GetName()); // name is WallComponent_0-wall[/INDENT]
}
}
And Logs:
LogTemp: Warning: UWallComponent constructor Default__WallComponent starts
LogTemp: Warning: UWallComponent constructor WallComponent_0 starts
LogTemp: Warning: UWallComponent constructor Wall starts
LogTemp: Warning: UWallComponent (Wall): owner exists (TEST_ACTOR_BP_C_0)
LogTemp: Warning: UWallComponent (Wall): _instances: (Wall-wall)
LogTemp: Warning: UWallComponent (Wall): OnRegister _instances: WallComponent_0-wall
In logs I see:
- Constructor was called 3 times (might have something with CDO? But why not 2 times?)
- Value of **_instances **is changing between function calls:
- in constructor was to **_instances **pointing to component with name **Wall-wall **(as I would expect)
- inside function OnRegister is _instances pointing again to WallComponent_0-wall (also in other functions, so acessing **_instances **properties becames impossible)
Is there simple explanation for this behaviour? I could not find any resources explaing components lifecycle in depth.