Attaching weapons to sockets

Hello,
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){
	curItem->SetOwner(this);
	FVector loc = FirstPersonMesh->GetSocketLocation(FName("RightItemSocket"));
	curItem->AttachRootComponentTo(FirstPersonMesh, FName(TEXT("RightItemSocket")), EAttachLocation::SnapToTarget);
	curItem->SetActorLocation(loc);

}

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

Thanks,

You can attach Mesh components as follows:

Header:

// Constructor
YourPlayer();

UPROPERTY()
USkeletalMeshComponent* PlayerMesh;

UPROPERTY()
FName PlayerWeaponSocketName = TEXT("YourSocketName");

UPROPERTY()
USkeletalMeshComponent* WeaponMesh;

Cpp:

YourPlayer::YourPlayer()
{
	// 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) 
	{
        PlayerMesh->SetSkeletalMesh(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) 
	{
        WeaponMesh->SetSkeletalMesh(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:

61616-weaponattaching.png

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

I didnt take off physics, thats why it didnt work