SceneComponent inside SceneComponent

Hello guys I have a custom SceneComponent that looks like this:

UCLASS(ClassGroup = (Custom), meta = (BlueprintSpawnableComponent))
class THIRDPERSON_API UCameraRig : public USceneComponent
{
	GENERATED_BODY()

public:

	UCameraRig();

	virtual void TickComponent(float DeltaTime, ELevelTick TickType, FActorComponentTickFunction* ThisTickFunction) override;

protected:

	virtual void BeginPlay() override;

private:

	class USceneComponent* Pivot;

	class USpringArmComponent* SpringArm;

	class UCameraComponent* Camera;
};

Its constructor looks like this:

UCameraRig::UCameraRig()
{
	PrimaryComponentTick.bCanEverTick = true;

	this->Pivot = this->CreateDefaultSubobject<USceneComponent>(TEXT("Pivot"));
	this->Pivot->AttachTo(this);
	this->SpringArm = this->Pivot->CreateDefaultSubobject<USpringArmComponent>(TEXT("SpringArm"));
	this->SpringArm->AttachTo(this->Pivot);
}

The goal is for this CameraRig component is to create additional SceneComponents when it’s attached to an Actor.
The code in the constructor doesn’t work, but illustrates the idea. Basically I can attach all the needed SceneComponents by hand in the editor, but I want to attach only my custom CameraRig SceneComponent and it will handle the rest. How can this be achieved?

If I understand you correctly, you want your UCameraRig to basically dynamically create a bunch of other components on the owning actor based on some behavior/options?

You could simply overload PostLoad (which will be called after your the owning actor has loaded and all the components have been initialized) in your UCameraRig and then just use GetOwner() to get a reference to the actor and start looking for the components you expect - and if they aren’t there, add them.

PostLoad/PostInitProperties are better places to do any “constructor-like” logic as there is a lot happening in a UE constructor with regard to serialization and such.

Yes. I don’t want to include any Camera logic in my ACharacter class, so my idea is to have the camera logic as a component and attach it to my character. I will give your suggestion a try. I have been thinking a lot if all this trouble is worth it, because it’d be a lot easier to create these components inside my ACharacter like it’s done in most of the template projects. The thing is that I really like to separate things logically and it doesn’t make a lot of sense for the player class to be concerned with camera logic. I will update you on my progress.

Hello again, I played around with your idea. It’s promising. I couldn’t get it to work, but I will experiment more. I will update you as soon as I get results.