today I created a capsule component via the blueprint menu (add component) and I was able to set my skeletal mesh component as the parent for this new capsule. I was also able to get a dropdown menu when clicking on the option “Parent Socket” and chose to whatever socket/bone I want to attach this new capsule.
However after creating the same capsule with c++ I was not able to use the dropdown menu and it was plain empty.
Here is my code and a screenshot.
.h:
/**Hitbox used to determine headshots */
UPROPERTY(VisibleDefaultsOnly, Category = Camera)
UCapsuleComponent* HitBox_Head;
This is a known issue that is currently open in our tracking database (UE-20519). It is possible to attach the component to a socket by specifying the socket in code. Here is an example from Character.cpp: FP_Gun->AttachTo(Mesh1P, TEXT("GripPoint"), EAttachLocation::SnapToTargetIncludingScale, true);
This is a really old question and I believe now you can just do this now (in case anyone else is wondering):
HitBox_Head->SetupAttachment(GetMesh(), "head");
Where "head" is the name of the socket you would have selected in the Parent Socket dropdown.
“However after creating the same
capsule with c++ I was not able to use
the dropdown menu and it was plain
empty.”
This is because you can not set or change the socket on inherited components (in your case the inherited component being the HitBox_Head). If you hover over the magnify glass next to Parent Socket it will tell you this. Hope this helps!
It doesn’t work, at least when called in actor constructor.
Object is attached to parent, but “Parent Socket” field is still null in editor and transform won’t be relative to passed socket
HeadMesh = CreateDefaultSubobject(TEXT("HeadMesh"));
HeadMesh->SetupAttachment(BodyMesh, FName(TEXT("NeckSocket"))); // THIS DOESN'T WORK!
EDIT: errata corrige, it works! you only need to assign the skeletal mesh to the parent component in the c++ constructor and then attach the child component (this is obvious, but I didn’t get it at first)
USkeletalMesh* BodySkeletalMesh = UFunctionLibrary::GameData->PcBodySkeletalMesh; // in our project, game data is a static var to access game singleton where skeletal mesh asset is stored
BodyMesh->SetSkeletalMesh(BodySkeletalMesh);
HeadMesh->SetupAttachment(BodyMesh, FName(TEXT("NeckSocket")));
inside the character constructor, and it adds the neck_01 socket as a parent, performing as expected in level editor, but parent socket field still shows up as null on the blueprint