Attaching weapons to sockets

So I am currently trying to attach my weapon/actor to the player socket at the hand. I trace to see if the player is looking at the object, then if the player presses the “Use” button it i call the Use method, where I pass in a AUsableItem Parameter. When I call this I am attempting to attach the item inside the socket that I set up in the skeleton:

void APlayerCharacter::Use(AUsableItem* curItem){
	FVector loc = FirstPersonMesh->GetSocketLocation(FName("RightItemSocket"));
	curItem->AttachRootComponentTo(FirstPersonMesh, FName(TEXT("RightItemSocket")), EAttachLocation::SnapToTarget);


This is the code I found, but it does not work. Wondering if someone else has something that might help me with this?


You can attach Mesh components as follows:


// Constructor

USkeletalMeshComponent* PlayerMesh;

FName PlayerWeaponSocketName = TEXT("YourSocketName");

USkeletalMeshComponent* WeaponMesh;


	// Create PlayerMesh Mesh Component
	PlayerMesh = CreateDefaultSubobject<USkeletalMeshComponent>(TEXT("PlayerMesh"));
	// Find and assign the mesh using constructor helpers
	static ConstructorHelpers::FObjectFinder<USkeletalMesh> PlayerMeshOb(TEXT("SkeletalMesh'/Path/To/Your/Mesh'"));
    if (PlayerMeshOb.Object) 
	// Set PlayerMesh as rootcomponent
	RootComponent = PlayerMesh;
	// Create Weapon Mesh Component
	WeaponMesh = CreateDefaultSubobject<USkeletalMeshComponent>(TEXT("WeaponMesh"));
	// Find and assign the mesh using constructor helpers
	static ConstructorHelpers::FObjectFinder<USkeletalMesh> WeaponMeshOb(TEXT("SkeletalMesh'/Path/To/Your/Mesh'"));
    if (WeaponMeshOb.Object) 
	// Attach Weapon mesh to PlayerMesh
	WeaponMesh->AttachTo(PlayerMesh, PlayerWeaponSocketName);

	// Do other stuff

Can anyone help me with this? I have been searching for an answer for a while

After trying this, and doing:

curItem->getItemMesh()->AttachTo(FirstPersonMesh, FName("RightItemSocket") );

Still does not work, no errors, but when I do GetAttachParentSocketName() it returns none

Also is there a way to attach the whole Actor? like instead of just the mesh?

If the Mesh is the Root Component of the UsableItem then attaching the Mesh to the socket means attaching the UsableItem to the socket. It is the same thing.

Could you elaborate on what you mean by “Still does not work”. Is the Weapon Mesh visible but not attached, or not visible at all?

Thank you, that helps

Well, Like I said, no errors, but this is what happens in the editor:


The object just remains in place, does not move at all

I didnt take off physics, thats why it didnt work