• Cannot figure out how to measure the time certain key was held
    9 replies, posted
Hi, I've been trying to code a gauss-like weapon and faced a following problem: I cannot get to work a code, which would calculate a time alt fire was held, which would be used to calculate a damage and kickback multiplier. I've tried the following for a test: function SWEP:Think() -- Called every frame if SERVER then if self.Owner:KeyDown(IN_ATTACK2) then ChargeStart = CurTime() end if self.Owner:KeyReleased(IN_ATTACK2) then ChargeTime = self.Weapon:SetVar("ChargeTime", ((CurTime()) - ChargeStart)) msg(ChargeTime) end end end But console reports, that ChargeTime is nil value -_-. Can anyone help me and post a code which would work?
ChargeTime is nil because SetVar doesn't return anything. [url]http://wiki.garrysmod.com/?title=Entity.SetVar[/url]
What should I do instead?
[QUOTE=OneManArmy;18152782]What should I do instead?[/QUOTE] Here's how I would do it : [lua]function SWEP:Think() -- Called every frame if SERVER then if !self.ChargeStarted and self.Owner:KeyDown(IN_ATTACK2) then self.ChargeStarted = CurTime() elseif !self.Owner:KeyDown(IN_ATTACK2) then print( CurTime() - self.ChargeStarted ) --This prints how much time has passed since ChargeStarted has been set self.ChargeStarted = nil --So that we can set it again end end end[/lua] Notice how the usage of [lua] tags makes it much easier to read.:3:
Ok, thanks, but i've go some questions: 1) "if !self.ChargeStarted" - what it does? 2)" self.ChargeStarted = nil --So that we can set it again " Um, why I wouldn't be able to set it again without this line?
1) Not 100 percent about why ! is used so I don't want to look dumb 2) " self.ChargeStarted = nil --So that we can set it again " is not a local variable so it will exist outside the function but your going to want to be able to call it more than once so we need to remove it so it can be used again nil is used because it means no value
It seems that it is refreshing ChargeStart each frame...
[QUOTE=*Fish*;18163844]1) Not 100 percent about why ! is used so I don't want to look dumb[/QUOTE] Don't worry, you already look dumb. '!' is the same as 'not'. Therefore 'not nil' or '!nil' is equivalent to true.
[QUOTE=*Fish*;18163844]1) Not 100 percent about why ! is used so I don't want to look dumb 2) " self.ChargeStarted = nil --So that we can set it again " is not a local variable so it will exist outside the function but your going to want to be able to call it more than once so we need to remove it so it can be used again nil is used because it means no value[/QUOTE] ! means not, like the guy above me said, so if self.ChargeStarted doesn't exist, then the statement evaluates to true. self.ChargeStarted is set to nil because an if statement above it some where checks if it exists, which after we set it to nil, it doesn't. Setting something to nil makes it equal nothing, like it doesn't even exist. So after we release the key, we want to start over. Also quebec, I think that code would print nil every Think, until you pressed the key and released it, then it would print the time between press and release, then go back to spamming nil again. Edited: [lua]function SWEP:Think() -- Called every frame if SERVER then if !self.ChargeStarted and self.Owner:KeyDown(IN_ATTACK2) then self.ChargeStarted = CurTime() elseif !self.Owner:KeyDown(IN_ATTACK2) and self.ChargeStarted then --Added 'and self.Chargestarted' so it only prints if that var exists, and I even used and instead of && just for you. print( CurTime() - self.ChargeStarted ) --This prints how much time has passed since ChargeStarted has been set self.ChargeStarted = nil --So that we can set it again end end end[/lua]
You're right Yaka, and your addition is exactly what was needed
Sorry, you need to Log In to post a reply to this thread.