• PrimaryAttack Help
    7 replies, posted
Hey FP, I am working on a SWEP and I've run into a complication. So I went to test a few things and I noticed that it wasn't working exactly as I anticipated. [url]http://wiki.garrysmod.com/page/WEAPON/PrimaryAttack[/url] [lua] function SWEP:PrimaryAttack() if CLIENT then chat.AddText( "CL_FIRE" ) end if SERVER then print( "SV_FIRE" ) end end [/lua] Only the "SV_FIRE" will print. If I were to run [lua] SWEP.Test = 0 function SWEP:PrimaryAttack() self.Test = self.Test + 1 end if CLIENT then hook.Add( 'Tick', 'TestPrimaryAttack', function() chat.AddText( self.Test ) end ) end [/lua] I still only get '0's, but the server value increments. Why wouldn't PrimaryAttack be called on the clientside? Is PrimaryAttack not shared? The wiki says it is [url]http://wiki.garrysmod.com/page/WEAPON/PrimaryAttack[/url] Am I doing something wrong?
- Bump, been a day with no response. I've tried a few things now with no luck. The SWEP contains AddCSLuaFile() ect. Will SinglePlayer effect this? I know SteamID64() returns nil in singleplayer.
Have you tried running the AddText on self.Owner?
[lua] //SWEP:PrimaryFire()\\ function SWEP:PrimaryAttack() if ( !self:CanPrimaryAttack() ) then return end local bullet = {} bullet.Num = self.Primary.NumberofShots bullet.Src = self.Owner:GetShootPos() bullet.Dir = self.Owner:GetAimVector() bullet.Spread = Vector( self.Primary.Spread * 0.1 , self.Primary.Spread * 0.1, 0) bullet.Tracer = 0 bullet.Force = self.Primary.Force bullet.Damage = self.Primary.Damage bullet.AmmoType = self.Primary.Ammo local rnda = self.Primary.Recoil * -1 local rndb = self.Primary.Recoil * math.random(-1, 1) self:ShootEffects() self.Owner:FireBullets( bullet ) self.Weapon:EmitSound(Sound(self.Primary.Sound)) self.Owner:ViewPunch( Angle( rnda,rndb,rnda ) ) self:TakePrimaryAmmo(self.Primary.TakeAmmo) self.Weapon:SetNextPrimaryFire( CurTime() + self.Primary.Delay ) self.Weapon:SetNextSecondaryFire( CurTime() + self.Primary.Delay ) end [/lua] This maybe might help? I dont know why you are using print functions...
Thanks for the thought, but chat.AddText adds text to the LocalPlayer()'s chatbox. It is a clientside function and would only print the text to that clients chatbox. EDIT: I am using print functions as an example here. Currently my issue is that PrimaryAttack isn't happening on Clientside when it is a Shared hook. Basically that is a test function so every-time I shoot the gun it should print "SV_FIRE" in console (which it does) and also print "CL_FIRE" in chat. (Which also appears in console as an orange-y colour in Single Player)
It seems to be a gmod bug, because it should print in both states.
SWEP:PrimaryAttack is only called on client if in multiplayer. The only reason it is called on client is for prediction. [url]http://wiki.garrysmod.com/page/Prediction[/url] You can however force it to be called on client: [code] if ( game.SinglePlayer() ) then self:CallOnClient( "PrimaryAttack" ) end [/code] Note that in multiplayer, SWEP:PrimaryAttack is called multiple times per one "shot" with the gun, because prediction.
That clears a lot up. Thank you. As for the multiple calls, this can be prevented by using IsFirstTimePredicted? Correct? EDIT: Read the link. Helped a lot. Thanks again.
Sorry, you need to Log In to post a reply to this thread.