• timer.simple which sets a number var each second
    19 replies, posted
Hello FP, I wanted to know how to make a timer.simple to set a var to the given seconds remaining, Like I have a timer, which runs a function in 120 seconds, and I want to display the remaining seconds somewhere, how would I go about doing so? I thought about using a timer.create function which runs every second and repeats 120 times that sets the var, but that somehow doesnt work that good
:snip: bad reading
[QUOTE=stev_;48486112][url=http://wiki.garrysmod.com/page/timer/TimeLeft]timer.TimeLeft[/url][/QUOTE] thats only for timer.create, I want it to be functional with timer.simple.
[QUOTE=whitestar;48486141]thats only for timer.create, I want it to be functional with timer.simple.[/QUOTE] Nothing stops you from using timer.Create with one repetition. [editline]18th August 2015[/editline] In reality though, I'd recommend not using timer library for this at all, and go for the CurTime() and extra variable approach. [editline]18th August 2015[/editline] The timer library is useful for things like infinite timers and "fire-and-forget" timers.
[QUOTE=Robotboy655;48486179]Nothing stops you from using timer.Create with one repetition. [editline]18th August 2015[/editline] In reality though, I'd recommend not using timer library for this at all, and go for the CurTime() and extra variable approach.[/QUOTE] would frametime work? Since I just created a working function which is kinda hacky: [code] function ENT:Timer() time2 = math.floor(math.Clamp(time, 0, 10)) time = time - FrameTime() return time2 end [/code]
This is how you usually do it: [code] function ENT:StartTimer(delay) self.EndTime = CurTime() + delay end function ENT:Think() if self.EndTime and self.EndTime <= CurTime() then self.EndTime = nil -- do some crap here end end [/code]
[QUOTE=whitestar;48486189]would frametime work? Since I just created a working function which is kinda hacky: [code] function ENT:Timer() time2 = math.floor(math.Clamp(time, 0, 10)) time = time - FrameTime() return time2 end [/code][/QUOTE] I personally try to avoid doing timers like that, adding a certain number to a variable every frame, because in this case you have to take care of FPS dependency, host_timescale and all that boring stuff which CurTime() methods conveniently work around.
[QUOTE=Robotboy655;48486212]I personally try to avoid doing timers like that, adding a certain number to a variable every frame, because in this case you have to take care of FPS dependency, host_timescale and all that boring stuff which CurTime() methods conveniently work around.[/QUOTE] I dont know how curtime works though, since if I'd place CurTime() instead FrameTime() it'd be to 0 instantly.
[QUOTE=whitestar;48486221]I dont know how curtime works though[/QUOTE] It gives you time in seconds since start of the server. Look at my [URL="http://facepunch.com/showthread.php?t=1481749&p=48486206&viewfull=1#post48486206"]previous post[/URL] for an example.
Why don't you read the wiki page for it then, as a start? [url]http://wiki.garrysmod.com/page/Global/CurTime[/url] And inspect this post: [QUOTE=mijyuoon;48486206]This is how you usually do it: [code] function ENT:StartTimer(delay) self.EndTime = CurTime() + delay end function ENT:Think() if self.EndTime and self.EndTime <= CurTime() then self.EndTime = nil -- do some crap here end end [/code][/QUOTE]
[QUOTE=mijyuoon;48486206]This is how you usually do it: [code] function ENT:StartTimer(delay) self.EndTime = CurTime() + delay end function ENT:Think() if self.EndTime and self.EndTime <= CurTime() then self.EndTime = nil -- do some crap here end end [/code][/QUOTE] I dont want to do a complete timer function, I only want to make a "counter" which counts down from any number, eg 10
[QUOTE=whitestar;48488720]I dont want to do a complete timer function, I only want to make a "counter" which counts down from any number, eg 10[/QUOTE] Then just use value of [i]self.EndTime - CurTime()[/i].
[QUOTE=whitestar;48488720]I dont want to do a complete timer function, I only want to make a "counter" which counts down from any number, eg 10[/QUOTE] You can calculate time left by doing [lua]self.EndTime - CurTime()[/lua] without having to set a variable every second. This can also even be calculated every frame on clients to use a timer-like element in a HUD/Draw/Paint function. Just remember to network your EndTime so clients know it. Ninja'd damnit
[code] function ENT:Timer(delay) self.EndTime = CurTime() + delay return self.EndTime - CurTime() end [/code] When I use that code, the remaining time doesnt update in the draw function.
Jeez, did you at least try to understand how the code works? You [b]don't[/b] set [i]self.EndTime[/i] constantly, you do it [b]once[/b].
[QUOTE=mijyuoon;48489172]Jeez, did you at least try to understand how the code works? You [b]don't[/b] set [i]self.EndTime[/i] constantly, you do it [b]once[/b].[/QUOTE] how would I return the time then in an function where I draw it? it was said by calculating it with - curtime.. [editline]19th August 2015[/editline] since if I would just use to paint self.EndTime, it would not be "resetable" or such. [editline]19th August 2015[/editline] also I use [code] self.width = math.Approach(self.width, 400, 40*FrameTime()) [/code] for a smooth progress bar, how would I go about using it with curtime?
[code] -- this is on server function ENT:SetTimer(delay) self.EndTime = CurTime() + delay self:SetNWFloat("EndTime", self.EndTime) end -- this is on client function ENT:CallThisInSomeDrawingHook() self.EndTime = self:GetNWFloat("EndTime") local dt = CurTime() - self.EndTime draw.DrawText("Time: "..dt, "Default", 0,0, Color(255,0,0)) end [/code]
You set self.EndTime once whenever you want it to take time, and you do so relative to CurTime() (self.EndTime = CurTime() + delay). You do [I]self.EndTime - CurTime()[/I] at any given point where you want to know how much time is left. Doing this in the same function as where you set EndTime will make the time left equal to the delay (as literally no time has passed). For a timer to update every frame, you could do this subtraction in any draw-type hook. Stick to FrameTime in your smooth progress bar. Edit: Is that progress bar the timer/countdown? If so, you could replace 40*FrameTime() with (400/delay)*FrameTime(). With 400 being your max width and delay being the amount of seconds the you set EndTime above CurTime().
Well, I want it to be CurTime instead of FrameTime, Since the bar somehow speeds up upon moving the entity, so its out of synch with the time itself.
You could also try using [I](self.EndTime - CurTime()) / delay[/I]. This will be a number between 0 and 1 representing the progress through the timer. Try doing that and multiplying the max width with this number to get current width of the timer bar.
Sorry, you need to Log In to post a reply to this thread.