Default weapon Spawn causing issues

Dear experts,

I am spawning a default weapon for the main character in C++ but when I start playing the weapon is at location x 0 y 0 z 0. I am trying it to attach to the right hand socket:

sword_faraway

void AAdventureCharacter::SpawnDefaultWeapon()
{	
	//check the default subclass
	if(DefaultWeaponClass)
	{
		//spawn weapon
		AWeapon* DefaultWeapon = GetWorld()->SpawnActor<AWeapon>(DefaultWeaponClass);


		//get the right hand socket
		const USkeletalMeshSocket* RightHandSocket = this->GetMesh()->GetSocketByName(FName("hand_rSocket"));

		if(RightHandSocket)
		{
			//attach the weapon to the right hand socket
			RightHandSocket->AttachActor(DefaultWeapon,GetMesh());
		}
		//set equipped weapon to the newly spawned weapon
		EquippedWeapon = DefaultWeapon;
	}
}

The character I am using is modular and consists of SkeletalMeshComponents and when I start playing I get this error: LogSkinnedMeshComp: Warning: GetSocketByName(hand_rSocket): No SkeletalMesh for Component(CharacterMesh0) Actor(AdventureCharacterBP_C_0)

I think somehow the

USkeletalMeshSocket* RightHandSocket

is not recognized. Is that correct?

Thank you, Peter

Have you tried using DefaultWeapon->AttachToComponent(...)?

Hi, sorry, my late reply. I figured it out and now it works. I am using USkeletalMeshComponent for different parts of the character, so I have to refer to “Glove” and then use the Socket on that like this:

if(Glove)
		{
			const FTransform Transform = Glove->GetSocketTransform(FName("hand_rSocket"),ERelativeTransformSpace::RTS_World);
			DefaultWeapon = GetWorld()->SpawnActor<AWeapon>(DefaultWeaponClass,Transform,SpawnParams);
			DefaultWeapon->AttachToComponent(Glove,
				FAttachmentTransformRules(EAttachmentRule::SnapToTarget,EAttachmentRule::KeepRelative,EAttachmentRule::KeepRelative,false),
				FName("hand_rSocket"));
		}

I was on the wrong track before trying to attach to a UStaticMeshComponent