timer.simple which sets a number var each second

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

thats only for timer.create, I want it to be functional with timer.simple.

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.

would frametime work? Since I just created a working function which is kinda hacky:



function ENT:Timer()
	time2 = math.floor(math.Clamp(time, 0, 10))
	time = time - FrameTime()
	return time2
end


This is how you usually do it:



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


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.

I dont know how curtime works though, since if I’d place CurTime() instead FrameTime() it’d be to 0 instantly.

It gives you time in seconds since start of the server. Look at my previous post for an example.

Why don’t you read the wiki page for it then, as a start?

And inspect this post:

I dont want to do a complete timer function, I only want to make a “counter” which counts down from any number, eg 10

Then just use value of self.EndTime - CurTime().

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



function ENT:Timer(delay)
	self.EndTime = CurTime() + delay
	return self.EndTime - CurTime()
end


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 don’t set self.EndTime constantly, you do it once.

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



self.width = math.Approach(self.width, 400, 40*FrameTime())


for a smooth progress bar, how would I go about using it with curtime?



-- 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


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 self.EndTime - CurTime() 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 (self.EndTime - CurTime()) / delay. 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.