Adding Ammo Count to Game

I’ve been attempting to add this functionality for quite some time now. However, it seems that I’m overlooking something.

So I want to add an ammo count, a single ammo count similar to that of Quake or Doom. With ammo pickups distributed throughout the map. The gun can fire up to 10 times then stops, but when I attempt to reload by overlapping with an ammo pickup the the static mesh does not respond. The static mash parent is a blueprint (actor) its parent class is ammocrate.cpp (below) which is also a actor. This is what I’ve done so far.

AmmoCrate.cpp

AAmmoCrate::AAmmoCrate()
    {
        PrimaryActorTick.bCanEverTick = true;

        Count = 10;
        SphereRadius = 100.0f;

        TouchSphere = CreateDefaultSubobject<USphereComponent>(TEXT("TouchSphereComponent"));
        TouchSphere->InitSphereRadius(SphereRadius);
        TouchSphere->SetCollisionProfileName("Trigger");
        RootComponent = TouchSphere;

        StaticMesh = CreateDefaultSubobject<UStaticMeshComponent>(TEXT("StaticMeshComponent"));
        StaticMesh->SetupAttachment(RootComponent);

        TouchSphere->OnComponentBeginOverlap.AddDynamic(this, &AAmmoCrate::OnOverlapBegin);
    }

    void AAmmoCrate::OnOverlapBegin(class UPrimitiveComponent* OverlappedComp, class AActor* OtherActor, class UPrimitiveComponent* OtherComp, int32 OtherBodyIndex, bool bFromSweep, const FHitResult& SweepResult)
    {
        AFirstPersonCharacter *FPCharacter = Cast<AFirstPersonCharacter>(OtherActor);
        AMannequin* TPCharacter = Cast<AMannequin>(OtherActor);
        AGun *Gun = Cast<AGun>(OtherActor);

if (TPCharacter)
{
    Gun->AmmoPool = Gun->AmmoPool + Count;

    this->Destroy();
}

Afterwards I created a OnReload() function similar to the OnFire() function, where I call it from the First Person Character.

FirstPersonCharacter.cpp

void AFirstPersonCharacter::OnReload()
{
    Gun->OnReload();
}

I’s set up a UE_LOG call on the OnReload function it does not even log out that the ammo crate has been overlapped with, what am I missing here ?

AFirstPersonCharacter FPCharacter = Cast(OtherActor);
AMannequin
TPCharacter = Cast(OtherActor);
AGun *Gun = Cast(OtherActor);

Now I am not quite sure what your object hierarchy is but only one of these will ever succeed as OtherActor is of a single type (AFirstPersonCharacter, AMannequin or AGun ).

  • This means that if TPCharacter is set → Gun is not set and you can’t get AmmoPool.
  • If Gun is set → TPCharacter is not set and it can’t pass through the “if” statement.

You should somehow get your AGun variable from inside of FPCharacter or TPCharacter (not sure which one holds it in your game)

Happy coding