Ammo is not picked up if character spends ammo at the pickup respawn point

Problem

  • Get up to the pickup respawn point with full ammo

  • Shoot (one shot is enough)

  • Pickup can't be taken

Solution

  • Create an array of pointers to APawn in the base pickup:

UPROPERTY()
TArray<APawn*> OverlappingPawns;
  • In NotifyActorBeginOverlap, if it was not possible to take the pickup, we add the pointer to the pawn to the array:

void ASTUBasePickup::NotifyActorBeginOverlap(AActor* OtherActor)
{
    Super::NotifyActorBeginOverlap(OtherActor);

    const auto Pawn = Cast<APawn>(OtherActor);
    if (GivePickupTo(Pawn))
    {
        PickupWasTaken();
    }
    else if (Pawn)
    {
        OverlappingPawns.Add(Pawn);
    }
}
  • In tick function (you can also make a custom timer), iterate through the array and see if we can give someone a pickup from the saved pointers in the array, and if we can, then we give it to:

void ASTUBasePickup::Tick(float DeltaTime)
{
    Super::Tick(DeltaTime);

    AddActorLocalRotation(FRotator(0.0f, RotationYaw, 0.0f));

    for (const auto OverlapPawn : OverlappingPawns)
    {
        if (GivePickupTo(OverlapPawn))
        {
            PickupWasTaken();
            break;
        }
    }
}
  • Override the NotifyActorEndOverlap function (which is called, as you might guess, when the actor exits the collision) and in it we remove the pawn pointer from the array:

void ASTUBasePickup::NotifyActorEndOverlap(AActor* OtherActor)
{
    Super::NotifyActorBeginOverlap(OtherActor);

    const auto Pawn = Cast<APawn>(OtherActor);
    OverlappingPawns.Remove(Pawn);
}

Commit in repository

Last updated

Was this helpful?