How to create a tree of actors in C++?

I wish to instantiate at runtime a bunch of actors and parent them properly, in C++.

Problem: my code crashes UnrealEngine with an

Unhandled Exception: EXCEPTION_ACCESS_VIOLATION reading address 0x0000000000000148

Let’s say I create a Pawn that’s a robotic arm. To keep it very simple:

RobotPawn:
Base (Actor, probably just a cube)
    HipPivot (Actor, just for the socket/anchor/transform)
        Torso (Actor, prolly a cylinder)
            ShoulderPivot (Actor, just for the socket/anchor/transform))
                Arm (Actor, another cylinder)

The hip pivot turns left-right, the shoulder pivot turns up/down.

How do I create this hierarchy of actors at runtime? What I tried crashed everything.

So far so good:

On my Pawn, I defined ChildActorComponents for the three body parts (base torso arm) and the two pivots (hip and shoulder).

RobotBase = CreateDefaultSubobject<UChildActorComponent>("Base");
RobotHip = CreateDefaultSubobject<UChildActorComponent>("Hip");
RobotTorso = CreateDefaultSubobject<UChildActorComponent>("Torso");
RobotArm = CreateDefaultSubobject<UChildActorComponent>("Arm");
RobotShoulder = CreateDefaultSubobject<UChildActorComponent>("Shoulder");

I understand that these are all components, not actors. The Hip and Shoulders are empty actors, just used as points/transforms where the body parts are attached. However, the Base, Torso and Arm have their own C++ class because I want to build several robots by subclassing these parts.

RobotBase->SetChildActorClass(ARobotBase::StaticClass());
RobotHip->SetChildActorClass(AActor::StaticClass());
RobotTorso->SetChildActorClass(ARobotTorso::StaticClass());
RobotShoulder->SetChildActorClass(AActor::StaticClass());
RobotArm->SetChildActorClass(ARobotArm::StaticClass());

And then of course, call ->CreateChildActor() on each component.

So far so good: when I bring my pawn into the world, it creates five objects of the right class. Problem: they’re not parented to each other, they’re all created at the top-level of the world. I want to attach the actors.

Now, the hard part: properly attaching the actors.

I want to attach the actors to each other, so I thought of doing this:

RobotHip->GetChildActor()->AttachToActor(RobotBase->GetChildActor(), FAttachmentTransformRules::KeepRelativeTransform);

Boom, that crashes the editor.

So, I am doing it wrong, am I not?
I’m new, I just can’t find the tutorials or documentation to get this to work.

Thanks for the TSubclassOf, that’s super useful. So, thanks to these properties, in the editor, I’ll be able to select specific subclasses, right? But then, I won’t be able to see the children in the editor since they’re spawned only when SpawnRobot is called.

I think that you’d better create custom Spawner, that knows how and in which order to attach different parts. Smth like that:

UCLASS()
class ARobotSpawner: public AActor{

GENERATED_BODY()

public:

void SpawnRobot(AActor* parent) const { ... // do some stuff }

protected:

UPROPERTY(EditAnywhere, Category = "Robot Spawner", BlueprintReadWrite)
TSubclassOf<AActor> HeadClass;

UPROPERTY(EditAnywhere, Category = "Robot Spawner", BlueprintReadWrite)
TSubclassOf<AActor> NeckClass;

...

UPROPERTY(EditAnywhere, Category = "Robot Spawner", BlueprintReadWrite)
TSubclassOf<AActor> FootClass;

};

As for attach error - when you create actors with UChildActorComponent, they are already attached, so this is smth strange from UE point of view.

That’s true, the main idea - to call it when you need, after that they will appear. During Play for example. Do you need to edit them in Editor mode? If yes you can try smth like Call in Editor from Scripting the Editor using Blueprints | Unreal Engine Documentation

Here it is )) https://answers.unrealengine.com/questions/1000550/dynamically-spawning-actors-in-the-editor.html