Hi All! Im having a little issue with my weapon class. So far ive got a parent class that handles all the firing and shooting functionality for all weapons. I can create child blueprint classes based on this parent class and then modify requirements for each weapon such as weapon mesh, the damage, the sounds etc etc. I now want to implement an ammo function into the base weapon class but i have no idea how i should go about doing it. Ive got the decrement ammo function working and thats called whenever the player shoots the weapon. Im just confused as to how id go about doing the reload and when id call it.
Ive attached a screenshot of my BaseWeapon blueprint in this message as well as the decrement ammo function.
If you haven’t already, you’ll want to have a variable that stores how big the weapon’s mag size is. Then in your reload function check the current amount of ammo in the mag to the mag size, and if it’s less set current ammo to the max mag size. In your Fire Weapon custom event, I would then call the reload function after Stop Fire Weapon. Just remember that with this method when Fire Weapon is called and the Ammo in Mag is 0, the reload function will automatically be called. Also, I would make an ActionMapping for reloading at anytime so that the player does not have to wait for an empty mag to reload.
The MaxMagCapcity holds the maximum ammo in the mag
the TotalAmmo holds the Total Ammo in the gun so thats the total of all the ammo in each mag
and the AmmoInMag is the current ammo being used. This is the one thats being decremented in my Decrement Ammo Count function.
Okay so in the reload function you will want to set AmmoInMag to MaxMagCapacity if AmmoInMag is less than MaxMagCapacity. With this you will be able to call the reload function at any time due to the if check. I do have some questions though. First is TotalAmmo the amount of ammo available for the weapon? If so where are you decreasing that value when the weapon fires? Is that what clamp does in your Decrease Ammo Count function? What I’m getting at is where do you want available ammo for the weapon to decrease? You can either do this in the reload function itself or Decrease Ammo Count (which might look a little confusing at first but still achieves the same thing).
Okay so the TotalAmmo variable holds all the ammo in the gun. For example lets say my assault rifle showed 20/80.
80 would be the TotallAmmo in the gun to use and 20 would be the current ammo that is in the mag - This is the one thats being decremented in the Decrement Ammo Function. When the player presses the left mouse button this is being decremented to 0 and stops them from firing and they would then need to reload the *weapon.
Ive got the reload function semi working.
Just a little confused as to how to minus the reloaded amount from the total amount.
After reviewing what I said, I’m going to have to redo my explanation which I don’t have time for right now. There is a lot more needed in the reload function than I first envisioned. I can give you an explanation later unless somebody else does before I do.
EDIT: If you want a simple way to remove ammo from TotalAmmo, in your Decrease Ammo Count function just subtract one from TotalAmmo if TotalAmmo does not equal 0. Whenever you fire, a bullet is removed from TotalAmmo as well as AmmoInMag. This does look a little confusing as whenever you reload TotalAmmo doesn’t change but it still works. A example of a game that does this is Payday 2.
What I was trying to explain removes ammo from TotalAmmo when you reload and is a lot more in-depth.
Just saw your edit. I wanted it so that the ammo is removed from the TotalAmmo when i reload. So for example lets say i shot 5 times, then 5 would be removed from the total ammo and added to the current ammo.
iamjustagamer75 had the same idea as me so I think I can explain it:
AmmoClipSize is your MaxMagCapacity, CurrentAmmo is your AmmoInMag, CurrentMaxAmmo is your TotalAmmo, and AmmoNeededForReload is an entirely new variable composed of the difference between MaxMagCapacity and AmmoInMag. This variable is basically how many bullets you have fired from the magazine. So if your magazine size is 20 and you fire 10 bullets, then when you reload this variable would be a value of 10. Next we check if the TotalAmmo is less than or equal to AmmoNeededForReload. The reason for this check is to see if there are more bullets in the mag than there are in your ammo pool (Say you have a situation where your rifle shows 20/10). If this check is true, then we add the remaining TotalAmmo to AmmoInMag and set TotalAmmo to 0 since we used all of it (Say for our 20/10 example, if you fire all 20 bullets in your mag then you need 20 bullets to reload. Since our TotalAmmo is 10 which is less than 20, we can only add the remaining TotalAmmo to our magazine, thus leaving us with 10/0). Now back to the branch, if TotalAmmo is greater than AmmoNeededForReload we just perform a basic reload where we take how many bullets were fired (AmmoNeededForReload) and remove that amount from TotalAmmo, then set the AmmoInMag to our MaxMagCapacity (So if we have 20/80, and fire only 10 bullets, the AmmoNeededForReload is 10 and after calculations we get 20/70).
Hope this helped and thank you iamjustagamer75 for your work.
Thank You Both! Zadeon and Iamjustagamer75!
that works exactly as intended… just have a small issue. How can i prevent the player from attempting to reload either when the player has max ammo or when ammo is 0.
Also another issue ive found… when i get to the last mag. im only able to shoot once even if it says i have 30 shots left.
Video to show what i mean:
Well thank your for the kind words, I cant really go that far on a forum post. I answer some little one off questions here and there, but I have a patreon page where the majority of my tutorials on things like this live.
I can point you in the right direction though, a “canreload” function added before your reload action can stop it from reloading when its not supposed to an example would be…
That’s strange about it only firing one shot on the last mag. What happens if you still have some TotalAmmo left that’s under the mag size like 30/10? I’m guessing it might be a problem with the check for if Total Ammo is less than or equal to ammo needed for reload. Does your reload function automatically call when the mag reaches 0 or is it manually called?