How to attach inherited SceneComponent to a socket?

Hi everyone!

I’m a bit of a newbie at C++ and UE4, so I’ve been working on my knight/paladin character recently, and I’ve come across an error, hope you guys can help me out.

So, on my character, I have a scenecomponent that I have declared and initialized in my C++ code which holds 2 static meshes, a sword and a sheath(sword cover), I want that scenecomponent to attach to a socket of my choice at compile time and also at runtime(when a key is pressed(in the future)), I have also created a blueprint from that character code of mine. If I click on the scenecomponent in the blueprint editor there is no text box where I can type in the socket name unlike when I create a scenecomponent from inside the Blueprint editor, it will have a text box to type in the Socket name. So I tried attaching the scenecomponent to the socket from inside my constructor after initializing it. But as it seems now, it didn’t really work, I can say that because when I run the game, the sword and the sheathe stay still in a place while my character plays the run animation instead of moving with the character, I mean it does move with the character, but it does not stay attached to its waist like its blueprint initialized counter part(another scenecomponet with a static mesh of a sword in it which I added directly in the blueprint editor).

Here is my constructor code, hope you guys can help me:-

    //Setup the components
    //Root = CreateDefaultSubobject<USceneComponent>(TEXT("Root"));
    //RootComponent = Root;

    //Seathe Socket
    Seathe_Socket = CreateDefaultSubobject<USceneComponent>(TEXT("Seathe_Socket"));
    Seathe_Socket->SetupAttachment(GetMesh(), ("SwordInSeatheSocket"));

    //SwordInHand Socket
    SwordInHand_Socket = CreateDefaultSubobject<USceneComponent>(TEXT("SwordInHand_Socket"));

    LongSword = CreateDefaultSubobject<UStaticMeshComponent>(TEXT("LongSword"));

    LongSwordSeathe = CreateDefaultSubobject<UStaticMeshComponent>(TEXT("LongSwordSeathe"));

Are you sure “GetMesh()” is initialized by the time you set up attachments to it? May be something to confirm.

It would help if you could share your class definition/header file as well.

1 Like