c++ equivalent of Add ChildActorComponent

Dear Community,

I am trying to add child actor components to an actor upon its creation.
I know i can do this in blueprints by using the Add ChildActorComponent node and specifying the Child Actor Class.

Is there a way to do exactly this in c++? or is there a way to automate this (as in have a loop which would add ChildActorComponents of various classes depending on an Array of classes)

Thanks in advance.

I think what you’re looking for is [FONT=Courier New]UChildActorComponent.

On 4.6, it would be something along the lines of the following (typed in forum, never compiled, caveat emptor, no refunds, no guarantee):

In .h:

UChildActorComponent* ChildActor;

In .cpp constructor:

ChildActor = ObjectInitializer.CreateDefaultSubobject<UChildActorComponent>(this, TEXT("InventoryCamera"));
ChildActor->ChildActorClass = [Actor class to create]::StaticClass();

You can find the API documentation and source for [FONT=Courier New]CreateChildActor() here: UChildActorComponent::CreateChildActor | Unreal Engine Documentation

I seem to be unable to create a child actor component in c++, ObjectInitializer can be only used in the constructor (i tried creating new objectinitializer outside of the constructor but it fails to create the component), and CreateChildActor throws a compile error with unresolved externals, i’ve also tried to create an actor by spawnActor but im unable to do so prior to start of the game.

In blueprints i can use Add ChildActorComponent in the construction script but I have to manually specify the child actor class in the details panel (which isnt very efficient as I need to create quite few child actors based on a class array).

UChildActorComponent is a bit of a kludge meant to work around certain hierarchies that don’t work well in Blueprint. Blueprints are meant to be created with a hierarchy of components, but in some rare cases you’ll need to add another actor to that hierarchy, which is something that Blueprint does not handle well at all. In the simple cases, the best solution is to separate whatever actor you want to add into a component of its own (just like static mesh actor is merely a thin wrapper for static mesh component) which you can then create and handle without going through a child actor component.

In any event, you can use Jeff’s solution except for one change: remove the CreateChildActor call from the constructor. You cannot spawn new actors in constructors, only subobjects (which Actors cannot be).

If you really want to keep the ChildActorComponent, a simple fix would be to keep those two lines intact:

ChildActor = ObjectInitializer.CreateDefaultSubobject<UChildActorComponent>(this, TEXT("InventoryCamera"));
ChildActor->ChildActorClass = [Actor class to create]::StaticClass();

You don’t need to manually call CreateChildActor as it will be called for you when the component is created. In fact, trying to call anything on it will yield linker unresolved externals as it is not exported from the engine. (See the MinimalAPI tag in its class declaration.) For this reason, I don’t believe ChildActorComponent is meant to be used in C++.

It’s worth pointing out that in C++, you don’t really need a child actor component since you can much more easily create and manage actor hierarchies. For instance:

void AMyCharacter::PostInitializeComponents()

	FActorSpawnParameters SpawnParams;
	SpawnParams.bNoCollisionFail = true;
	AMyPickup* Pickup = GetWorld()->SpawnActor<AMyPickup>( PickupClass, SpawnParams );
	AddPickup( Pickup );

This was lifted from our game almost as-is and it is how we spawn initial inventory contents. You can use actors spawned in this way just like you would use one spawned by a childactorcomponent.


You mention that new actors cannot be spawned in the constructor, yet using the add child component node in blueprints ConstructionScript does spawn a child actor of the specified class and i can get a refference to that actor (which is what im trying to do) prior to launching the game.

Thanks for this thread. I don’t know if anyone is still reading or responding, but in case anyone is can you describe how to do the C++ equivalent of AddChildActorComponent in a way that works outside of an actor constructor but inside another actor’s construction script?


Are you per chance mixing up constructor and ConstructionScript? Those two arent the same at all. They refer to different functions.

This may be an old thread, but I also struggle with creating and attaching an Actor from inside the construction script.

I have a C++ class (deriving from AActor) which provides a method, which is supposed to spawn actors at certain locations. This method is called from the construction script of the BP that derives from that C++ class.
CreateDefaultSubobject does not work (probably for the reason stated above: it is not meant to be used for actors) and SpawnActor can not be called from ConstructionScripts so nothing happens.
Simply calling CreateDefaultSubobject on ChildActorComponent does not work (probably because it needs to be registered?).

But it was stated above that ChildActorComponent is not supposed to be used from C++. So I would like to know what the proper C++ approach would look like for creating and attaching actors to itself (an actor). Can anyone help me out with this? Must I define a component instead of an actor and follow this page?

Yeah i was mixing up construction script and constructors at the time
, btw in my case the actors i needed to spawn were representing my game systems (inventory, combat, quests etc…) and i remade them as components that i then spawn in c++, if i need to spawn an actor alone, i do it on begin play

Actually I realized a few minutes ago that CreateDefaultSubobject ist meant to be used inside the constructor only. Not inside the ConstructionScript. We are supposed to use NewObject and its alike when we want to dynamically spawn outside of the constructor:

Edit: nvm I fixed this as well, this time I was too quick to ask :smiley:

My game systems are Actor Components and I spawn them like this in my main character system class

Inventory_System = PCIP.CreateDefaultSubobject<UInventorySystemClass>(this, “Inventory_System”);

The ConstructionScript equivalent in C++ is the virtual method OnConstruction.
You can override that method and that’s how you’ll get the ConstructionScript functionallity in C++.

Thanks guys, very useful!