Help with Burst Fire code

Hey there! I had this code working when it was like this:
[lua]
function SWEP:PrimaryAttack()
if not self:CanPrimaryAttack() then return end
local fire = function()
self:EmitSound ( self.Primary.Sound )
self:ShootBullet(20, 2, 1, 0)
self:TakePrimaryAmmo( 1 )
self.Owner:FireBullets( bullet )
self.Weapon:SendWeaponAnim( ACT_VM_PRIMARYATTACK ) --View model animation
self.Owner:MuzzleFlash() --Crappy muzzle light
self.Owner:SetAnimation( PLAYER_ATTACK1 )
self.Owner:ViewPunch( Angle( math.random( -.5, -.3),
math.random( -.2, .2),
math.random( -.3, .3)))
end
timer.Create(“burst”, 0.1, 3, fire)
self:SetNextPrimaryFire( CurTime() + .5 )
end
[/lua]
But it had a slight delay before firing, and I figured it was because the function is within the primary attack, I tried moving it out:
[lua]
local fire = function()
self:EmitSound ( self.Primary.Sound )
self:ShootBullet(20, 2, 1, 0)
self:TakePrimaryAmmo( 1 )
self.Owner:FireBullets( bullet )
self.Weapon:SendWeaponAnim( ACT_VM_PRIMARYATTACK ) --View model animation
self.Owner:MuzzleFlash() --Crappy muzzle light
self.Owner:SetAnimation( PLAYER_ATTACK1 )
self.Owner:ViewPunch( Angle( math.random( -.5, -.3),
math.random( -.2, .2),
math.random( -.3, .3)))
end

function SWEP:PrimaryAttack()
if not self:CanPrimaryAttack() then return end
timer.Create(“burst”, 0.1, 3, fire)
self:SetNextPrimaryFire( CurTime() + .5 )
end
[/lua]
but know it doesn’t know what self is, and I don’t know how to define it. Any help?

You need the function otherwise the game has no idea what you are doing. Replace the “self:SetNextPrimaryFire( CurTime() + .5 )” with self:SetNextPrimaryFire( CurTime() + 0).

I’ve tried that already, even with that set to 0 there’s still a delay between left clicking and the gun firing. I’m stumped as to what is causing the delay.

This isn’t the way to add burst… Don’t make timers in SWEP code, bad idea. Additionally, you need to use IsFirstTimePredicted( ) because of how SWEPs work… Quick lesson, feel free to add on Steam; I tutor too ( free )…

CLIENT executes SWEP functions in a quick burst, ie the client spams the function calls until the server and client sync. Not my favorite thing… Using if ( !IsFirstTimePredicted( ) ) then return; end prevents code from running except on that one instance of client and server in sync.

Second, client and server both need to call the function and they both need the same result… I made an old burst system for TTT weapons which worked great, but the sound was off; didn’t always play the 3rd shot for the client; that’s been fixed and it was my fault. I was doing things incorrectly, and the client and server weren’t in sync.

I’ll be releasing my new weapon base in the very near future with my TTT rewrite as part of my AcecoolDev Skeletonized Dev Base which supports a lot… Safe, Semi, Burst 2 shot, Burst 3 shot, Auto. Additionally, my burst mode uses the FN CAL and HK style systems and how all burst weapons operate… The weapon only fires while the trigger is depressed… If in burst, the weapon stops firing only when the trigger is released, or the burst count is reached… which ever comes first. On mouse release, the burst count resets ( FN CAL and HK style systems ).

The first COLT M16 didn’t reset the counter unless all 3 were shot, so you can shoot 1 of the 3, or 2 of the 3 and then only get 2 or 1 next pull… FN CAL which was out around the same time if I recall correctly, or earlier, had a proper clock reset mechanism, so when the trigger was released the 3 rounds would be available for next burst…

So, what are you doing wrong?

Timers, not a good idea… If you want the bullets to be shot like that, you can use the bullet.Num. But, that doesn’t do the delay…

Let me know what you’re expecting out of this… Do you want the shots to fire at once, do you want a click-release bang-bang-bang like CS:S Glock? It isn’t realistic, but you could do that with timer, but not the same way.

If you need to do it on timers, call the shoot part, not the primary attack…


//
// Timer method; repeating code; could use local recursive...
// Also doesn't prevent shooting if ammo out...
//
self:ShootBullet( 15, 1, 0.03 );
timer.Simple( 0.05, function( )
	self:ShootBullet( 15, 1, 0.03 );
	timer.Simple( 0.05, function( )
		self:ShootBullet( 15, 1, 0.03 );
	end );
end );

Now, if you want to set it up so that it acts just like a real gun would… ie… shoot only until burst number is up, or the trigger is released… whichever comes first… Let me know and I can walk you through it.

The reason your new code doesn’t work, is because you declared the function OUTSIDE of SWEP… self won’t exist there. Make it SWEP:BurstFire( ) and use your timer.blah function( ) self:BurstFire( ) end ); … But, it is sloppy… Feel free to add me on Steam, I’ll help you get it working either way; realistic or CS:S style without slop.

There’s a great article on the wiki about prediction, I’d recommend reading through it. http://wiki.garrysmod.com/page/Prediction

Thanks! I’ve added you on steam ( IntrovertedIntrovert ). I’ve been using the timer thing for now. It’s still a little iffy. Hopefully we can clear things up? :slight_smile: