How do I attach two actors to another in C++?

Hi,

I am new to UE4 but not new to programming. (PLCs, Robots, Vision-Software and other automation stuff)

I started with the “Components and Collision” tutorial with no problem. For the further questions I made an Actor that Orbits around it’s root. No problem so far. But I can’t attach the collidig pawn to my Orbit.
As I figured out you can use AttachToActor or UChildActorComponent.
I couldn’t get it to work with AttachToActor so I tried it as a UChildComponent.

My header:

UChildActorComponent* Orbit;
TSubclassOf<AOrbit> OrbitClass;

My constructor:

Orbit = CreateDefaultSubobject<UChildActorComponent>(TEXT("Orbit"));
Orbit->AttachTo(SphereVisual);
Orbit->SetupAttachment(SphereVisual);
Orbit->SetChildActorClass(OrbitClass);
Orbit->CreateChildActor();

I tried with attach to and without but the orbit didn’t show up.
It works per drag and drop in the UE.

What’s the failure and does it have to do with if it is registered?

Thanks for your help,
Chris

Hey Chris!

Because of reflection (and blueprint system) system construction time attachment reflected only at engine startup or when you create/inherit new blueprint class from this class… (so in short c++ classes are created when you inherit a new BP class from it)

But since UChildActorComponent is just a component and his job to create a new actor, the newly created actor would be definitly not attached because it would be created at runtime i think :slight_smile: (maybe you need reattach it after object is spawned)

Also you wont see UChildActorComponent* Orbit because you missing the reflection macro to make it visible in blueprint (you can read about here: A new, community-hosted Unreal Engine Wiki - Announcements - Unreal Engine Forums , and here: Properties | Unreal Engine Documentation )

Should look like this

UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "Components)
UChildActorComponent* Orbit;

BTW ue4 has a huge component system, so if you dont want to use a whole new actor as orbit, you can use lot of other components, like ustaticmeshcomponent…
you can attach this components at construction time and you can work with them :slight_smile: