Attaching Components from Different actors

Hey,

So ive got an actor that has a mesh component attached to it. (Actor1)
Ive got a second actor that also has a mesh component attached to it. (Actor2)

the second actor is spawned at runtime, and id like to attach it’s mesh component to be a child of the first actors mesh.

Ive tried the following:

Actor2->Mesh->AttachTo(Actor1->Mesh, NAME_None, EAttachLocation::SnapToTarget);
Actor2->Mesh->AttachParent = Actor1->Mesh;

And indeed inside the editor i can see that the second actor appears as a child of the first:
I.e:

   Actor1
     -> Actor2

However when i move Actor1’s Mesh:

Actor1->Mesh->SetWorldLocation(Somewhere);

The second actors mesh simply doesnt follow it!
Any hints?

Cheers!

Do you register new mesh component to the Actor1? I think there is function inside AActor that does the registering, Look at registering component in here
Beside, I really cant understand why you would like to attach a component of an actor to another component of an other actor, it seems a violation to philosophy of actor class.

“ActorComponents are automatically registered when the owning Actor is spawned” - Both actors are spawned in the scene, so i dont think its a registration problem (Unless you mean doing some sort of cross-registration?)

As for the cross-attaching, the tl:dr version is that its the only way i could think of doing it,

(the above is a simplified example, but basically Actor1 has 2 meshes, and there are many Actor2’s with 2 meshes each, which need to follow the appropriate mesh on Actor1)

yes they are automatically registered, but to their initiator actor, I guess you have to register them again to the new actor.(just guessing)

Have you tried

Actor2->AttachRootComponentToActor(Actor1, NULL, EAttachLocation::KeepRelativeOffset, false);

Aye, that’s what I went with the in end, ( by making the meshs on Actor1 and Actor2 be thier own actor’s with just a mesh component on it, and storing the reference via a pointer).

then that method worked perfectly, (sadly it didnt work initially as it would only attach to the root component of each actor, and i needed the cross attachment i mentioned in the other comment)

Seems a bit overengineered, but as long as it works ammiright?

Thanks for both your help, i guess it was just the wrong way of doing things!

(If you want to convert your comment to an answer ill mark it as correct)

  • Cheers!

I converted it, thnx :smiley: