Why does my spare ammo go into the negatives?

My issue is that whenever I hit ‘R’ to reload the gun, my current ammo increases and my spare ammo decreases. However when I was debugging, I found that my spare ammo variable would go into the negatives. Now I was instantly aware why, I set my current ammo variable to be equal to the gun’s magasine size however my problem is I tried many different ‘equations’ that have not given me a correct solution. It is probably something obvious. I am NEW to this so don’t hate on me too hard :stuck_out_tongue:

void ReloadGun () {

		AmmoToReloadFrom = AmmoToReloadFrom - (MagSize - CurrentAmmo); //Subtract the number of bullets fired from the magasine from the spare ammo
		AmmoShot = MagSize - CurrentAmmo;
		CurrentAmmo = MagSize;
	}

This is my ReloadGun() function, the line that needs ‘fixing’ is CurrentAmmo = MagSize;

Thanks for any help at all possible, any extra information that may be required, just let me know and I will edit :slight_smile: Cheers.

I supose that if you try to reload when AmmoToReloadFrom do not have enought ammo to reload a full magazine.

so you need something like

if (AmmoToReloadFrom < (Magsize-CurrentAmmo))
{
CurrentAmmo += AmmoToReloadFrom;
AmmoToReloadFrom = 0;
}
else
{
your current code
}

Bye!

//number of bullets required
int diff = magazineSize - currentAmmo;
//total bullet mines

    if (diff > totalAmmo)
    {   
        currentAmmo = totalAmmo;
        totalAmmo = 0;
    }
    else{
        currentAmmo = magazineSize;;
        totalAmmo -= diff;
    }

I think that you should put a conditional about if you wasted some ammos and then another conditional about if the spare ammo is 0 then not reload the gun or only reload with the remaining ammos.