Auto refilling Ammo

I am currently working on a swep and wondered, what the code for a self-refilling ammo. Here is an example: http://www.youtube.com/watch?v=zmVbVkNATMQ

Someone please tell me?



local maxammo = 50

timer.Create("ammoregen" .. self:EntIndex(), 0.1, 0, function()
     if IsValid(self) then
          if CurTime() > self:GetNextPrimaryFire( ) then
               if self:Clip1() < maxammo && self:Clip1() > 0 then
                    self:TakePrimaryAmmo(-1)
               elseif self:Clip1() == 0 then
                    self:SetClip1(1)
               end
          end
     end
end)


[editline]11th October 2013[/editline]

Thanks, but in which function do I paste that?

look at the available hooks and see for yourself

or look at gamemodes\base\entities\weapons\weapon_base

Can’t you just tell me where these codes belong to, please? I can’t find anything

just put it in a autorun folder.

If you’re putting it in a SWEP, you’ll want to add it to the SWEP:Initialize() function in the SWEP.

[editline]12th October 2013[/editline]




function SWEP:Intialize()
local maxammo = 50

timer.Create("ammoregen" .. self:EntIndex(), 0.1, 0, function()
     if IsValid(self) then
          if CurTime() > self:GetNextPrimaryFire( ) then
               if self:Clip1() < maxammo && self:Clip1() > 0 then
                    self:TakePrimaryAmmo(-1)
               elseif self:Clip1() == 0 then
                    self:SetClip1(1)
               end
          end
     end
end)
end


Just shove that whole bad boy in your swep file or base




function SWEP:Initialize()

	// other initialize code goes here

	if CLIENT then
	
		// Create a new table for every weapon instance
		self.VElements = table.FullCopy( self.VElements )
		self.WElements = table.FullCopy( self.WElements )
		self.ViewModelBoneMods = table.FullCopy( self.ViewModelBoneMods )

		self:CreateModels(self.VElements) // create viewmodels
		self:CreateModels(self.WElements) // create worldmodels
		self:SetWeaponHoldType( self.HoldType )
		
		local maxammo = 5

timer.Create("ammoregen" .. self:EntIndex(), 0.1, 0, function()
     if IsValid(self) then
          if CurTime() > self:GetNextPrimaryFire( ) then
               if self:Clip1() < maxammo && self:Clip1() > 0 then
                    self:TakePrimaryAmmo(-1)
               elseif self:Clip1() == 0 then
                    self:SetClip1(1)
               end
          end
     end
end)
end



Like this?

Yeah. You’ll want to destroy the timer too to prevent lots of timers being in existence.

So add this in there too



function SWEP:OnRemove()
timer.Destroy("ammoregen" .. self:EntIndex())
end


Alright, but I still have a different clipsize, and it won’t regenerate when shooting :confused:

[editline]12th October 2013[/editline]



function SWEP:Equip()

    self.Weapon:EmitSound("weapons/ioncannon/ioncannon_get.wav")

	if SERVER then
		if self.Owner:IsPlayer() then 
			self.Owner:GiveAmmo(10, "CombineCannon")
		end
		umsg.Start("SetNextPrimaryFire", self.Owner)
			umsg.Float(CurTime() + self.Primary.Delay)
		umsg.End() 
	end
end

function SWEP:PrimaryAttack()
	if SERVER then
	
	local maxammo = 10
	if self.Owner:GetAmmoCount("CombineCannon") == 0 then return end
	self:SetNextPrimaryFire(CurTime() + self.Primary.Delay)
	self:SendWeaponAnim( ACT_VM_PRIMARYATTACK );
	self:TakePrimaryAmmo( 1 )
	self.Owner:SetAnimation( PLAYER_ATTACK1 )
	self.Owner:SetVelocity(self.Owner:GetForward() * -200 )


There’s something with “Ammo”, maybe that’s the problem. But I have no idea

Well since your weapon is only dealing with clips, stop trying to use GiveAmmo and functions like that, your best friends will be SetClip1(), Clip1(), and TakePrimaryAmmo()

If you want to regenerate ammo while shooting, you’ll have to remove the line “if CurTime() > self:GetNextPrimaryFire( ) then” and its corresponding end inside of the timer we created earlier.



timer.Create("ammoregen" .. self:EntIndex(), 0.1, 0, function()
     if IsValid(self) then
               if self:Clip1() < maxammo && self:Clip1() > 0 then
                    self:TakePrimaryAmmo(-1)
               elseif self:Clip1() == 0 then
                    self:SetClip1(1)
               end
          
       end
end)
end


Uhh like this?