How to attach component to socket with C++ at compile time?

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:-

//Constructor
APlayerCharacter::APlayerCharacter()
{
	//Setup the components
	//Root = CreateDefaultSubobject<USceneComponent>(TEXT("Root"));
	//RootComponent = Root;
	
	//Seathe Socket
	Seathe_Socket = CreateDefaultSubobject<USceneComponent>(TEXT("Seathe_Socket"));
	Seathe_Socket->SetupAttachment(GetMesh(), FName (TEXT("SwordInSeatheSocket")));
	
	

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

	//LongSword
	LongSword = CreateDefaultSubobject<UStaticMeshComponent>(TEXT("LongSword"));
	LongSword->SetupAttachment(Seathe_Socket);

	//LongSwordSeathe
	LongSwordSeathe = CreateDefaultSubobject<UStaticMeshComponent>(TEXT("LongSwordSeathe"));
	LongSwordSeathe->SetupAttachment(Seathe_Socket);
	}

This is the way it can be done

In your .h file create an pointer to the weapon class

e.g
class MySwordClassActor* CWeapon;

In your Character/Pawn/Knight Class call the weapon class

#include <MySwordClassActor.h>

void KnightClass::OnBeginPlay()
{
    CWeapon = //Spawn the weapon of MySwordClass into the world
    

    //Attach CWeapon to The socket location
}

OnBeginPlay is not a valid function. its BeginPlay.

MyBad its BeginPlay()

You have just commented out stuff, could you tell me what lines of code to actually use to do the things you commented out?

1 Like

You have just commented out stuff, could you tell me what lines of code to actually use to do the things you commented out?

And the weapon is just a static mesh component, not an actor.

In the .h file of Pawn

public :

//Your Socket name that you added in persona to the character for editable via blueprint
UPROPERTY(EditDefaultsOnly, Category = "Socket")
	FName CustomSocket;

//Your Knight or Paladin Mesh on which the attachment os to be done
	UPROPERTY(EditDefaultsOnly, Category = "HatMesh")
	class USkeletalMeshComponent* PlayerMesh;

//The Mesh that is to be attached onto the player
	UPROPERTY(EditDefaultsOnly, Category = "HatMesh")
	class UStaticMeshComponent* HatMesh;

In .cpp file of Pawn

//Constructor function
AMyPawn::AMyPawn()
{
 	// Set this pawn to call Tick() every frame.  You can turn this off to improve performance if you don't need it.
	PrimaryActorTick.bCanEverTick = true;

//Adding a default socket name
	CustomSocket = "PropSocket";

//Creating the player mesh and setting it as root(Generally root is the collision capsule)
	PlayerMesh = CreateDefaultSubobject<USkeletalMeshComponent>(TEXT("PlayerMesh"));
	RootComponent = PlayerMesh;

//Creating the mesh  to be attached
	HatMesh = CreateDefaultSubobject<UStaticMeshComponent>(TEXT("HatMesh"));
	HatMesh->SetMobility(EComponentMobility::Movable);
	HatMesh->SetupAttachment(RootComponent);
}

// Called when the game starts or when spawned
void AMyPawn::BeginPlay()
{
	Super::BeginPlay();
	
//Attaching the mesh to the player
	HatMesh->AttachTo(PlayerMesh, CustomSocket, EAttachLocation::SnapToTarget, false);

}