Jump to content

[HELP] Update dx render


Recommended Posts

Posted

Hello guys, I am trying to make a piss system, with a bar that should how much piss you hold ( xD haha) and I use set and getElementData for this. The problem is that I don't know how to make the render update, if I set my piss to 0, I want to bar to go down:

if (counter > 25) then 
                dxDrawText ( barName, barx/0.499 + 1, bary/0.492, 76, 12, tocolor ( 255, 255, 255, 255 ), 1, "default", "center", "center", false, false, true ) 
                dxDrawRectangle (barx, bary, 80, 14, tocolor (0,0,0,255), false) 
                dxDrawRectangle (barxoverlay-0.5, baryoverlay-0.8, 77, 10, tocolor (235,141,45,127), false) 
                dxDrawRectangle (barxoverlay-0.5, baryoverlay-0.8, 75.5*barRelative, 10, tocolor (235,141,45,185), false) 
                counter = counter +1 
            else 
                counter = counter +1 
            end 
            if (counter == 50) then 
                counter = 0 
            end 

The piss amount is "barRelative"

  • Moderators
Posted

You could try something like this: (NOT TESTED)

local calculateProgress = function (timeNow,futureTime, startValue,endValue,duration) 
    local timeNow = timeNow  or getTickCount() 
    if timeNow < futureTime then 
        if startValue > endValue then  
            local factor = 1-(futureTime-timeNow)/duration  
            return ((startValue-endValue)* factor) + endValue 
        elseif startValue < endValue then  
            local factor = ((futureTime-timeNow)/duration)  
            return ((endValue-startValue)* factor) + startValue 
        else -- remember you can't do 0/number, so... 
            return startValue 
        end 
    else 
        return endValue 
    end 
end 
  
  
  
  

local futureTime = getTickCount()+500 
  
addEventHandler("onClientRender",root, 
function () 
    dxDrawText(tostring(calculateProgress(getTickCount(),futureTime,1,10,500)),300,300) 
end) 

Make sure that the futureTime-startTime matches the duration.

Posted

Ok so, I got this bar that indicates how much piss you hold, it's like the health bar. When the bar is 100% full, and then when I set my piss to 50%, the bar does not update unless I restart the resource...

Posted

Here is the code:

  
function showBar(barx, bary, barxoverlay, baryoverlay, barRelative, counter, barName) 
    return table.insert(listBRS, {barx, bary, barxoverlay, baryoverlay, barRelative, counter, barName}) 
end 
addEvent("onClientShowBar",true) 
addEventHandler("onClientShowBar", root, showBar) 
  
function renderBar() 
    if (#listBRS > 0) then 
        for i, values in ipairs(listBRS) do 
            local barx, bary, barxoverlay, baryoverlay, barRelative, counter, barName = unpack(values) 
            if (counter > 25) then 
                dxDrawText ( barName, barx/0.499 + 1, bary/0.492, 76, 12, tocolor ( 255, 255, 255, 255 ), 1, "default", "center", "center", false, false, true ) 
                dxDrawRectangle (barx, bary, 80, 14, tocolor (0,0,0,255), false) 
                dxDrawRectangle (barxoverlay-0.5, baryoverlay-0.8, 77, 10, tocolor (235,141,45,127), false) 
                dxDrawRectangle (barxoverlay-0.5, baryoverlay-0.8, 75.5*barRelative, 10, tocolor (235,141,45,185), false) 
                counter = counter +1 
            else 
                counter = counter +1 
            end 
            if (counter == 50) then 
                counter = 0 
            end 
        end 
    end 
end 
addEventHandler("onClientRender",getRootElement(),renderBar) 
  
function renderPissShit() 
    local sx,sy = guiGetScreenSize () 
    local x = sx/800*683 
    local y = sy/600*89 
    local x1 = sx/800*685 
    local y1 = sy/600*91 
    local pissData = tonumber(getElementData(getLocalPlayer(), "piss"))/100 
    local pissBar = 100 
    local title = "PISS" 
    showBar(x, y, x1, y1, pissData, pissBar, title) 
end 
addCommandHandler("piss", renderPissShit) 

  • Moderators
Posted (edited)
function showBar(barx, bary, barxoverlay, baryoverlay, barRelative, counter, barName) 
    return table.insert(listBRS, {barx, bary, barxoverlay, baryoverlay, barRelative, counter, barName}) 
end 
addEvent("onClientShowBar",true) 
addEventHandler("onClientShowBar", root, showBar) 
  
function renderBar() 
    if (#listBRS > 0) then 
         
        --for i, values in ipairs(listBRS) do 
         
        for i=#listBRS,1, -1 do -- make sure you can remove items properly. (using table.remove) This is only possible when you inverse your loop. 
            local values = listBRS[i] 
            -- first make sure you know at which array your variables are. 
            local barx, bary, barxoverlay, baryoverlay, barRelative, counter, barName =  
                values[1], -- barx 
                values[2], -- bary 
                values[3], -- barxoverlay 
                values[4], -- baryoverlay 
                values[5], -- barRelative 
                values[6], -- counter = at array 6 << very important. 
                values[7]  -- barName 
  
            if (counter > 25) then 
                dxDrawText ( barName, barx/0.499 + 1, bary/0.492, 76, 12, tocolor ( 255, 255, 255, 255 ), 1, "default", "center", "center", false, false, true ) 
                dxDrawRectangle (barx, bary, 80, 14, tocolor (0,0,0,255), false) 
                dxDrawRectangle (barxoverlay-0.5, baryoverlay-0.8, 77, 10, tocolor (235,141,45,127), false) 
                dxDrawRectangle (barxoverlay-0.5, baryoverlay-0.8, 75.5*barRelative, 10, tocolor (235,141,45,185), false) 
                counter = counter +1 
            else 
                counter = counter +1 
            end 
             
            values[6] = counter -- now save the edited variable counter back at array 6 
             
            if (counter >= 50) then 
             
                --counter = 0 --(Don't you need to remove that item?) 
                 
                table.remove(listBRS,i) -- removed! 
                 
            end 
             
             
             
        end 
    end 
end 
addEventHandler("onClientRender",getRootElement(),renderBar) 
  
function :~() 
    local sx,sy = guiGetScreenSize () 
    local x = sx/800*683 
    local y = sy/600*89 
    local x1 = sx/800*685 
    local y1 = sy/600*91 
     
    -- Making sure the value is always correct. (between 0 t/m 100) Never trust element data, since all resources can edit it. 
    local pissData = math.max(math.min(tonumber(getElementData(getLocalPlayer(), "piss")) or 0,100),0)/100  
    -------------------- 
     
    local pissBar = 100 
    local title = "PISS" 
    showBar(x, y, x1, y1, pissData, pissBar, title) 
end 
addCommandHandler("piss", :~) 

Edited by Guest
Posted

Where have you defined "values"? I see you used this think:

for i=#listBRS,1, -1 do  

I never used it before, and I don't even know what it is :P I been always using ipairs

  • Moderators
Posted

That is right, since there are no comments of your own in the code I do not know what the code should do.

You are adding bars in to a all in one bar manage system, it is still unclear what your end result must be.

Posted

Ok so, I'll try to explain this as best as I can. I want to make a bar such as the health bar, which should stay on the screen when created. When I piss, I want the bar to go down, as with the health, when my health goes down, this bar should go down :P

  • Moderators
Posted

and what is the counter for?

Well if that is what you want, you don't need all that code.

  
local pissBarStatus = false 
  
function showBar() 
    pissBarStatus = not pissBarStatus 
end 
addEvent("onClientShowBar",true) 
addEventHandler("onClientShowBar", root, showBar) 
  
addCommandHandler("piss", showBar) 
  
local sx,sy = guiGetScreenSize () 
local barx = sx/800*683 
local bary = sy/600*89 
local barxoverlay = sx/800*685 
local baryoverlay = sy/600*91 
  
local barName = "PISS" 
local counter = 0 
  
  
addEventHandler("onClientRender",root, 
function () 
    if pissBarStatus then 
        local barRelative = math.max(math.min(tonumber(getElementData(localPlayer, "piss")) or 0,100),0)/100 
        if (counter > 25) then 
            dxDrawText ( barName, barx/0.499 + 1, bary/0.492, 76, 12, tocolor ( 255, 255, 255, 255 ), 1, "default", "center", "center", false, false, true ) 
            dxDrawRectangle (barx, bary, 80, 14, tocolor (0,0,0,255), false) 
            dxDrawRectangle (barxoverlay-0.5, baryoverlay-0.8, 77, 10, tocolor (235,141,45,127), false) 
            dxDrawRectangle (barxoverlay-0.5, baryoverlay-0.8, 75.5*barRelative, 10, tocolor (235,141,45,185), false) 
            counter = counter +1 
        else 
            counter = counter +1 
        end 
        if (counter >= 50) then 
            counter = 0  
        end 
    end 
end) 

Posted
and what is the counter for?

Well if that is what you want, you don't need all that code.

  
local pissBarStatus = false 
  
function showBar() 
    pissBarStatus = not pissBarStatus 
end 
addEvent("onClientShowBar",true) 
addEventHandler("onClientShowBar", root, showBar) 
  
addCommandHandler("piss", showBar) 
  
local sx,sy = guiGetScreenSize () 
local barx = sx/800*683 
local bary = sy/600*89 
local barxoverlay = sx/800*685 
local baryoverlay = sy/600*91 
  
local barName = "PISS" 
local counter = 0 
  
  
addEventHandler("onClientRender",root, 
function () 
    if pissBarStatus then 
        local barRelative = math.max(math.min(tonumber(getElementData(localPlayer, "piss")) or 0,100),0)/100 
        if (counter > 25) then 
            dxDrawText ( barName, barx/0.499 + 1, bary/0.492, 76, 12, tocolor ( 255, 255, 255, 255 ), 1, "default", "center", "center", false, false, true ) 
            dxDrawRectangle (barx, bary, 80, 14, tocolor (0,0,0,255), false) 
            dxDrawRectangle (barxoverlay-0.5, baryoverlay-0.8, 77, 10, tocolor (235,141,45,127), false) 
            dxDrawRectangle (barxoverlay-0.5, baryoverlay-0.8, 75.5*barRelative, 10, tocolor (235,141,45,185), false) 
            counter = counter +1 
        else 
            counter = counter +1 
        end 
        if (counter >= 50) then 
            counter = 0  
        end 
    end 
end) 

The bar keeps flashing (disappearing and appearing)

What I want is, if you have 70 pis on you, your bar should be like this:

daY3A.png

And when you have like 10, your bar thingy... fill.. should go down:

9QkMj.png

The think is that before, the bar did not update, it remained at 70

  • Moderators
Posted

It is flashing because of your counter part...

    local pissBarStatus = false 
      
    function showBar() 
        pissBarStatus = not pissBarStatus 
    end 
    addEvent("onClientShowBar",true) 
    addEventHandler("onClientShowBar", root, showBar) 
      
    addCommandHandler("piss", showBar) 
      
    local sx,sy = guiGetScreenSize () 
    local barx = sx/800*683 
    local bary = sy/600*89 
    local barxoverlay = sx/800*685 
    local baryoverlay = sy/600*91 
      
    local barName = "PISS" 
      
      
    addEventHandler("onClientRender",root, 
    function () 
        if pissBarStatus then 
            local barRelative = math.max(math.min(tonumber(getElementData(localPlayer, "piss")) or 0,100),0)/100 
            dxDrawText ( barName, barx/0.499 + 1, bary/0.492, 76, 12, tocolor ( 255, 255, 255, 255 ), 1, "default", "center", "center", false, false, true ) 
            dxDrawRectangle (barx, bary, 80, 14, tocolor (0,0,0,255), false) 
            dxDrawRectangle (barxoverlay-0.5, baryoverlay-0.8, 77, 10, tocolor (235,141,45,127), false) 
            dxDrawRectangle (barxoverlay-0.5, baryoverlay-0.8, 75.5*barRelative, 10, tocolor (235,141,45,185), false) 
        end 
    end) 

Posted

Now it doesn't even show... Anyway, I went back to an older code:

local listBRS = {} 
  
function showBar(barx, bary, barxoverlay, baryoverlay, barRelative, barName) 
    return table.insert(listBRS, {barx, bary, barxoverlay, baryoverlay, barRelative, barName}) 
end 
addEvent("onClientShowBar",true) 
addEventHandler("onClientShowBar", root, showBar) 
  
function renderBar() 
    if (#listBRS > 0) then 
        for i=#listBRS,1, -1 do  
            local values = listBRS[i] 
            local barx, bary, barxoverlay, baryoverlay, barRelative, barName = 
            values[1], -- barx 
            values[2], -- bary 
            values[3], -- barxoverlay 
            values[4], -- baryoverlay 
            values[5], -- barRelative 
            values[6]  -- barName 
            dxDrawText ( barName, barx/0.499 + 1, bary/0.492, 76, 12, tocolor ( 255, 255, 255, 255 ), 1, "default", "center", "center", false, false, true ) 
            dxDrawRectangle (barx, bary, 80, 14, tocolor (0,0,0,255), false) 
            dxDrawRectangle (barxoverlay-0.5, baryoverlay-0.8, 77, 10, tocolor (235,141,45,127), false) 
            dxDrawRectangle (barxoverlay-0.5, baryoverlay-0.8, 75.5*values[5], 10, tocolor (235,141,45,185), false) 
        end 
    end 
end 
addEventHandler("onClientRender",getRootElement(),renderBar) 
  

This works perfectly, but I can't get it to update... I tried to explain this to you and I will try again... my data is piss = 100, ok? Now, the bar is full, then I set my piss to 50, which is a half, but the bar still stays full instead of decreasing to a half... I swear if you don't understand this, I'll make a video xDDD

The only thing I need is to make it check if the elementData changed, so the bar can also change

  • Moderators
Posted

It doesn't show up, because I created an error/warning or you didn't use the piss command.

Yet your previous code is kinda strange and has a lot of features you don't need. (like having the possibility to create multiply bars, which you aren't using in your code.)

The reason why you can't update it, is because your data can't be edited easily. When you use the piss command multiply times, it will fill up the table with the same data. The data will never be delete, it will only increase. And at the end the players with not very good pc's will have very low fps.

Pls stick with the latest code I posted and debug is correctly.

Posted
It doesn't show up, because I created an error/warning or you didn't use the piss command.

Yet your previous code is kinda strange and has a lot of features you don't need. (like having the possibility to create multiply bars, which you aren't using in your code.)

The reason why you can't update it, is because your data can't be edited easily. When you use the piss command multiply times, it will fill up the table with the same data. The data will never be delete, it will only increase. And at the end the players with not very good pc's will have very low fps.

Pls stick with the latest code I posted and debug is correctly.

Ok, so, I did use the "piss" command and there was no error in debugscript. Secondly, I need the feature to create a lot of bars, because I want to make a separate system, like a bar-system that I will use for a system for hunger, thirst, piss, poop, etc.. I don't want to keep creating a new function for each bar.

And what you are saying is that the bar won't change as it will only create another one, I got that.. :/ But what if I create a new table for each thing? (hunger, thirst, piss, etc)

EDIT: I used your code, but I removed something, maybe I shouldn't of remove it but it didn't make sense to me:

I changed this:

function showBar() 
   pissBarStatus = not pissBarStatus 
end 

Into:

function showBar() 
   pissBarStatus = true 
end 

And it seem to work, I guess I have to create a bar for each thing I need :P

  • Moderators
Posted

It depends if you use custom table Keys. When you use them in combination with the pairs loop. (not ipairs)

It is possible and manage able.

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