AttachToComponent called twice before actually snap to socket

Hi all,

In my custom ACharacter class I have a simple method as the one below:

void AMyCharacter::ActivateInteraction()
{
	if(CurrentInteractableItem)
	{
		if(AMyEquip* Equip = Cast<AMyEquip>(CurrentInteractableItem.GetObject()))
		{
			Equip->AttachToComponent(GetMesh(), FAttachmentTransformRules::SnapToTargetIncludingScale, TEXT("ToolSocket"));
		}
	}
}

I cannot figure out what is wrong but it essentially needs to be executed twice before attaching the Equip to the skeletal mesh.

And with “need to be” I intend that in debug mode i see that on first call this line of code get called and executed

Equip->AttachToComponent(GetMesh(), FAttachmentTransformRules::SnapToTargetIncludingScale, TEXT("ToolSocket"));

but it does nothing.
If I call it again the Equip snaps to the socket.

I’m sure is something trivial I’m missing :slight_smile: any help will be really appreciated.

Othere relevant (or maybe not) infos are:

  • CurrentInteractableItem is a TScriptInterface;
  • GetMesh() came from USkeletalMeshComponent::GetMesh();
  • I also tried FName(TEXT(“ToolSocket”)) instead of TEXT(“ToolSocket”) as SocketName without success.

Thanks in advance

Luca

Sockets names are usually FName, not FText; if this code works, maybe FText can be implicitly converted to FName. You can try FName(“ToolSocket”), but I don’t think that’s the issue, because if socket is not found, it will normally get attached to the component origin instead.

Yepp that’s not the problem. To double check I tried your suggestion and nothing changed. The behavioural strangeness is that the equip attach himself to the skeletal socket (so the socket is always recognized) but only on second attempt. The first attempt always fails

I actually did solve the problem changing the AttachmentTransformRules from this one:

Equip->AttachToComponent(GetMesh(), FAttachmentTransformRules::SnapToTargetIncludingScale, TEXT("ToolSocket"));

with this one

Equip->AttachToComponent(GetMesh(), FAttachmentTransformRules(EAttachmentRule::SnapToTarget, true), TEXT("ToolSocket"));

I think the true boolean did the job (it refers to bInWeldSimulatedBodies ).

Hope it helps anyone in my same situation :slight_smile: