What math formula do you use to get the time it needs to fill a 100/100 bar with a certain interval.
5 replies, posted
So i'm making a loading bar style thing, and i'm going to be supplying a number, which is the time it takes to get to 100. What's the math to auto calculate what value it needs to go up by in order to complete the bar exactly on time?
For example if I want to make a bar reach 100 in 5 minutes, what number should it count up by every 0.15 seconds so that it reaches 100 in 5 minutes exactly.
5/100 minutes per interval...
find the time and use math.approach?
Calculator, my good sir.
What? No I mean like, If I set the time to 30 seconds, I want it to reach 100 at 30 seconds exactly.
Store the current time (CurTime()) in a variable that you can access at all times. It all depends on what you want to do, it can be a global variable, it can be part of an entity's table, it can be a networked variable if it's for an event cooldown serverside with a progress visible by every client. I'll call it starttime, for example.
Then every time you want to update your bar, take (CurTime() - starttime), divide it by 30, and multiply the result by 100. Make sure to clamp the result between 0 and 100 too, just in case.
bar_w: the width of the progress bar
EndTime: the target time
[lua]
//w = counting up(bar fills)
local TimeLeft = EndTime - CurTime()
if TimeLeft < 1 then TimeLeft = 0 end
local percent = math.Round(TimeLeft/EndTime)
local fill = bar_w*percent
[/lua]
[lua]
//w = count down(bar empties)
local TimeLeft = EndTime - CurTime()
if TimeLeft < 1 then TimeLeft = 0 end
local percent = 1 - math.Round(TimeLeft/EndTime)
local fill = bar_w*percent
[/lua]
Now you also have the variables percent and TimeLeft to play with for displays.
Sorry, you need to Log In to post a reply to this thread.