Jump to content

A problem about dxDrawRectangle


jingzhi

Recommended Posts

Hey guys, I am trying to make a function to draw progress bar:

function dxDrawProgressBar(BGW,BGH,PBW,PBH,BGcolor,PBcolor,progress) 
    if BGW > PBW and BGH > PBH then 
        if not executed then  
            local executed = true 
            px,py = guiGetScreenSize() 
            local BGstartx = math.floor((px - BGW) / 2) 
            local BGstarty = math.floor((py - BGH) / 2) 
            local PBstartx = math.floor((px - PBW) / 2) 
            local PBstarty = math.floor((px - PBH) / 2) 
            percent = math.floor(PBW / 100) 
        end 
    local drawprogress = math.floor(progress * percent)  
    dxDrawRectangle(BGstartx,BGstarty,BGW,BGH,BGcolor,true) 
    dxDrawRectangle(PBstartx,PBstarty,drawprogress,PBH,PBcolor,true) 
    end 
end 
  

and I tried to run the function with these parameters

        addEventHandler("onClientRender",getRootElement(), 
        function() 
        local progress = math.floor((timepast / repairtime) * 100) 
        dxDrawProgressBar(410,51,380,28,tocolor(127, 127, 127, 255),tocolor(233, 254, 0, 255),progress) 
        end) 

Unfortunately, it is not working, debugscript3 says :

Bad argument @ dxDrawRectangle :13: Expected vector-2 at argument 1, got nil

Bad argument @ dxDrawRectangle :14: Expected vector-2 at argument 1, got nil

Please help, thank you very much!

Link to comment
Because of your "if not executed then - local executed = true" lines

the positions be nil after the first frame

also this is useless because even if it works

you will have problems if you draw more than 1 progressbar

But these are the values that are not changing, thats why I put this, and it calculate it once to save CPU. I maybe wrong, please point out if i'm wrong :)

Link to comment
px,py = guiGetScreenSize() 
  
function dxDrawProgressBar(BGW,BGH,PBW,PBH,BGcolor,PBcolor,progress) 
    if BGW > PBW and BGH > PBH then 
        local BGstartx, BGstarty, PBstartx, PBstarty, percent, executed; 
        if not executed then 
            executed = true 
            BGstartx = math.floor((px - BGW) / 2) 
            BGstarty = math.floor((py - BGH) / 2) 
            PBstartx = math.floor((px - PBW) / 2) 
            PBstarty = math.floor((px - PBH) / 2) 
            percent = math.floor(PBW / 100) 
        end 
         
        local drawprogress = math.floor(progress * percent) 
        dxDrawRectangle(BGstartx,BGstarty,BGW,BGH,BGcolor,true) 
        dxDrawRectangle(PBstartx,PBstarty,drawprogress,PBH,PBcolor,true) 
    end 
end 

Link to comment

You cant calculate it just one time since it's in onClientRender

you can do something like that:

function dxCreateProgressBar(BGW,BGH,PBW,PBH) 
    if BGW > PBW and BGH > PBH then 
    local pB = createElement("dxProgressBar")  
    local px,py = guiGetScreenSize() 
    local BGstartx = math.floor((px - BGW) / 2) 
    local BGstarty = math.floor((py - BGH) / 2) 
    local PBstartx = math.floor((px - PBW) / 2) 
    local PBstarty = math.floor((px - PBH) / 2) 
    local percent = math.floor(PBW / 100) 
    setElementData(pB,"datas",{BGW=BGW,BGH=BGH,PBW=PBW,PBH=PBH,BGstartx=BGstartx,BGstarty=BGstarty,PBstartx=PBstartx,PBstarty=PBstarty,Percent=percent}) 
    return pB 
    end 
end 
  
function dxDrawProgressBar(ProgressBar,progress,BGcolor,PBcolor) 
    if isElement(ProgressBar) then 
    local datas = getElementData(ProgressBar,"datas") 
    local drawprogress = math.floor(progress * datas.Percent) 
    dxDrawRectangle(datas.BGW,datas.BGstarty,datas.BGW,datas.BGH,BGcolor,true) 
    dxDrawRectangle(datas.PBstartx,datas.PBstarty,drawprogress,datas.PBH,PBcolor,true) 
    end 
end 
  
-------------- 
  
local myPB = dxCreateProgressBar(410,51,380,28) 
addEventHandler("onClientRender",getRootElement(), 
function() 
local progress = math.floor((timepast / repairtime) * 100) 
dxDrawProgressBar(myPB,progress,tocolor(127, 127, 127, 255),tocolor(233, 254, 0, 255)) 
end 
) 

Link to comment
px,py = guiGetScreenSize() 
  
function dxDrawProgressBar(BGW,BGH,PBW,PBH,BGcolor,PBcolor,progress) 
    if BGW > PBW and BGH > PBH then 
        local BGstartx, BGstarty, PBstartx, PBstarty, percent, executed; 
        if not executed then 
            executed = true 
            BGstartx = math.floor((px - BGW) / 2) 
            BGstarty = math.floor((py - BGH) / 2) 
            PBstartx = math.floor((px - PBW) / 2) 
            PBstarty = math.floor((px - PBH) / 2) 
            percent = math.floor(PBW / 100) 
        end 
         
        local drawprogress = math.floor(progress * percent) 
        dxDrawRectangle(BGstartx,BGstarty,BGW,BGH,BGcolor,true) 
        dxDrawRectangle(PBstartx,PBstarty,drawprogress,PBH,PBcolor,true) 
    end 
end 

You haven't done anything

like that it will calculate on every frame and he dont want that

Link to comment
px,py = guiGetScreenSize() 
  
function dxDrawProgressBar(BGW,BGH,PBW,PBH,BGcolor,PBcolor,progress) 
    if BGW > PBW and BGH > PBH then 
        local BGstartx, BGstarty, PBstartx, PBstarty, percent, executed; 
        if not executed then 
            executed = true 
            BGstartx = math.floor((px - BGW) / 2) 
            BGstarty = math.floor((py - BGH) / 2) 
            PBstartx = math.floor((px - PBW) / 2) 
            PBstarty = math.floor((px - PBH) / 2) 
            percent = math.floor(PBW / 100) 
        end 
         
        local drawprogress = math.floor(progress * percent) 
        dxDrawRectangle(BGstartx,BGstarty,BGW,BGH,BGcolor,true) 
        dxDrawRectangle(PBstartx,PBstarty,drawprogress,PBH,PBcolor,true) 
    end 
end 

I got confused, why this will make the calculation every frame? But after the first frame the "excuted" will be true and the it won't pass the if statement and won't do the calculations again? Please point out :|

Link to comment

It will work if you just used the function one time

but on the second time the variables will be updated to the new positions of the new progressbar and the old progressbar gonna have the new positions of the new progressbar.

And dont forget that you are working with function that will be used in onClientRender event

My code above is the one of the best ways to do what you are trying to do

Link to comment
It will work if you just used the function one time

but on the second time the variables will be updated to the new positions of the new progressbar and the old progressbar gonna have the new positions of the new progressbar.

And dont forget that you are working with function that will be used in onClientRender event

My code above is the one of the best ways to do what you are trying to do

Doing this will reset on every frame? So if i set "executed" to true and it will be nil again on the next frame?

Link to comment

if it's local yea it will reset

but if it's not local not it wont reset

but if you used the function more than one time

like that

addEventHandler("onClientRender",root, 
function() 
dxDrawProgressBar(...) 
dxDrawProgressBar(...) 
dxDrawProgressBar(...) 
end 
) 

all the progressbars well have the last progressbar created positions

Link to comment
if it's local yea it will reset

but if it's not local not it wont reset

but if you used the function more than one time

like that

addEventHandler("onClientRender",root, 
function() 
dxDrawProgressBar(...) 
dxDrawProgressBar(...) 
dxDrawProgressBar(...) 
end 
) 

all the progressbars well have the last progressbar created positions

I got a clue, thank you :)

Link to comment

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...