Search in
Sort by:

Question Status:

Search help

  • Simple searches use one or more words. Separate the words with spaces (cat dog) to search cat,dog or both. Separate the words with plus signs (cat +dog) to search for items that may contain cat but must contain dog.
  • You can further refine your search on the search results page, where you can search by keywords, author, topic. These can be combined with each other. Examples
    • cat dog --matches anything with cat,dog or both
    • cat +dog --searches for cat +dog where dog is a mandatory term
    • cat -dog -- searches for cat excluding any result containing dog
    • [cats] —will restrict your search to results with topic named "cats"
    • [cats] [dogs] —will restrict your search to results with both topics, "cats", and "dogs"

AnswerHub Maintenance

Background maintenance is scheduled to occur between 9 - 11am EDT on Tuesday, May 21. Site operation may be slower than normal during this time and a brief interruption in operation may be observed

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.


         PrimaryActorTick.bCanEverTick = true;
         Count = 10;
         SphereRadius = 100.0f;
         TouchSphere = CreateDefaultSubobject<USphereComponent>(TEXT("TouchSphereComponent"));
         RootComponent = TouchSphere;
         StaticMesh = CreateDefaultSubobject<UStaticMeshComponent>(TEXT("StaticMeshComponent"));
         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;

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


 void AFirstPersonCharacter::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 ?

Product Version: UE 4.19
more ▼

asked Aug 06 '18 at 07:34 AM in C++ Programming

avatar image

21 3 6

(comments are locked)
10|2000 characters needed characters left

1 answer: sort voted first
 AFirstPersonCharacter *FPCharacter = Cast<AFirstPersonCharacter>(OtherActor);
 AMannequin* TPCharacter = Cast<AMannequin>(OtherActor);
 AGun *Gun = Cast<AGun>(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

more ▼

answered Aug 06 '18 at 08:58 AM

avatar image

1.2k 6 5 9

(comments are locked)
10|2000 characters needed characters left
Your answer
toggle preview:

Up to 5 attachments (including images) can be used with a maximum of 5.2 MB each and 5.2 MB total.

Follow this question

Once you sign in you will be able to subscribe for any updates here

Answers to this question