Constructing Actors out of Actors

I have a APuzzlePiece made up of APuzzleBlock, and the blocks have functions to help build valid PuzzlePieces. How do I go about constructing a PuzzlePiece out of PuzzleBlocks? I could do it in Blueprint easily enough with the Add Child Actor Component, but I’m very confused about UChildActorComponent. It doesn’t seem to be working so I must have something wrong due to misconception about this type.

This is what I have in the PuzzlePiece Constructor:

NewBlock = CreateDefaultSubobject<UChildActorComponent>(TEXT("PieceBlock0"));
	if (NewBlock)
	{
		const TSubclassOf<APuzzleBlock> BlockClass;
		NewBlock->SetChildActorClass(BlockClass);
		NewBlock->CreateChildActor();
	}

But when I run this, No child actor appears in world. Please help!

I don’t use Child Actor Components… but maybe I can help.

Here’s what I’ve noticed:

  • From what I’ve seen, CreateChildActor is handled automatically, so you don’t need to call it.
  • You never assigned a value to the BlockClass, so its just nullptr, and not Actor is created.

So, I think you need to change your code to this:

 NewBlock = CreateDefaultSubobject<UChildActorComponent>(TEXT("PieceBlock0"));

 if (NewBlock)
 {
     const TSubclassOf<APuzzleBlock> BlockClass = APuzzleBlock::StaticClass();
     NewBlock->SetChildActorClass(BlockClass);
 }

Note: The “StaticClass” static member function returns the UClass of the object. It’s really useful in cases where you need to change the class of an actor being spawned, for instance.

Hope this helps!