void AShooterCharacter::EquipWeapon(AWeapon* WeaponToEquip)
{
// Ignore collision with the weapon’s components
WeaponToEquip->GetAreaSphere()->SetCollisionResponseToAllChannels(ECollisionResponse::ECR_Ignore);
WeaponToEquip->GetCollisionBox()->SetCollisionResponseToAllChannels(ECollisionResponse::ECR_Ignore);
// Determine which socket to attach the weapon to based on the current equipped weapon
FName SocketName;
if (EquippedWeapon == nullptr)
{
SocketName = FName("RightHandSocket");
}
else
{
// You can implement your logic here to choose the appropriate socket for the new weapon
// For example, if EquippedWeapon is already equipped, you might want to attach the new weapon to a different socket
SocketName = FName("SecondaryWeaponSocket");
}
// Get the socket by name
const USkeletalMeshSocket* WeaponSocket = GetMesh()->GetSocketByName(SocketName);
if (WeaponSocket)
{
// Attach the weapon to the socket
WeaponSocket->AttachActor(WeaponToEquip, GetMesh());
}
else
{
// Handle the case where the socket doesn't exist (e.g., attach to a default location or log an error)
}
// Broadcast the equipped weapon information
if (EquippedWeapon == nullptr)
{
EquipItemDelegate.Broadcast(-1, WeaponToEquip->GetSlotIndex());
}
else
{
EquipItemDelegate.Broadcast(EquippedWeapon->GetSlotIndex(), WeaponToEquip->GetSlotIndex());
}
// Update the equipped weapon reference and its state
EquippedWeapon = WeaponToEquip;
EquippedWeapon->SetItemState(EItemState::EIS_Equipped);
}