Ammo Limit, Save the Rest

Alright, I found a lot of threads about limiting ammo, but I want to make sure whatever you’re getting the ammo from doesn’t disappear.
Well, for instance… in Half-Life 2, a npc drops an AR2 with 30 rounds in it. You only have 45 rounds in reserve, out of the 60 limit. When you walk over that weapon you take 15, but the weapon stays there. That weapon is still holding 15 rounds!

I want something like that. The player picks up whatever they don’t have in stock, but they leave the rest in the weapon. How could that be done?

I am open to spawning another swep with less ammo if need be.

yust gife the entity(the gun) values equal with the weaponstock when they drop them…

Post code or something from the entity that gives you the ammo?

For example, modifying the base TTT ammo so it keeps the remaining ammo:
[lua]function ENT:Touch(ent)
if SERVER and self.taken != true then
if (ent:IsValid() and ent:IsPlayer() and self:CheckForWeapon(ent) and self:PlayerCanPickup(ent)) then

     local ammo = ent:GetAmmoCount(self.AmmoType)
     -- need clipmax info and room for at least 1/4th
     if self.AmmoMax >= (ammo + math.ceil(self.AmmoAmount * 0.25)) then
        local given = self.AmmoLeft or self.AmmoAmount
        given = math.min(given, self.AmmoMax - ammo)
        ent:GiveAmmo(given, self.AmmoType)
	if self.AmmoLeft then
		self.AmmoLeft = self.AmmoLeft - given
	else
		self.AmmoLeft = self.AmmoAmount - given
	end
	if self.AmmoLeft <= 0 then
		self:Remove()
		self.taken = true
	end

        -- just in case remove does not happen soon enough
        
     end
  end

end
end[/lua]

It’s not a gun, but I want to limit the supply.

I like the idea Ozy, but I’m not sure I understand how to work that in. This swep has a default clip size, 32. How would I prevent the swep from disappearing, give the player the amount they need, and subtract that amount from the weapon. If the weapon is empty kill it.

Bear in mind at the same time someone else could pick this thing up as a weapon, not as ammo.

[editline]19th January 2014[/editline]

It would be placed in a level, maybe I should instead make a sent.
Touch it and if you have the weapon it’ll do the ammo math. If the player doesn’t have the weapon it would give them the weapon and kill itself. It will still be resting there even if it has 0 supply.

Normally weapons don’t give ammo automatically, you have to code that in manually. The only reason the default sweps do it is because it’s defined in C++ code. Basically what you want is right there in the code I just gave you, just look at the rest of the base_ttt_ammo entity code and you can basically copy paste it into your SWEP.

Actually, in my experience sweps do give you ammo on pickup.
I got my problem solved perfectly using a sent for pickup, rather than the weapon itself. Thank you, though!

[editline]19th January 2014[/editline]

You can’t spawn a swep from the q menu if you already have it, but if you use the console “ent_create” function you can make more of them

[editline]19th January 2014[/editline]

This wasn’t about picking up ammo, this was about limiting how much you can have, AND keeping the weapon on the ground even after you pick it up. Normally when you touch the swep it disappears and gives you the default clip size in ammo. If you don’t already have it you’ll get it as if you used the q menu.