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:
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.