DProgress Bar Help

I’ve been trying to script a DProgress Bar on a DFrame and I want it to be hooked up to a timer, such as a 10 second timer changing the progress bar fraction as it counts down. I have a few ideas about going with this but they don’t seem to work. Any suggestions and/or help with my troubles?

Thanks in advance.

When you start your timer, set a variable for the DProgressBar to CurTime(), then in a think hook (for the panel) do SetFraction(10-(CurTime()-your var))

Sorry, A little new to GLua, If you could give me an insight on setting “a variable for the DProgressBar to CurTime()” that would be really helpful.

local var = CurTime() - 10
And then SetFraction( 10 - ( var - CurTimr() )



attempt to call method 'SetFraction' (a nil value)


Post your updated code




local DProgress = vgui.Create( "DProgress", DumpMenu )
DProgress:SetPos( 10, 30 )
DProgress:SetSize( 200, 20 )
DProgress:GetFraction()

local function onThink()
	print( "test" )
DProgress:SetFraction( 10 - ( var - CurTime() ))
end

hook.Add( "Think", "DumpThink", onThink )

end)

Ima guess I didn’t set it up right.

Edit: Above the code in my editor is a Dframe and it works just fine without the new code, so I didn’t post it.

That should work apart from the random “end)” on the last line.

So,
This line is useless right here,


DProgress:GetFraction()

And var is undefined when you’re doing calculations. Checkout the post by gonzalolog on defining it. I’m assuming the


end)

at the end is for a parent function for all the code.

As per your problem with the error. When the menu gets removed, you also need to remove the hook. So modify your onThink function to look like this




local function onThink()
	if (not IsValid(DumpMenu)) then
		hook.Remove("Think", "DumpThink")
                return
	end

        print( "test" )
	DProgress:SetFraction( 10 - ( CurTime() - var ))
end


However, you shouldn’t really use a separate hook for this. Use a panel think hook which will automatically get removed, like such:



function DProgress:Think()
	self:SetFraction(10 - (var - CurTime()))
end


That way you won’t need to check to make sure the panel is valid, and you will have a lot less clutter. Now, going back to Robotboy, you should be doing what he suggested. Here’s a rough idea of what you need to do. Define a variable that is equal to CurTime() before the Think function. In the SetFraction() function, do (timerlength - (CurTime() - variableYouDefined)) / timerlength, to get the fraction you need.

I’m having trouble with the “In The SetFraction() function, do (timerlength - (CurTime() - variableYouDefined)) / timerlength” Part. Do I need to set a timer variable for the “timerlength” or just input a number.

Whichever you want. You said you wanted your timer length to be 10 seconds right? If so, just replace timerlength with 10

Error:


 
[ERROR] addons/fearprops/lua/entities/dumpster00/cl_init.lua:26: unexpected symbol near '/'
  1. unknown - addons/fearprops/lua/entities/dumpster00/cl_init.lua:0 

Code:


local A1 = CurTime()
local DProgress = vgui.Create( "DProgress", DumpMenu )
DProgress:SetPos( 10, 30 )
DProgress:SetSize( 200, 20 )

function DProgress:Think()

        self:SetFraction(10 - (CurTime() - A1)) / 10
end

Close, you need another set of parenthesis around the computation.



 self:SetFraction((10 - (CurTime() - A1)) / 10)


Alright, Awesome, It’s working. Only one problem, it’s going backwards, and I’d like to to move it forwards.

Edit: Another thing, how can i make it close the DFrame once it’s done, Like just add DFrame:Close() in the think function?

You said count down, use the logic there, we won’t spoon-feed you anymore

I guess I had a different image in mind, of the DProgress Bar inverted and it going forwards as the time went down.