Hello,
While setting up a player character in C++ I was trying to attach some SceneComponents (in that particular case a CameraComponent) to a specific bone/socket of the characters skeletal mesh.
The problem was, that I didn’t want to further specify my skeletal mesh in C++ but rather create a blueprint out of my C++ class and assign the mesh there (in order to keep my C++ code clear of any assets).
So I was basically trying to use a socket in my C++ constructer, that didn’t exist at that point of time. It simply didn’t work.
/** Non-functional cpp-code */
FName MySocket = FName(TEXT("SocketName"));
// This socket won't exist until the BP derived from this class is set up in the editor
MyCamera = CreateDefaultSubobject<UCameraComponent>(TEXT("MyCamera"));
MyCamera->AttachTo(GetMesh(), MySocket);
// NOTE: Will replace AttachTo() soon by AttachToComponent()
Things started to get weird when another user suggested to create a second camera (with identical properties) and attach it to my mesh as well.
/** This DID work however! */
FName MySocket = FName(TEXT("SocketName"));
MyCamera = CreateDefaultSubobject<UCameraComponent>(TEXT("MyCamera"));
MyCamera->AttachTo(GetMesh(), MySocket);
Camera2 = CreateDefaultSubobject<UCameraComponent>(TEXT("Camera2"));
Camera2->AttachTo(GetMesh(), MySocket);
It worked, both cameras behaved like originally intended and sticked to the assigned socket. I would just like to understand why so I can work around this problem in the future (allways creating spare-components isn’t a solution). The user who suggested this workaround blamed this behaviour on an engine bug.
Could that be the case? Or did I cause this error?
If you need more details, feel free to take a look at the original quetion:
Thanks in advance!