Adding Ammo Count to Testing Grounds

Hey all,

I’ve been attempting to add this functionality for quite some time now. However, it seems that I’m overlooking something. Maybe someone could help me out.

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 game crashes. 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 || FPCharacter || Gun)
	{
		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();
}

The OnReload() function in Gun.cpp still is not complete, but again whenever I run into the ammo pickup static mesh object the game crashes, it is suppose to log out a message then destroy itself. Does anyone know why its behaving this way?

I tried debugging the game and ended up with this, both complaining that Gun was a nullptr, not sure what to do from here.

1 Like

Did you ever figure it out?

Privacy & Terms