How to get location of child skeletal mesh component?

I have a character with about 10 skeletal mesh components that are all underneath the “Mesh(Inherited)” component. They are things like the head, chest, boots, gloves, etc… of the character. I’m trying to access the location of the “Chest” skeletal mesh component with C++. How can I get a reference to it and get it’s location?

If it involves array, providing sample syntax code would be great. I’m just beginning to learn the Unreal API. Thanks.

You created the sub skeletal meshes in the blueprint, right? In that case you will have to find them in C++ by name, for example like this (untested):



for (USkeletalMeshComponent * Comp : GetComponents<USkeletalMeshComponent>())
{
if (Comp->GetFName() == FName("Chest"))
{
// do stuff
break;
}
}


Depending on your setup it could be nicer though to declare the components in C++ and then fill in the values in the blueprint, that way you have direct pointers that you can use instead of soft referencing by their name.

Ok, I think I got it to work now. I just had to experiment a little bit to get the classes and syntax correct. And the “GetFName()” functions that you gave me worked out well. I ended up creating an array of type “USceneComponent” which I was able to then populate with the “GetChildrenComponents()” function.

Here is part of my code that seems to work:


// Array that will be populated mainly by child skeletal mesh components via a GetChildrenComponents function
 TArray<USceneComponent*> SkeletalMeshComponentArray2;

// Reference the main skeletal mesh component on the NPC
   USkeletalMeshComponent * SkeletalMeshComp1 = EnemiesInSphereRadius[ActorIndex]->FindComponentByClass<USkeletalMeshComponent>();

   // Populate the "SkeletalMeshComponentArray2" array with all of the child skeletal meshes under the main one
   SkeletalMeshComp1->GetChildrenComponents(true, SkeletalMeshComponentArray2);



for (int32 ActorIndex2 = 0; ActorIndex2 < SkeletalMeshComponentArray2.Num(); ++ActorIndex2)
    {
     // If the currently iterated scene component has the name "SK_Chest" then use that to get it's location.
     if (SkeletalMeshComponentArray2[ActorIndex2]->GetFName() == FName("SK_Chest"))
     {

      ChestLocationOfEnemy = SkeletalMeshComponentArray2[ActorIndex2]->GetComponentLocation();
     }
    }

For now, it seems to be working, I have to test it some more, but I’m pretty sure it is getting the skeletal mesh chest location now. Thanks for your help! :slight_smile:

1 Like