I have made a c++ class for picking up weapon (a gun or a close range weapon), but it only works for picking up the first item from each class. For example if I place two guns from the same blueprint class onto the level, I can only pick up one of them. Once I drop the first gun I picked up (first gun is destroyed), I cannot pick up the second gun.
I am thinking that there might be a logical error in my picking-up function:
TArray<AActor*> Weapons;
this->GetOverlappingActors(Weapons);
UE_LOG(LogTemp, Warning, TEXT("Picking up"));
if (Weapons.Contains(Gun))
{
UE_LOG(LogTemp, Warning, TEXT("Has Gun"));
for (int i = 0; i < Weapons.Num(); i++)
{
AGun* const GunItems = Cast<AGun>(Weapons[i]);
if (GunItems && !GunItems->IsPendingKill() && GunItems->ActorHasTag("Pickable"))
{
GunItems->AttachToComponent(GetMesh(), FAttachmentTransformRules::SnapToTargetIncludingScale, FName("HoldWeapon"));
bGun = true;
/*bDropped = false;*/
Weapons.Empty();
UE_LOG(LogTemp, Warning, TEXT("Gun Picked up"));
}
}
}
if (Weapons.Contains(CloseWeapon))
{
UE_LOG(LogTemp, Warning, TEXT("Has Melee"));
for (int i = 0; i < Weapons.Num(); i++)
{
AItem* const CloseItems = Cast<AItem>(Weapons[i]);
if (CloseItems && !CloseItems->IsPendingKill() && CloseItems->ActorHasTag("CloseRange"))
{
CloseItems->AttachToComponent(GetMesh(), FAttachmentTransformRules::SnapToTargetNotIncludingScale, FName("HoldWeapon"));
bMelee = true;
/*bDropped = false;*/
Weapons.Empty();
UE_LOG(LogTemp, Warning, TEXT("Melee Picked up"));
}
}
}
Weapon will be destroyed once the player drop it, and player can only hold one weapon at a time.
I have also clear the array after each pick up, is there anything I missed that is causing the problem?