• Auto refilling Ammo
    11 replies, posted
I am currently working on a swep and wondered, what the code for a self-refilling ammo. Here is an example: [url]http://www.youtube.com/watch?v=zmVbVkNATMQ[/url] Someone please tell me?
[CODE] 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) [/CODE]
[editline]11th October 2013[/editline] [QUOTE=vegasx;42492637][CODE] 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) [/CODE][/QUOTE] Thanks, but in which function do I paste that?
[QUOTE=Noah;42492876]Thanks, but in which function do I paste that?[/QUOTE] [url=http://wiki.garrysmod.com/page/Category:WEAPON_Hooks]look at the available hooks and see for yourself[/url] or look at gamemodes\base\entities\weapons\weapon_base
[QUOTE=Luni;42493043][url=http://wiki.garrysmod.com/page/Category:WEAPON_Hooks]look at the available hooks and see for yourself[/url] or look at gamemodes\base\entities\weapons\weapon_base[/QUOTE] 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] [CODE] 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 [/CODE] Just shove that whole bad boy in your swep file or base
[QUOTE=vegasx;42496924]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] [CODE] 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 [/CODE] Just shove that whole bad boy in your swep file or base[/QUOTE] [CODE] 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 [/CODE] 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 [CODE] function SWEP:OnRemove() timer.Destroy("ammoregen" .. self:EntIndex()) end [/CODE]
[QUOTE=vegasx;42497642]Yeah. You'll want to destroy the timer too to prevent lots of timers being in existence. So add this in there too [CODE] function SWEP:OnRemove() timer.Destroy("ammoregen" .. self:EntIndex()) end [/CODE][/QUOTE] Alright, but I still have a different clipsize, and it won't regenerate when shooting :/ [editline]12th October 2013[/editline] [QUOTE=vegasx;42497642]Yeah. You'll want to destroy the timer too to prevent lots of timers being in existence. So add this in there too [CODE] function SWEP:OnRemove() timer.Destroy("ammoregen" .. self:EntIndex()) end [/CODE][/QUOTE] [CODE] 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 ) [/CODE] 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.
[QUOTE=vegasx;42500508]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.[/QUOTE] [CODE] 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 [/CODE] Uhh like this?
Sorry, you need to Log In to post a reply to this thread.