Download

Can you add a Socket to a Skeleton using C++ ?

Hi guys!

I have a question about adding a Socket to a Skeleton using C++. The question in very simple, is it at all doable?

I have a Characteer class set up with a Skeletal mesh that has 5 bones. My code is as follows:

Header:

UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = “Testing”)
USkeletalMeshSocket* mySocket = CreateDefaultSubobject<USkeletalMeshSocket>(TEXT(“mySocket”));

UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = “Testing”)
USkeleton* mySkeleton = CreateDefaultSubobject<USkeleton>(TEXT(“mySkeleton”));

UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = “Testing”)
UPhysicsAsset* myPhysAsset = CreateDefaultSubobject<UPhysicsAsset>(TEXT(“myPhysAsset”));

=====

CPP:

//Skeletal Mesh
static ConstructorHelpers::FObjectFinder<USkeletalMesh> MyActor2_Component1_Visual(TEXT("/Game/Meshes/myCharacter"));
if (MyActor2_Component1_Visual.Succeeded())
{
GetMesh()->SetSkeletalMesh(MyActor2_Component1_Visual.Object);
}

=====

The UskeletalMeshSocket, Uskeleton and UphysicsAsset are added because I am experimenting and trying different stuff out. However, I feel like I have searched and tried everything under the sun. Is it doable?

I attached a picture showing what I am trying to do. In the picture I have used Blueprint to add a socked to my Skeleton, but I am trying to learn how to do it in code. Is it doable? I have thoroughly searched and tried a million things with no luck so I am here asking you guys.

Thank you in advance!

Any idea anyone?

Ok, solved it on my own, posting the solution here for anyone who comes across this post that it might help.

The Solution:

Header File:

//Skeleton setup
UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = “Testing”)
USkeleton* mySkeleton = CreateDefaultSubobject<USkeleton>(TEXT(“mySkeleton”));
//Socket Setup
UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = “Testing”)
USkeletalMeshSocket* mySocket = NewObject<USkeletalMeshSocket>(mySkeleton);

===============

CPP file:

//Skeletal Mesh. Mesh NOT socket
staticConstructorHelpers::FObjectFinder<USkeletalMesh> MyActor2_Component1_Visual(TEXT("/Game/Meshes/myCharacter"));
if (MyActor2_Component1_Visual.Succeeded())
{
GetMesh()->SetSkeletalMesh(MyActor2_Component1_Visual.Object);
}

//Skeleton Component
staticConstructorHelpers::FObjectFinder<USkeleton> mySkeleton_Visual(TEXT("/Game/Meshes/myCharacter_Skeleton"));
if (mySkeleton_Visual.Succeeded())
{
mySkeleton = mySkeleton_Visual.Object;
}

mySkeleton->Sockets.Empty(); //Clears all sockets
mySkeleton->Modify(); //Not sure what this does but it is needed.
mySocket->SocketName =“mySocket”; //Obviously sets the sockets name
mySocket->BoneName =“Bone_004_end”; //Attaches to the bone you choose
mySkeleton->Sockets.Add(mySocket); //Finally adds the socket to the skeleton

Tested and works, hope it helps someone.