# 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:

```cpp
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:

```cpp
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:

```cpp
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:

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

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

[Commit in repository](https://github.com/life-exe/UnrealEngine/commit/0da7b42056072fd2bd01d67aaeed26720ceb6e0b)


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://lifeexe-art.gitbook.io/unreal-engine-c-course-eng/lessons/lecture-079/ammo-is-not-picked-up-if-character-spends-ammo-at-the-pickup-respawn-point.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
