Reusable collection of components


I’m working on a train game and have some questions how to make reusable parts.

For example: A train coupler part, includes several meshes, sounds and logic. i want it to be one item in the actor hierarchy to make it more readable, and with a few settings. A normal locomotive or train car would have 2 of these train couplings. Then for example a prime mover, control panels, and lots of interactive systems, would be a horrible mess and inflexible to integrate all in a base actor.

What is the best (intended) way to achieve this? Child actors, A scene component that creates its own child components in the constructor? Some other method?

Best Regards

Hi JayZone80,

That sounds like you’ve got it right, Child Actor Components would be the way to go - you wouldn’t have to add them from the Constructor, you could add them straight into the Blueprint from the Blueprint Editor if wanted - both ways would work.

I tried make some components in c++ with child components attached (created in the constructor), it worked fine but the hierarchy in the outliner is wrong (the components should be under janney coupler).

But it seem to keep the correct relative positions, so it might just be a bug?

	PrimaryComponentTick.bCanEverTick = true;

	bShouldCollideWhenPlacing = true;

	CouplerMeshComponent = CreateDefaultSubobject<UStaticMeshComponent>(TEXT("Coupler Mesh"));

	CouplerJawMeshComponent = CreateDefaultSubobject<UStaticMeshComponent>(TEXT("Coupler Jaw Mesh"));
	CouplerJawMeshComponent->SetupAttachment(CouplerMeshComponent, TEXT("Pivot"));
	CouplerAudioComponent = CreateDefaultSubobject<UAudioComponent>(TEXT("Coupler Audio"));
	CouplerAudioComponent->SetupAttachment(CouplerMeshComponent, TEXT("Pivot"));
	CouplerAudioComponent->bAutoActivate = false;

	CouplerReleaseAudioComponent = CreateDefaultSubobject<UAudioComponent>(TEXT("Coupler Release Audio"));
	CouplerReleaseAudioComponent->SetupAttachment(CouplerMeshComponent, TEXT("Pivot"));
	CouplerReleaseAudioComponent->bAutoActivate = false;

I’d try using “CouplerJawMeshComponent->AttachToComponent(CouplerMeshComponent,FAttachmentTransformRules::KeepRelativeTransform);” and the same for the Audio component etc.

I just tested using AttachToComponent, both in constructor and OnComponentCreated().
No difference in the outliner. But i also tested the hierarchy in BP, and there it seem just fine, so seem it only was a bug in the outliner!

Thank you for your help! Seem my original solotion was fine! :slight_smile: