Download

AttachToActor from Blueprint works, not from C++

I reduced my problem to the following: I have two static mesh spheres, as the subject states, I can use ‘AttachToActor’ from the level blueprint to make ‘Sphere2’ the child of ‘Sphere1’ and the world outliner shows that state correctly.

From C++, If I use an Actor iterator to find both spheres and call ‘AttachToActor’ the call seems to succeed (no errors from stepping through the engine code) but the action does not take place.

Also what is odd, is that once I ‘stop’ the editor gameplay THEN Sphere2 goes under Sphere1 once I am back in the editor.

Is there something reason the gameplay state would not be updated by calling ‘AttachToActor’ from C++ but does work from BP ?

Thanks,
Jason

PS I should mention I am using engine version 4.23.1.

I should mention I am using engine version 4.23.1.

Hello Jason!

I am not sure if this is what you are looking for or trying to do, but I think you need to be using AttachToComponent(), instead of AttachToActor(). Normally you would create a USceneComponent and assign it as your root component, from there you can attach further components to your root component. Here is some sample code as an example:

This would go in your .cpp constructor:


    // Scene & Root Component
    MySceneComponent = CreateDefaultSubobject<USceneComponent>(TEXT("MySceneComponent"));
    RootComponent = MySceneComponent;

    // Create a Static Mesh Component and attach to the Root Component
    MyMesh1 = CreateDefaultSubobject<UStaticMeshComponent>(TEXT("MyMesh1"));
    MyMesh1->AttachToComponent(RootComponent, FAttachmentTransformRules::KeepRelativeTransform);

    // Create another Static Mesh Component and attach to the Root Component
    MyMesh2 = CreateDefaultSubobject<UStaticMeshComponent>(TEXT("MyMesh2"));
    MyMesh2->AttachToComponent(RootComponent, FAttachmentTransformRules::KeepRelativeTransform);

Now you have two meshes attached to your root component that you can configure as you see fit. I might be giving you an answer that isn’t quite what you are looking for, so I dug this up as it might be more in line with your question: Howto procedurally create child actors (properly)? - Unreal Engine Forums

Hope this helps! Have a great day.

Thank you for the response. I am pretty sure I tried ‘AttachToComponent’ as well but I will give it a second look. Basically I am trying to take two already existing actors, which means I am not at the moment creating them dynamically, and make one the child of another at run-time. As I mentioned I am able to do it via a Blueprint but not via C++ which is odd since I usually have parity between those.

Perhaps if I manually extract both root components and do this as the component level maybe it will work better.

I will report back, thanks for the help.
Jason