Logic For Reloading

Hello!

Currently creating some basic reloading logic.
This is what Im dealing with so far:

            if(Input.GetButtonDown("Reload") && _item.currentMag != _item.magSize)
            {

                _item.currentMag += _item.magSize;
                _item.Extraammo -= _item.magSize;
            }

The problem I am facing is probably quite obvious. The Extra ammo will go below 0.
How would I Prevent it from going below 0. So if my mag size is 30.
And then I have 20 bullets left. Then I reload and Only have 5 extra ammo.
How will I make it end up to 25 and not 30?
And how do I prevent it from taking 30 If i only add 8 bullets forexample?

Mathf.Max(0, _item.magSize) will return the larger of the two numbers. If magSize > 0, then magSize is returned, otherwise 0 is returned.

Code example?

OK, so to break it all down, what your script currently does is;

If (the reload button is pressed, and the current rounds are not equal to the magazine’s capacity),
{
Add a magazine’s capacity worth of bullets to your current rounds,
then subtract a magazine’s capacity worth of bullets from your reserve ammunition.
}

This means you’ll be squishing too many bullets into your gun, as firing once and reloading will leave you with 59 bullets to fire instead of just putting in one and giving you 30.

Instead, take a full magazine’s capacity and subtract your current rounds from it to find out how many bullets are required to top up your magazine. Then you want to check if you actually have that many bullets in your reserve ammunition - so, if (you have more than or equal to that number) {chuck that number of bullets in your gun by adding that number to the current rounds, and subtracting the same number from your reserve ammunition. Else (if you don’t have enough bullets in your reserve ammunition) {then only add however many you have left in your reserve ammunition to your current rounds}. You could also add an extra bit of the if statement for when you have no reserve ammunition at all - there’s no point replacing your empty magazine with another empty magazine after all.

In a script it’d look something like this;

if (Input.GetButtonDown("Reload") && currentRounds != magazineCapacity)
{
   // Figure out how many bullets are needed to top up your magazine.
   var roundsNeeded : int = magazineCapacity - currentRounds;

   if(roundsNeeded <= reserveAmmunition)
   {
     // Add as much as you can fit in the magazine, and remove that amount from your reserve.
     currentRounds += roundsNeeded;
     reserveAmmunition -= roundsNeeded;
   }

   else if (reserveAmmunition == 0)
   {
     // No reloading, because no bullets are left.
   }

   else
   {
     // Just add what you have left then.
     currentRounds += reserveAmmunition;
     reserveAmmunition = 0;
   }
}
1 Like

Strictly speaking there are of course other ways to do this that would take fewer lines of code, and which would otherwise be better in various ways. I’d also usually put some debug conditions in there too so weird logic irons itself out (or it at least tells you what’s going on), but I figure the logic behind this is a bit more comprehensible this way.

1 Like

Works perfectly. Thanks

Wolfie, you explained this like a champ! Thank you!