Add Child actor component in C++

Hi everyone

How can I have an equivalent of Add Child Actor Component in C++ (withe a class param).

I made this little test in BP and work very great, but now I need to use array as feed for the add child actor (access to the “class” param)

in fact I’m trying to Create and Add some child actor component, using an array of class as feed, at the construction phase.

I have try this code from but not working :

void ASS_Base_Ship::spawnAttachMods() {
	//Primary Weapon
	for (int i = 0; i < primaryWeaponSlots.Num(); i++) {
		UPrimitiveComponent* NewComp = ConstructObject<UPrimitiveComponent>(primaryWeaponSlots[i].mod, this, "PrimaryWeapon" + i);
		if (NewComp) {
			NewComp->AttachTo(this->ShipMesh, primaryWeaponSlots[i].socketName, EAttachLocation::SnapToTarget);

any Idea on how I can doing it ?

But Child Actor Components is normal component like any other which work like a Actor slot in your Actor

You add it like any other component and set ChildActor.

's code looks like some kind of hack, i’m not sure if he is aware that you can simply place UChildActorComponent. But i think whats missing in code is:

AddComponent(FName("PrimaryWeapon"), false, FTransform(), NULL);

I don’t really understand what I have to do with that ^^

Then learn how to add component of any class, once you learn that you will be able place any component you like. you do this:

In header:

UClassOfTheComponent* Component;

Then want you want to create component:

Component = ConstructObject<UClassOfTheComponent>(UClassOfTheComponent::StaticClass(), GetOwner(), NAME_None, RF_Transient);

Then set up variables that set up the component (in case of UChildActorComponent you set ChildActorClass which will spawn actor, or ChildActor if you got ready one). If you deal with SceneComponent you need to attach it to something, so you can copy code here:

Component->AttachTo(this->ShipMesh, primaryWeaponSlots[i].socketName, EAttachLocation::SnapToTarget);

And with every component you need to register it


for a simple component like a SM or a camera I know, but Here need to add component, using an Array containing the class of the subactor that I need to add

to be more clear, I have a Ship, that have an array containaing Engine, Weapon, Shield class, but I can’t know which class can be here and how many execept by the array’s size

so It’s on runtine that I want to do that, I would like to have it in the Ctor, for debug purpose

template <class SceneComponentClass>
SceneComponentClass* UMyClass::SpawnChildComponent(USkeletalMeshComponent* Component,
                                                                        const FString Name, const FName SocketName = "")
	SceneComponentClass* ChildComponent = NewObject<SceneComponentClass>(
		Component, SceneComponentClass::StaticClass(), FName{Name});

	if (SocketName != "")
		ChildComponent->AttachToComponent(Component, FAttachmentTransformRules::SnapToTargetNotIncludingScale,
		ChildComponent->AttachToComponent(Component, FAttachmentTransformRules::SnapToTargetNotIncludingScale);
	ChildComponent->CreationMethod = EComponentCreationMethod::Instance;
	return ChildComponent;

none of this answers the OP question

1 Like

You can add it as you would a child component, just need to assign the child actor class.

	ComponentVariable = CreateDefaultSubobject<UChildActorComponent> (FName(TEXT("Component Name")));
	ComponentVariable ->SetupAttachment(ParentComponent);