Jump to content

A problem about dxDrawRectangle


jingzhi

Recommended Posts

Posted

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!

Posted

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

Posted
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 :)

Posted
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 

Posted

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 
) 

Posted
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

Posted

He won't. I've done one thing, look properly. After first frame he will calculate only once, and then if statemant won't run again.

@EDIT. My bad, didn't notice that value will reset after each frame, tho. Up user's solution is good.

Posted
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 :|

Posted
This line

local BGstartx, BGstarty, PBstartx, PBstarty, percent, executed;

is reset executed everyframe

If I delete "excuted" from this line will it still reset or there will be some other problems?

Posted

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

Posted
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?

Posted

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

Posted
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 :)

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...