• Weird SWEP Glitch
    26 replies, posted
So the following is the code I added to cl_init.lua: [lua] function SWEP:Initialize() if self.CanPrimaryAttack then hook.Add("KeyPress", "FOVChangeOnPress", function(ply, key) if (key == IN_ATTACK2) then if (!self.FOVChanged and self.Secondary.IronFOV != 0 and !self.FOVSet) then self.Owner:SetFOV(self.Secondary.IronFOV, 0.2) self.FOVSet = true self.FOVChanged = true end end end) hook.Add("KeyRelease", "FOVChangeOnRelease", function(ply, key) if (key == IN_ATTACK2) then if (self.FOVChanged and self.Secondary.IronFOV != 0 and self.FOVSet) then self.Owner:SetFOV(0, 0.2) self.FOVSet = false self.FOVChanged = false end end end) hook.Add("KeyPress", "FOVChangeOnRunPressed", function(ply, key) if (self.FOVChanged and key == IN_SPEED) then if (self.Secondary.IronFOV != 0 and self.FOVSet and FOVChanged) then self.Owner:SetFOV(0, 0.2) self.FOVSet = false self.FOVChanged = false end end end) hook.Add("KeyPress", "FOVStayOnAttackPressed", function(ply, key) if (self.FOVChanged and key == IN_ATTACK) then if (!FOVChanged and self.Secondary.IronFOV != 0 and !self.FOVSet) then self.Owner:SetFOV(self.Secondary.IronFOV, 0.2) self.FOVSet = true self.FOVChanged = true end end end) end end [/lua] Yes, "SWEP.FOVChanged = false" is in shared.lua Please, no one suggest that I put this in shared. I want the weapon to run as smoothly as possible. So, every time I shoot... The FOV resets, even whenever I'm holding IN_ATTACK2. Please help! NOTE: I only have to press IN_ATTACK, I do not need to release it. NOTE #2: No errors are being displayed.
Anyone know how to fix this?
Guys, I really need this. Please help me out!
Seriously? No one at all? Lol
try putting this outside of the function SWEP:Initialize() function local FOVChanged = false other then that you will have to explain what is going on better then you have
I've added it to shared.lua "SWEP.FOVChanged = false" Also, I'm trying to change the fov when the person presses attack2, and when they release attack2 it should reset the fov. But I want it clientside instead of shared.
(pls dont blame me if im wrong) You have to use self.FOVChanged if you set SWEP.FOVChanged instead of FOVChanged
I think the problem might be that you're creating so many keypress and keyrelease hooks at once when you could just be using one and putting all the code in one hook - something is probably getting overwritten or is being run in the wrong order Also, I think your hooks might get overwritten unless you use self as the unique identifier of them
[QUOTE=MPan1;50006929]I think the problem might be that you're creating so many keypress and keyrelease hooks at once when you could just be using one and putting all the code in one hook - something is probably getting overwritten or is being run in the wrong order Also, I think your hooks might get overwritten unless you use self as the unique identifier of them[/QUOTE] Maybe you could use the "PlayerBindPress" hook.
[QUOTE=MPan1;50006929]I think the problem might be that you're creating so many keypress and keyrelease hooks at once when you could just be using one and putting all the code in one hook - something is probably getting overwritten or is being run in the wrong order Also, I think your hooks might get overwritten unless you use self as the unique identifier of them[/QUOTE] Well, I've only had one keypress and keyrelease hooks... It still didn't work. Also, I'm confused on what you mean when you say "use self as the unique identifier of them". [editline]26th March 2016[/editline] [QUOTE=CupCakeR;50006894](pls dont blame me if im wrong) You have to use self.FOVChanged if you set SWEP.FOVChanged instead of FOVChanged[/QUOTE] This was wrong, you can set a variable instead. However, setting all your main variables are convenient if you do it in shared.lua But, you made me think about doing that so I did. It's pretty convenient.
[QUOTE=Percipience;50011709]Also, I'm confused on what you mean when you say "use self as the unique identifier of them".[/QUOTE] Well, I just mean the second argument for the hook- e.g. [CODE] hook.Add("KeyPress", "FOVChangeOnPress", function(ply, key) [/CODE] The second argument you gave was "FOVChangeOnPress". If another hook was created with that identifier, it'd get overwritten and it wouldn't work, so that's why I think using 'self' - meaning the weapon - would be a good idea (you can use anything for that identifier) There's only one problem with using 'self' in your code though- since you're creating multiple hooks that have the same event for them, they'd all overwrite themselves, so that's why I think it'd be a good idea to make them all one
On a fundamental level, why are you using gamemode hooks in a SWEP? They would run even when you have a different weapon equipped. Since you're setting FOV, all you need to do is define the SWEP's [img]http://wiki.garrysmod.com/favicon.ico[/img] [url=http://wiki.garrysmod.com/page/WEAPON/TranslateFOV]WEAPON/TranslateFOV[/url] hook or [img]http://wiki.garrysmod.com/favicon.ico[/img] [url=http://wiki.garrysmod.com/page/WEAPON/CalcView]WEAPON/CalcView[/url].
[QUOTE=Neat-Nit;50014120]On a fundamental level, why are you using gamemode hooks in a SWEP? They would run even when you have a different weapon equipped. Since you're setting FOV, all you need to do is define the SWEP's [img]http://wiki.garrysmod.com/favicon.ico[/img] [url=http://wiki.garrysmod.com/page/WEAPON/TranslateFOV]WEAPON/TranslateFOV[/url] hook or [img]http://wiki.garrysmod.com/favicon.ico[/img] [url=http://wiki.garrysmod.com/page/WEAPON/CalcView]WEAPON/CalcView[/url].[/QUOTE] What's he supposed to do if he wants to check when a player's pressing W?
This is just making me more confused lol. Would I use swep hooks instead?
[QUOTE=MPan1;50014203]What's he supposed to do if he wants to check when a player's pressing W?[/QUOTE] [img]http://wiki.garrysmod.com/favicon.ico[/img] [url=http://wiki.garrysmod.com/page/Player/KeyDown]Player:KeyDown[/url] + [img]http://wiki.garrysmod.com/favicon.ico[/img] [url=http://wiki.garrysmod.com/page/Player/KeyDownLast]Player:KeyDownLast[/url] Or store the first time KeyDown was found true as a variable like self.start_forward = CurTime() (and obviously reset it when keydown is false)
[QUOTE=Percipience;50014345]This is just making me more confused lol. Would I use swep hooks instead?[/QUOTE] It'd be more ideal- you could use [img]http://wiki.garrysmod.com/favicon.ico[/img] [url=http://wiki.garrysmod.com/page/WEAPON/PrimaryAttack]WEAPON/PrimaryAttack[/url] for the IN_ATTACK bit and [img]http://wiki.garrysmod.com/favicon.ico[/img] [url=http://wiki.garrysmod.com/page/WEAPON/SecondaryAttack]WEAPON/SecondaryAttack[/url], but then there's still the problem of having a hook for the IN_SPEED thing - but you could probably do a [URL="https://wiki.garrysmod.com/page/Player/KeyDown"]keydown[/URL] check in [img]http://wiki.garrysmod.com/favicon.ico[/img] [url=http://wiki.garrysmod.com/page/WEAPON/Think]WEAPON/Think[/url] to take care of that [editline]27th March 2016[/editline] a little bit ninja'd
so, I now have the following code: [lua] function SWEP:Think() hook.Add("KeyPress", "FOVChangeOnRunPressed", function(ply, key) if (self.FOVChanged and key == IN_SPEED) then if (self.Secondary.IronFOV != 0 and self.FOVSet and FOVChanged) then self.Owner:SetFOV(0, 0.2) self.FOVSet = false self.FOVChanged = false print("imrunninlel") end end end) end function SWEP:SecondaryAttack() hook.Add("KeyPress", "FOVChangeOnPress", function(ply, key) if (key == IN_ATTACK2) then if (!self.FOVChanged and self.Secondary.IronFOV != 0 and !self.FOVSet) then self.Owner:SetFOV(self.Secondary.IronFOV, 0.2) self.FOVSet = true self.FOVChanged = true print("hi") end end end) hook.Add("KeyRelease", "FOVChangeOnRelease", function(ply, key) if (key == IN_ATTACK2) then if (self.FOVChanged and self.Secondary.IronFOV != 0 and self.FOVSet) then self.Owner:SetFOV(0, 0.2) self.FOVSet = false self.FOVChanged = false print("bye") end end end) end function SWEP:PrimaryAttack() if (self.FOVChanged and self.Secondary.IronFOV != 0 and !self.FOVSet) then self.Owner:SetFOV(self.Secondary.IronFOV, 0.2) self.FOVSet = true self.FOVChanged = true print("shootinglel") end end [/lua] For some reason, none of the print statements are being executed. I'm so confused right now lol.
I didn't mean to create the hooks in those functions, I meant for you to get the code you're putting in the hooks to run in the functions there - nevermind though, I didn't notice you had a KeyRelease hook, so it wouldn't have worked anyway, so I guess you'll have to undo all that stuff so it was the same as how you originally did it (sorry for making you do that, I just didn't notice you had a different hook, and also I don't actually know what the problem is :ohno:)
[QUOTE=MPan1;50017891]I didn't mean to create the hooks in those functions, I meant for you to get the code you're putting in the hooks to run in the functions there - nevermind though, I didn't notice you had a KeyRelease hook, so it wouldn't have worked anyway, so I guess you'll have to undo all that stuff so it was the same as how you originally did it (sorry for making you do that, I just didn't notice you had a different hook, and also I don't actually know what the problem is :ohno:)[/QUOTE] Fuck... This is weird lol
uhh why would the keyrelease hook make any difference? All this stuff should still be in the SWEP hooks. I'm on mobile right now so I can't type up an example, but I'll try and explain what I meant as soon as I get on a PC. [editline]28th March 2016[/editline] If this is a double-post, could someone please explain to me how and when they get auto-merged? If this one is merged then, good :P On topic: [lua]function SWEP:SecondaryAttack() self.TargetFOV = self.Secondary.IronFOV end function SWEP:TranslateFOV(oldfov) self.CurrentFOV = self.CurrentFOV or oldfov end self.TargetFOV = self.TargetFOV or oldfov end self.LastFOVTime = self.LastFOVTime or CurTime() end if self.TargetFOV == 0 then self.TargetFOV = oldfov end if !self.Owner:KeyDown(IN_ATTACK2) then self.TargetFOV = oldfov end -- Player let go of secondary fire local FOVChangeSpeed = 1 -- tweak this 1 until you get a nice result local changetime = CurTime() - self.LastFOVTime local allowedchange = changetime * FOVChangeSpeed self.CurrentFOV = math.Approach(self.CurrentFOV, self.TargetFOV, allowedchange) self.LastFOVTime = CurTime() return self.CurrentFOV end[/lua] This doesn't handle sprinting - I can't do all the work for you ;) Good luck! Edit: changed to use [img]http://wiki.garrysmod.com/favicon.ico[/img] [url=http://wiki.garrysmod.com/page/math/Approach]math.Approach[/url], when I realized there's a function for it. What I did before that was a manual implementation of math.Approach.
[QUOTE=Neat-Nit;50019373]uhh why would the keyrelease hook make any difference? All this stuff should still be in the SWEP hooks. I'm on mobile right now so I can't type up an example, but I'll try and explain what I meant as soon as I get on a PC. [editline]28th March 2016[/editline] If this is a double-post, could someone please explain to me how and when they get auto-merged? If this one is merged then, good :P On topic: [lua]function SWEP:SecondaryAttack() self.TargetFOV = self.Secondary.IronFOV end function SWEP:TranslateFOV(oldfov) self.CurrentFOV = self.CurrentFOV or oldfov end self.TargetFOV = self.TargetFOV or oldfov end self.LastFOVTime = self.LastFOVTime or CurTime() end if self.TargetFOV == 0 then self.TargetFOV = oldfov end if !self.Owner:KeyDown(IN_ATTACK2) then self.TargetFOV = oldfov end -- Player let go of secondary fire local FOVChangeSpeed = 1 -- tweak this 1 until you get a nice result local changetime = CurTime() - self.LastFOVTime local allowedchange = changetime * FOVChangeSpeed self.CurrentFOV = math.Approach(self.CurrentFOV, self.TargetFOV, allowedchange) self.LastFOVTime = CurTime() return self.CurrentFOV end[/lua] This doesn't handle sprinting - I can't do all the work for you ;) Good luck! Edit: changed to use [img]http://wiki.garrysmod.com/favicon.ico[/img] [url=http://wiki.garrysmod.com/page/math/Approach]math.Approach[/url], when I realized there's a function for it. What I did before that was a manual implementation of math.Approach.[/QUOTE] the TranslateFOV hook is handled server-side correct? if so, I can't use it. I'm working on a client-side initialization script
Nope, it's actually clientside-only. You can see on the wiki that it has an orange icon - that means client. Blue means serverside, and half-blue-half-orange obviously means it runs on both. Admittedly it's pretty bad that the wiki doesn't explain this icon anywhere...
[QUOTE=Neat-Nit;50020997]Nope, it's actually clientside-only. You can see on the wiki that it has an orange icon - that means client. Blue means serverside, and half-blue-half-orange obviously means it runs on both. Admittedly it's pretty bad that the wiki doesn't explain this icon anywhere...[/QUOTE] Oh, fuck, that's right. I get the colors confused all the time. Thanks mate. I'll try it out, and report back. [editline]28th March 2016[/editline] On your script - delete the "end" on your variables lel. But apart from that there's no errors. However, it doesn't change the FOV. [editline]28th March 2016[/editline] Changed "oldfov" to 0, and deleted the argument from the translatefov hook. This is fun... [url]https://vid.me/uDsN[/url]
[QUOTE=Percipience;50021294]On your script - delete the "end" on your variables lel. But apart from that there's no errors. However, it doesn't change the FOV.[/QUOTE] Whoops! Well surely you realized that the code I posted was untested :P I can't really think of a reason why it wouldn't work... can you print the return value just before you return it and see what you get? (with my code) Or spam it in the chat. Actually let me just type it up: [lua]function SWEP:TranslateFOV(oldfov) self.CurrentFOV = self.CurrentFOV or oldfov self.TargetFOV = self.TargetFOV or oldfov self.LastFOVTime = self.LastFOVTime or CurTime() if self.TargetFOV == 0 then self.TargetFOV = oldfov end if self.Owner:KeyDown(IN_ATTACK2) then self.TargetFOV = self.Secondary.IronFOV else self.TargetFOV = oldfov end local FOVChangeSpeed = 20 -- tweak this until you get a nice result local changetime = CurTime() - self.LastFOVTime local allowedchange = changetime * FOVChangeSpeed self.CurrentFOV = math.Approach(self.CurrentFOV, self.TargetFOV, allowedchange) self.LastFOVTime = CurTime() chat.AddText("FOV: " .. self.CurrentFOV) return self.CurrentFOV end[/lua] This should spam your chat with debug info :D make a video of what it shows
[lua] function SWEP:SecondaryAttack() self.TargetFOV = self.Secondary.IronFOV end function SWEP:TranslateFOV() self.CurrentFOV = self.CurrentFOV or 0 self.TargetFOV = self.TargetFOV or 0 self.LastFOVTime = self.LastFOVTime or CurTime() if (self.TargetFOV == 0) then self.TargetFOV = 0 end if (!self.Owner:KeyDown(IN_ATTACK2)) then self.TargetFOV = 0 end local FOVChangeSpeed = 1 local changetime = CurTime() - self.LastFOVTime local allowedchange = changetime * FOVChangeSpeed self.CurrentFOV = math.Approach(self.CurrentFOV, self.TargetFOV, allowedchange) self.LastFOVTime = CurTime() chat.AddText(self.CurrentFOV) return self.CurrentFOV end [/lua] [url]https://vid.me/IwD8[/url] What the fuck.. Lol I'm so confused. [editline]28th March 2016[/editline] *The console shows chat text, that's why I opened the console*
I don't know what to tell you... did you modify my code in any way?
That's all I have added, besides the [lua] function SWEP:SecondaryAttack() self.TargetFOV = self.Secondary.IronFOV end [/lua]
Sorry, you need to Log In to post a reply to this thread.