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

**URL:** <https://forums.unrealengine.com/t/how-to-create-a-tree-of-actors-in-c/478887>\
**Category:** C++\
**Tags:** child-actors, attach-to-actor, unreal-engine, CPP, question\
**Created:** [February 16, 2021, 3:51pm UTC](https://forums.unrealengine.com/t/how-to-create-a-tree-of-actors-in-c/478887 "2021-02-16T15:51:01Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Niriel](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@Niriel](https://forums.unrealengine.com/u/Niriel)\
**Post date:** [February 16, 2021, 3:51pm UTC](https://forums.unrealengine.com/t/how-to-create-a-tree-of-actors-in-c/478887/1 "2021-02-16T15:51:01Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![Kehel18](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@Kehel18](https://forums.unrealengine.com/u/Kehel18)\
**Post date:** [February 16, 2021, 5:09pm UTC](https://forums.unrealengine.com/t/how-to-create-a-tree-of-actors-in-c/478887/2 "2021-02-16T17:09:40Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![Niriel](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@Niriel](https://forums.unrealengine.com/u/Niriel)\
**Post date:** [February 16, 2021, 5:16pm UTC](https://forums.unrealengine.com/t/how-to-create-a-tree-of-actors-in-c/478887/3 "2021-02-16T17:16:08Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![Kehel18](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@Kehel18](https://forums.unrealengine.com/u/Kehel18)\
**Post date:** [February 16, 2021, 5:19pm UTC](https://forums.unrealengine.com/t/how-to-create-a-tree-of-actors-in-c/478887/4 "2021-02-16T17:19:56Z")

</div>

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](https://docs.unrealengine.com/en-US/ProductionPipelines/ScriptingAndAutomation/Blueprints/index.html)

---

<div class="post-metadata">

**Author:** ![Kehel18](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@Kehel18](https://forums.unrealengine.com/u/Kehel18)\
**Post date:** [February 16, 2021, 5:31pm UTC](https://forums.unrealengine.com/t/how-to-create-a-tree-of-actors-in-c/478887/5 "2021-02-16T17:31:12Z")

</div>

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