Actor not attaching to a Socket

Hi everyone,

I’m trying to attach a sword to my character’s hand.
In order to do this,i’m using this function:

void ABaseCharacter::SpawnWeaponInCharacterHand(FName HandSocketName)
{
	FActorSpawnParameters spawnParams;
	spawnParams.Owner = this;	
	SwordActor = GetWorld()->SpawnActor<ABaseSword>(ABaseSword::StaticClass(),spawnParams);
	SwordActor->AttachRootComponentToActor(this, HandSocketName, EAttachLocation::SnapToTarget, true);
	GEngine->AddOnScreenDebugMessage(-1, 15.0f, FColor::Red, HandSocketName.ToString());
}

The problem is that the sword,is attached to the character’s root bone instead of the HandSocket like it should.

If i instead use the “Attach Actor To Component” function from the character blueprint,the result is correct:
Blueprint:

21663-bp.png

Result:

Note that i checked the function parameter with the debug message,and the bone name is actually right.
So the question is…why it works on blueprint side,but not on C++?
Am i doing something wrong?

Thanks in advance.

If you take a look at the code for the Blueprint node which is functioning correctly, you’ll see this:

AttachRootComponentTo(InParent, InSocketName, AttachLocationType, bWeldSimulatedBodies);

This is what I’d try calling from your native code.
It looks like AttachRootComponentToActor always wants to attach to the root of the target actor, based on the comment in Actor.h:

Attaches the RootComponent of this Actor to the RootComponent of the supplied actor, optionally at a named socket.

I haven’t yet had a chance to look at the implementation for AttachRootComponentToActor, but its possible something isn’t quite right there (ie the comment indicates that it can use a socket, but the actual implementation will always just go straight to the root component).

Thanks,worked perfectly!
Yes,seems like there’s something strange about the behavior of the “AttachRootComponentToActor” function.