Need help with Tarray

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?

The main problem here, is that you are declaring your Weapons array as a local variable to your function so it is created each time the function is called, and destroyed when the function exits.

You need to make your Weapons array a member of your class.

2 Likes

Hi, thank you for the reply!
I have made the array a class variable and have the GetOverlappingActors function moved in side of the Tick function, but the problem still persists. I can pick up the DamageItem (blueprint name of the CloseWeapon), drop it, and then pick it up again by commented out the Destroy function, but if I drag the blueprint onto the level again and create a DamageItem2, the character would not pick up DamageItem2. (same to the gun).
Additionally, I created a UE_LOG to print out “Looping” right below the cast:

AGun* const GunItems = Cast<AGun>(Weapons[i]);

before the if statement. Same goes to the CloseWeapon.
“Looping” will be printed out fine when picking up DamageItem and Gun, but would not print if I try to pick up DamageItem2 and Gun2, is there anything else I have missed?

BTW, I only have one DamageItem BP, so all the properties and tags should remain unchanged for DamageItem2. (gun as well).

Just a quick update, I kind of bypass the problem by using the SwingSingleByChannel with sphere, instead of using array for overlapping actors. Still couldn’t quite figure out why the array was failing, but at least it is working right now