I don’t know if you already solved this or not but this one is also a solution you can use.
in your GameCharacter.h, create a variable of AGun* (You can also use another type for the item) and a public setter to set the Gun.
UCLASS()
class YOUR_API AGameCharacter : public ACharacter
{
...
private:
UPROPERTY(...)
class AGun* OverlappingGun;
public:
void SetOverlappingGun(AGun* Gun);
...
}
in the GameCharacter.cpp,
void AGameCharacter::SetOverlappingGun(AGun* Gun)
{
OverlappingGun = Gun;
}
And in the AGun.cpp, in the Begin and EndOverlap, you can set the overlapping gun of the character there.
void AGun::OnBeginOverlap(....)
{
// Make sure the one who overlap is the character
AGameCharacter* Char = Cast<AGameCharacter>(OtherActor);
if (Char)
{
Char->SetOverlappingGun(this);
}
}
void AGun::OnEndOverlap(....)
{
// Make sure the one who overlap is the character
AGameCharacter* Char = Cast<AGameCharacter>(OtherActor);
if (Char)
{
Char->SetOverlappingGun(nullptr);
}
}
Lastly, you just need to check the OverlappingGun value in the PickUp() function (dont forget to also bind the PlayerInput).
void AGameCharacter::SetupPlayerInputComponent(class UInputComponent* PlayerInputComponent)
{
// Set up gameplay key bindings
check(PlayerInputComponent);
PlayerInputComponent->BindAction("PickUp", IE_Pressed, this, &AGameCharacter::PickUp);
}
void AGameCharacter::PickUp()
{
// Check if there is overlapping gun
if (OverlappingGun)
{
// Pick up the gun
}
}
you can just put your pickup logic inside the PickUp() after checking if OverlappingGun is valid.