Bar Timer(like a HUD health bar)

Im currently working on an addon for gmod,

Heres the situation, I have a timer hooked up to the lemons, every 25 seconds, you gain a profit from how many lemons you have. Now, Im having a problem that Ive been trying to solve for like 3hours, so my last resort is facepunch; if you see in the picture there’s a green bar; how would I make the green bar scale downward(like when you take damage and your health bar goes done) but, the thing that is making it scale down would be the 25 second timer, from the lemons?

let me guess. your timer is serversided, right?
im not sure if you can grab the time clientside then, but

timer.TimeLeft should be usefull.
since you can calculate the full time clientside.
otherwise there is still the posibility for net or nwints

If you’re not using timer, there are several basic time calculations: Time Elapsed and Time Remaining.

I’d suggest turning your time elapsed into a float ( 0-1 modifier ) by taking time-elapsed / duration; which will cause the bar to fill from empty. To have it count backwards you simple use 1 - modifier;

Then, you multiply that by the max-width of the bar… IF you use the DProgressBar vgui element, just SetFraction to the 1 - modifier; value and it’ll handle the rest…

Don’t forget to clamp your modifier calculation from 0-1 to ensure the bar doesn’t over-fill or under-fill…

Other examples:

Countdown which syncs to client with optional delay / buffer time to ensure all clients receive it before the countdown starts… viable option; uses CurTime( ) and Duration to calculate everything…

Drawing bars

Proper HUD Creation

Using POLY to create shapes

Tilted health-bar ( emphasis on creating helper-functions to simplify the math )

Math for expanding / shrinking bar in non-traditional directions

Math for expanding / shrinking bar in non-traditional directions

If I use SetNWInt:(“timerleft”, timer.TimeLeft(“lemontimer”)), would I have to set the NWInt in a think hook, since I would have to update the value every-time the timer changes? or would it update automatically?

I may be misreading here but if you do stuff within panel.Paint = function() end then it’s called every frame the menu is open and should update instantly

how would I set the NWInt when Im not dealing with any self arg’s? its all ply or LocalPlayer()?

[editline]1st May 2015[/editline]

And also I have
lemonpanel.Paint = function (self)

but nothing is printing, even when I know that the lemon timer(which is serverside) is on, nothing prints

how would i get the TimeLeft to work?

Dose the timer.TimeLeft(), even work. I’ve been trying to just even print timer.TimeLeft(“lemontimer”) in console, serverside and or clientside, but it wont even give me a value, even tho there is a timer created

If you have a timer created serverside and only serverside, the clients will not have that timer created and thus timer.TimeLeft wouldn’t recognize the timer since it was never created.

I think that for the networking side of things you should just network 2 values: ‘start of countdown’ and ‘length of countdown’, and then do the countdown on the client, if you’re not already. It could reduce some of the networking overhead and would probably be better than continuously sending the timer.TimeLeft value which will be continuously changing.

Running timers for UI elements server-side then networking the timer seems like a very overkill way of going about something seemingly this simple.

If the server needs to track this 25 second period to pay you, keep running a repeating timer on the server that does so. But make sure that the first time the timer is initialised the client is told this and then start a repeating timer on the client for the UI elements that need it.

Though if there is a bit of latency the timers may not synchronise fully, you can handle that however if you know the real world time that the server-side timer starts and the real world time the client wants to start their timer. Just make sure these values are actually using the same time zone.

Ah, your idea is probably better than mine, however, I was just advising it over a constantly synchronised NWInt( timer ). And yes, I believe that both the client and server need the timer.

Yeah trying to sync up values like this with NWInt is just asking for needless traffic in a scenario where you’re only really interested in if the timer has started or not.

It should also be quite simple to implement a method to stop both timers if the client decides they don’t want to play the thing any more too. Just tell the server with a simple message “hey stop that” and kill the timers.