Progress bar based on time

Now I’ve this networked integer i call from another lua file which basically is either CurTime() + 10 or CurTime() + 100 depening on the situation, doesn’t really matter.

I’ve made this progress bar:



	if ( !LocalPlayer():Alive() ) then
		local BleedingTime = LocalPlayer():GetNWInt("BleedOutTime") - CurTime();
		local TrueBarSize = 600 / 100;
		local BarX = BleedingTime * TrueBarSize;


		-- The Top Box
		surface.SetDrawColor( 0, 0, 0, 200 );
		surface.DrawRect(ScrW() / 2 - 310, ScrH() / 2 - 135, 620, 90 );	
		draw.SimpleText( "You're Unconscious", "TextHUDSmall", ScrW() / 2, ScrH() / 2 - 105, Color(215, 70, 70, 255), TEXT_ALIGN_CENTER, TEXT_ALIGN_CENTER);
		draw.SimpleText( "You have to wait until a medic revives you, or until you bleed out to respawn.", "BleedOutText", ScrW() / 2, ScrH() / 2 - 85, Color(255, 255, 255, 255), TEXT_ALIGN_CENTER, TEXT_ALIGN_CENTER);
		
		-- The Progress Bar
		surface.SetDrawColor( 0, 0, 0, 200 );
		surface.DrawRect(ScrW() / 2 - 310, ScrH() / 2 - 35, 620, 70 );
		surface.SetDrawColor( 0, 0, 0, 200 );
		surface.DrawRect(ScrW() / 2 - 300, ScrH() / 2 - 25, 600, 50 );
		surface.SetDrawColor( 215, 70, 70, 255 );
		surface.DrawRect(ScrW() / 2 - 300, ScrH() / 2 - 25, BarX, 50 );
		draw.SimpleText( "Bleeding Out...", "TextHUDSmall", ScrW() / 2, ScrH() / 2, Color(255, 255, 255, 255), TEXT_ALIGN_CENTER, TEXT_ALIGN_CENTER);
	end


This is a progress bar that pretty much shows the time before you will respawn after death. Now my problem is that if the respawn time is 10 seconds instead of 100, the bar won’t start at full length, it will start at 10% bar length.

I tried using this, but it will only make the bar reset at 100% when the 100 second timer reaches 10 seconds left:



	if ( !LocalPlayer():Alive() ) then
		local BleedingTime = LocalPlayer():GetNWInt("BleedOutTime") - CurTime();
		if ( BleedingTime < 10 ) then
			local TrueBarSize = 600 / 10;
			local BarX = BleedingTime * TrueBarSize;
		elseif ( BleedingTime < 100 ) then
			local TrueBarSize = 600 / 100;
			local BarX = BleedingTime * TrueBarSize;
		end


How can i make this bar length start at 100% even when the respawn timer only is 10 seconds, but not when the timer starts at 100 seconds? Thanks for any help! :slight_smile:

The bar size should be defined by ScrW( ), the timer should be CurTime( ) - timeElapsed. timeElapsed should be CurTime( ) - startTime.

Then you use the mathematical trickery in order to make that number between 0 and 1 whereby you just divide that amount by the time amount. Once that’s done, you multiple that result with the maximum bar size.

so you’ll have something like: 60 * 0, 60 * 0.1, 60 * 0.5, 60 * 1 meaning the bar is 0, 10 percent, 50 percent, and 100 percent.

I will try and come back with results :slight_smile:

Feel free to add me on steam if you have more questions; by using a system based on time as shown above the bar will move smoothly. I do have an example of this which I made for a friend; it’s in one of my old backups so I’ll try to find it and either post it here or if you add me on steam I’ll give it to you there.

Here: While it does not use “time” it does show modifying the time and this will move forward smoothly. Substitute those values with time. I’ll try to find the newer version I coded which does it based on time.

[lua]local PANEL = PANEL or { };

function PANEL:Init( )
self.AcecoolsDPanel = vgui.Create(“DPanel”)
self.AcecoolsDPanel:SetSize(250, 25)
self.AcecoolsDPanel:Center( )

self.myProgressBar = vgui.Create( "DProgress", self.AcecoolsDPanel)
self.myProgressBar:SetSize( 240, 20 )
self.myProgressBar:SetFGColor( Color(255,255,0) )
self.myProgressBar:Center( )
self.myProgressBar:SetFraction( 0 )

self.AcecoolsDPanel.Paint = function( )
	draw.RoundedBox( 8, 0, 0, self.AcecoolsDPanel:GetWide( ), self.AcecoolsDPanel:GetTall( ), Color( 0, 0, 0, 225 ) )
	PANEL.myProgressBar:SetFraction( PANEL.myProgressBar:GetFraction( ) + (1/100) )
	if (PANEL.myProgressBar:GetFraction( ) &gt;= 1) then
		PANEL.AcecoolsDPanel:SetVisible(false)
	end
end

end

concommand.Add(“modelpanel3”, function( )
if (!IsValid(PANEL.AcecoolsDPanel)) then
PANEL:Init( )
else
PANEL.myProgressBar:SetFraction( 0 )
PANEL.AcecoolsDPanel:SetVisible(!PANEL.AcecoolsDPanel:IsVisible( ))
end
LocalPlayer( ):StopMoving( );
end)[/lua]

Im not sure i followed you completely but here’s what I did and it works like a charm :dance: Thanks!



if ( !LocalPlayer():Alive() ) then
        local BleedingTime = LocalPlayer():GetNWInt("BleedOutTime");
        local startTime = LocalPlayer():GetNWInt("DeathTime");
        local Countdown = BleedingTime - CurTime();
        local totalTime = BleedingTime - startTime;
        local trueTime = math.Clamp(Countdown/totalTime, 0, 1);
        local BarX = TrueTime * 600;