• Skipping numbers with timer
    5 replies, posted
Okay so basically I'm trying to create a respawn timer. Not sure if this is the best way to do so, so if you guys have any suggestions, please let me know. So the problem I'm running into is that the timer is really buggy. Sometimes it will go down by 1, and sometimes by 2. I don't see anything wrong with my code, so it's probably just a dumb mistake. Here's my code: [CODE]local lastVal = CurTime() or 0; local delta; function panel:Think() local delta = CurTime() - lastVal; local delta2Decimals = 10^2; local delta2Decimals = math.floor(delta * delta2Decimals + 0.5) / delta2Decimals; if(delta2Decimals % 1 == 0 && tonumber(self.LabelCounter:GetText()) > 0) then self.LabelCounter:SetText(self.LabelCounter:GetText() - 1); end local lastVal = CurTime(); end[/CODE] Any ideas?
[CODE] local RespawnLength = 20 local TimeToSpawn = RespawnLength for i = RespawnLength, 1, -1 do timer.Simple( i, function() TimeToSpawn = RespawnLength - i end end [/CODE] Using think is ugly. Use this function instead. TimeToSpawn will change by 1 second, every second, for however many seconds you set your respawn timer to be. You can simply place that number on their screen.
[lua]panel.EndTime = 0 panel.LastTime = 0 function panel:StartTimer(time) self.EndTime = CurTime() + time end function panel:Think() local newtime = math.max(0, math.Round(self.EndTime - CurTime(), 2)) if newtime ~= self.LastTime then self.LastTime = newtime self:SetText(tostring(newtime)) end end[/lua] Call panel:StartTimer(10) after you create it and it will begin counting down from 10. [QUOTE=CallMePyro;43234596] Using think is ugly.[/QUOTE] I don't know how you came to the conclusion that using a function the way it was supposed to be used is ugly, nor how you came to the conclusion that spawning a shitton of timers was the right thing to do.
First of all, let's calm down. No need to start swearing. Secondly, I am creating a couple of timers client side. I would agree with you if the timers were being made serverside, but they aren't. A client can very easily handle a couple timers. Thirdly, the code was using was messy, which is why I said it was ugly, not because the "Think" hook itself is actually ugly. I apologize for mispeaking.
First of all, thanks for both of your solutions. Please let this not start a fight. [QUOTE=JetBoom;43234752][lua]panel.EndTime = 0 panel.LastTime = 0 function panel:StartTimer(time) self.EndTime = CurTime() + time end function panel:Think() local newtime = math.max(0, math.Round(self.EndTime - CurTime(), 2)) if newtime ~= self.LastTime then self.LastTime = newtime self:SetText(tostring(newtime)) end end[/lua] Call panel:StartTimer(10) after you create it and it will begin counting down from 10. I don't know how you came to the conclusion that using a function the way it was supposed to be used is ugly, nor how you came to the conclusion that spawning a shitton of timers was the right thing to do.[/QUOTE] Now I'm wondering why mine wasn't working. I'm at work and I don't have enough time to read your's over.
[QUOTE=CallMePyro;43238429]I would agree with you if the timers were being made serverside, but they aren't. A client can very easily handle a couple timers.[/QUOTE] I don't see the difference unless you coded your server end so badly it actually matters. That is not the proper way to do things and eventually it will matter if you're doing everything improperly.
Sorry, you need to Log In to post a reply to this thread.