Trouble using AttachToComponent()

I have an Equipment class to represent items that can be picked up and put into the players hand. I want to use Actor->AttachToComponent() to do this.

void APlayerCharacter::EquipItem(AEquipment* Equipment)
{
        if (Equipment == nullptr) return;
	
	UE_LOG(LogTemp, Warning, TEXT("Equipping %s"), *Equipment->GetActorNameOrLabel());

	FName Socket = TEXT("RightHandGripPoint");

	FAttachmentTransformRules Rules = FAttachmentTransformRules(EAttachmentRule::SnapToTarget, false);

	Equipment->SetSimulatePhysicsAndCollision(false);
	
	Equipment->AttachToComponent(GetMesh(), Rules, Socket); 
}

I’m really not sure what part of the code is causing this issue. I know that there are usually a set of common issues that I’ve gone over and double checked aren’t the issue, these would be…

  • Ensuring the socket name is correct (used copy paste and validated by eye).
  • Equipment is definitely not a nullptr, the UE_LOG confirms that the interacted item is the one I want.
  • I’ve seen that it’s important to turn off the items physics and collision before attaching to the interacting character, so this is done here…
void AEquipment::SetSimulatePhysicsAndCollision(bool bSimulatePhysics)
{
	if (Mesh)
	{
		Mesh->SetSimulatePhysics(bSimulatePhysics);

		if (bSimulatePhysics)
		{
			Mesh->SetCollisionResponseToAllChannels(ECollisionResponse::ECR_Block);
		}
		else
		{
			Mesh->SetCollisionResponseToAllChannels(ECollisionResponse::ECR_Overlap);
		}
		
	}
}

Any help getting the equipment to attach properly is appreciated. I’m 99% sure the problem lies in the EquipItem() function, since the interface that calls this function handles every other case exactly as intended.

When attaching the item, are you attaching it from the root component? (Is the mesh the root or is there another parent component?)

The mesh isn’t the root component no. I’ve found a solution which I’m really unsure of why it works.

I was calling EquipItem() from a self-defined InventoryComponent using Parent->EquipItem().

It turns out that calling this code from the character class and the inventory class produces two different behaviours, with the characters behaviour giving me what I want.

1 Like