Unreal Engine C++ Course | ENG
  • About course
  • Links
    • Git
    • Visual Studio
    • Unreal Engine
  • Unreal Editor Hotkeys
  • Visual Studio Hotkeys
  • Console commands
  • Problems and solutions
    • Unable to start program error
  • Lessons
    • Lecture 065
      • Additive animation upon landing
      • Blocking shooting while running
    • Lecture 079
      • Pickup is visible after the ammo has been taken
      • Ammo is not picked up if character spends ammo at the pickup respawn point
    • Lecture 089
      • NiagaraSystem does not attach to muzzle
    • Lecture 148
    • Lecture 155
  • UE5
  • Automation
    • Code formatting
  • Student projects
Powered by GitBook
On this page
  • Problem
  • Solution

Was this helpful?

  1. Lessons
  2. Lecture 079

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);
}
PreviousPickup is visible after the ammo has been takenNextLecture 089

Last updated 3 years ago

Was this helpful?

Commit in repository