ULX

Author Topic: Scripting a progressbar  (Read 4817 times)

0 Members and 1 Guest are viewing this topic.

Offline Master-Guy

  • Newbie
  • *
  • Posts: 7
  • Karma: 1
Scripting a progressbar
« on: February 12, 2009, 10:38:43 AM »
Hi all,

Currently I'm just playing around some with lua files to see what's possible and at this moment I'm trying to make a progressbar.
For now I got it working, but it's based on the FPS to fill.

The code I have for now (cl_init.lua):

Code: [Select]
function GM:HUDPaint()
    cntTest = cntTest+1
    cntPcnt = math.floor((300/cntMax)*cntTest)
    cntPcntD3 = math.floor(cntPcnt/3)
   
    if stopdraw == false then
        draw.RoundedBox(2, boxLeft, boxTop, cntPcnt, 25, Color(0, 255, 0) )
        draw.RoundedBox(2, boxLeft+cntPcnt, boxTop, 300-cntPcnt, 25, Color(255, 0, 0) )
        draw.SimpleText(cntPcntD3, "ScoreboardText", boxLeft+150, 47, Color(255,255,255,255), TEXT_ALIGN_CENTER, TEXT_ALIGN_CENTER)
    end
   
    if cntPcnt >= 300 then
        stopdraw = true
    end
end

cntTest = 0
cntMax = 1200
boxLeft = (ScrW()/2)-150
boxTop = 35
stopdraw = false

Is there any way to make it work on a timer with milliseconds or deciseconds?
Any help would be greatly appreciated.

With kind regards,

MG

Offline JamminR

  • Ulysses Team Member
  • Hero Member
  • *****
  • Posts: 8096
  • Karma: 390
  • Sertafide Ulysses Jenius
    • Team Ulysses [ULib/ULX, other fine releases]
Re: Scripting a progressbar
« Reply #1 on: February 12, 2009, 02:55:09 PM »
Lua Manual and Gmod Lua Wiki are your friends. :)

I suggest the following.
os.time - Though a standard Lua function, I link to the Gmod wiki most of the time just to keep links to one place. Amazing what the "See also" links often can help give ideas for.

String.ToMinutesSecondsMilliseconds
"Though a program be but three lines long, someday it will have to be maintained." -- The Tao of Programming

Offline Master-Guy

  • Newbie
  • *
  • Posts: 7
  • Karma: 1
Re: Scripting a progressbar
« Reply #2 on: February 14, 2009, 01:33:20 AM »
Thanks for your reply, but I've found those already :P
Unfortunately I cannot get those get to work with milliseconds :(.. the smallest is seconds :'(

Offline JamminR

  • Ulysses Team Member
  • Hero Member
  • *****
  • Posts: 8096
  • Karma: 390
  • Sertafide Ulysses Jenius
    • Team Ulysses [ULib/ULX, other fine releases]
Re: Scripting a progressbar
« Reply #3 on: February 14, 2009, 05:53:36 AM »
You'd need to provide a time in seconds that also included decimals to the String.ToMinutesSecondsMilliseconds function for it to hand back milliseconds.
I thought that os.time didn't do any rounding.
I'm not sure how you've tested but all the numbers currently in your script have have no decimals.
"Though a program be but three lines long, someday it will have to be maintained." -- The Tao of Programming

Offline Master-Guy

  • Newbie
  • *
  • Posts: 7
  • Karma: 1
Re: Scripting a progressbar
« Reply #4 on: February 14, 2009, 11:18:10 AM »
I finally got it working..

Before today I tought that the timer function only worked with integers, but today I spotted the word "float" in the description and tested that..
It seems to do the trick.

Code: [Select]
function GM:HUDPaint()
    cntPcnt = math.floor((300/cntMax)*cntTest)
    cntPcntD3 = math.floor(cntPcnt/3)
   
    if stopdraw == false then
        draw.RoundedBox(2, boxLeft, boxTop, cntPcnt, 25, Color(0, 255, 0) )
        draw.RoundedBox(2, boxLeft+cntPcnt, boxTop, 300-cntPcnt, 25, Color(255, 0, 0) )
        draw.SimpleText(cntPcntD3, "ScoreboardText", boxLeft+150, 47, Color(255,255,255,255), TEXT_ALIGN_CENTER, TEXT_ALIGN_CENTER)
    end
   
    if cntPcnt >= 300 then
        stopdraw = true
print(string.ToMinutesSecondsMilliseconds(1234.5678))
    end
end


function advanceProgressA()
cntTest = cntTest + 1
if cntTest < cntMax then
timer.Create("ProgressTimerB", 0.1, 1, advanceProgressB, "")
end
end
function advanceProgressB()
cntTest = cntTest + 1
if cntTest < cntMax then
timer.Create("ProgressTimerA", 0.1, 1, advanceProgressA, "")
end
end


function startProgress(durationMS)
cntMax = math.Round(durationMS/10)
cntTest = 0
timer.Create("ProgressTimerA", 0.1, 1, advanceProgressA, "")
stopdraw = false
end

startProgress(3000)