• Old HUD Trying to update but causes Lagg.
    12 replies, posted
Hello everyone, I decided to get back into an old gamemode but I have a problem. The HUD Lags, I've been trying to search a way to fix it but unfortunate with no results. Here is the piece of the code. [B]Its pretty big[/B] [CODE]-- Draw the player's information. function GM:DrawPlayerInformation() local width = 0; local height = 0; -- Create a table to store the text. local text = {}; local information = {}; -- Insert the player's information into the text table. table.insert( text, {"Gender: "..(lpl._Gender or "Male"), "gui/silkicons/heart.png"} ); table.insert( text, {"Salary: $"..(lpl._Salary or 0), "gui/silkicons/folder_go.png"} ); table.insert( text, {"Money: $"..(lpl._Money or 0), "gui/silkicons/star.png"} ); table.insert( text, {"Details: "..lpl:GetNetworkedString("Details"), "gui/silkicons/status_offline.png"}); table.insert( text, {"Clan: "..lpl:GetNetworkedString("Clan"), "gui/silkicons/group.png"} ); table.insert( text, {"Job: "..lpl:GetNetworkedString("Job"), "gui/silkicons/wrench.png"} ); -- Loop through each of the text and adjust the width. for k, v in pairs(text) do if (string.Explode( ":", v[1] )[2] ~= " ") then if ( v[2] ) then width = self:AdjustMaximumWidth("ChatFont", v[1], width, nil, 24); else width = self:AdjustMaximumWidth("ChatFont", v[1], width); end -- Insert this text into the information table. table.insert(information, v); end end -- Add 16 to the width and set the height of the box. width = width + 16; height = (18 * #information) + 14; -- The position of the information box. local x = 8; local y = ScrH() - height - 8; -- Draw a rounded box to put the information text onto. draw.RoundedBox( 2, x, y, width, height, color_black_alpha ); -- Increase the x and y position by 8. x = x + 8; y = y + 8; -- Draw the information on the box. for k, v in pairs(information) do if ( v[2] ) then self:DrawInformation(v[1], "ChatFont", x + 24, y, color_white, 255, true); -- Draw the icon that respresents the text. surface.SetTexture( surface.GetTextureID( v[2] ) ); surface.SetDrawColor(255, 255, 255, 255); surface.DrawTexturedRect(x, y - 1, 16, 16); else self:DrawInformation(v[1], "ChatFont", x, y, color_white, 255, true); end -- Increase the y position. y = y + 18; end -- Return the width and height of the box. return width, height; end -- Draw the health bar. function GM:DrawHealthBar(bar) local health = math.Clamp(lpl:Health(), 0, 100); -- Draw the health and ammo bars. self:DrawBar("Default", bar.x, bar.y, bar.width, bar.height, color_red_alpha, "Health: "..health, 100, health, bar); end -- Draw the timer bar. function GM:DrawTimerBar(bar) local percento = math.Clamp((tonumber(lpl._JobTimeExpire-CurTime())/tonumber(lpl._JobTimeLimit))*100, 0, 100); lpl._NextEnter = lpl._NextEnter or CurTime() self:DrawBar("Default", bar.x, bar.y, bar.width, bar.height, color_orange_alpha, "Time Left: "..string.ToMinutesSeconds(math.floor(tonumber(lpl._JobTimeExpire)-CurTime())), 100, percento, bar); end -- Draw the ammo bar. function GM:DrawAmmoBar(bar) local weapon = lpl:GetActiveWeapon(); -- Check if the weapon is valid. if ( IsValid(weapon) ) then if ( !self.ammoCount[ weapon:GetClass() ] ) then self.ammoCount[ weapon:GetClass() ] = weapon:Clip1(); end -- Check if the weapon's first clip is bigger than the amount we have stored for clip one. if ( weapon:Clip1() > self.ammoCount[ weapon:GetClass() ] ) then self.ammoCount[ weapon:GetClass() ] = weapon:Clip1(); end -- Get the amount of ammo the weapon has in it's first clip. local clipOne = weapon:Clip1(); local clipMaximum = self.ammoCount[ weapon:GetClass() ]; local clipAmount = lpl:GetAmmoCount( weapon:GetPrimaryAmmoType() ); -- Check if the maximum clip if above 0. if (clipMaximum > 0) then self:DrawBar("Default", bar.x, bar.y, bar.width, bar.height, color_lightblue_alpha, "Ammo: "..clipOne.." ["..clipAmount.."]", clipMaximum, clipOne, bar); end end end -- Called when the bottom bars should be drawn. function GM:DrawBottomBars(bar) end -- Called when the top text should be drawn. function GM:DrawTopText(text) -- Check if the player is warranted. if (lpl:GetNetworkedString("Warrant") ~= "") then local _WarrantExpireTime = lpl._WarrantExpireTime; -- Text which is extended to the notice. local extension = "."; -- Check if the warrant expire time exists. if (_WarrantExpireTime) then local seconds = math.floor( _WarrantExpireTime - CurTime() ); -- Check if the amount of seconds is greater than 0. if (seconds > 0) then if (seconds > 60) then extension = " which expires in "..math.ceil(seconds / 60).." minute(s)."; else extension = " which expires in "..seconds.." second(s)."; end end end -- Check the class of the warrant. if (lpl:GetNetworkedString("Warrant") == "search") then text.y = self:DrawInformation("You have a search warrant"..extension, "ChatFont", text.x, text.y, color_brightgreen, 255, true, function(x, y, width, height) return x - width - 8, y; end); elseif (lpl:GetNetworkedString("Warrant") == "arrest") then text.y = self:DrawInformation("You have an arrest warrant"..extension, "ChatFont", text.x, text.y, color_brightgreen, 255, true, function(x, y, width, height) return x - width - 8, y; end); end end -- Check if the player is arrested. if ( lpl:Arrested() ) then local _UnarrestTime = lpl._UnarrestTime or 0; -- Check if the unarrest time is greater than the current time. if ( _UnarrestTime > CurTime() ) then local seconds = math.floor( _UnarrestTime - CurTime() ); -- Check if the amount of seconds is greater than 0. if (seconds > 0) then if (seconds > 60) then text.y = self:DrawInformation("You will be unarrested in "..math.ceil(seconds / 60).." minute(s).", "ChatFont", text.x, text.y, color_lightblue, 255, true, function(x, y, width, height) return x - width - 8, y; end); else text.y = self:DrawInformation("You will be unarrested in "..seconds.." second(s).", "ChatFont", text.x, text.y, color_lightblue, 255, true, function(x, y, width, height) return x - width - 8, y; end); end else text.y = self:DrawInformation("ur arrested but has no unarrest time (arrus is zero)", "ChatFont", text.x, text.y, color_lightblue, 255, true, function(x, y, width, height) return x - width - 8, y; end); end else text.y = self:DrawInformation("ur arrested but has no unarrest time (arrus below ctim)", "ChatFont", text.x, text.y, color_lightblue, 255, true, function(x, y, width, height) return x - width - 8, y; end); end end if lpl:Tied() then text.y = self:DrawInformation("You have been tied up!", "ChatFont", text.x, text.y, color_lightblue, 255, true, function(x, y, width, height) return x - width - 8, y; end); end -- Check if the player is wearing kevlar. if (lpl._ScaleDamage == 0.5) then text.y = self:DrawInformation("You are wearing kevlar which reduces damage by 50%.", "ChatFont", text.x, text.y, color_pink, 255, true, function(x, y, width, height) return x - width - 8, y; end); end end -- Called every time the HUD should be painted. function GM:HUDPaint() self:HUDPaintESP() if ( !self:IsUsingCamera() ) then self:DrawInformation(self.Config["Website URL"], "ChatFont", ScrW(), ScrH(), color_white, 255, true, function(x, y, width, height) return x - width - 8, y - height - 8; end); -- Loop through the money alerts. for k, v in pairs(self.moneyAlerts) do v.alpha = math.Clamp(v.alpha - 1, 0, 255); v.add = v.add + 1; -- Draw the money alert. self:DrawInformation(v.text, "ChatFont", ScrW(), ScrH() - 24 - v.add, v.color, v.alpha, true, function(x, y, width, height) return x - width - 8, y - height - 8; end); -- Check if the alpha is 0. if (v.alpha == 0) then self.moneyAlerts[k] = nil; end
No wonder, you are filling your table with data each frame. Not to mention creating locals each frame aswell.
[QUOTE=Netheous;47860168]No wonder, you are filling your table with data each frame. Not to mention creating locals each frame aswell.[/QUOTE] Do you know how to fix it perhaps?
[QUOTE=Netheous;47860168]No wonder, you are filling your table with data each frame. Not to mention creating locals each frame aswell.[/QUOTE] Whats bad with creating locals every frame? Doesn't garbage collection take care of it?
[QUOTE=polivlas;47860350]Whats bad with creating locals every frame? Doesn't garbage collection take care of it?[/QUOTE] Yeah, i knew creating tables every frame was a bad idea, but didn't know about the locals... if you don't change the vars, you can create them outside of the function.
But does anyone know how to fix that issue ? (Huds aren't really my thing)
[QUOTE=ExtReMLapin;47860370]Yeah, i knew creating tables every frame was a bad idea, but didn't know about the locals... if you don't change the vars, you can create them outside of the function.[/QUOTE] Just using locals each frame is fine, they're purged when a function call returns. It [i]may[/i] be a bad idea to create tables/vectors/angles each frame but not necesarily and sometimes you just can't avoid it.
I hate to refer to that but I am fairly certain that Acecool did a test on how locals created inside HUDPaint affect the performance.
So no one knows how to fix this issue?
[QUOTE=Rowie;47869300]So no one knows how to fix this issue?[/QUOTE] Define locals outside the functions and change their values inside. [editline]3rd June 2015[/editline] Same with that table.insert, inserting info to a table each frame is not so smart.
Alright thank you, I'll try it.
[code] -- Loop through each of the text and adjust the width. for k, v in pairs(text) do if (string.Explode( ":", v[1] )[2] ~= " ") then if ( v[2] ) then width = self:AdjustMaximumWidth("ChatFont", v[1], width, nil, 24); else width = self:AdjustMaximumWidth("ChatFont", v[1], width); end -- Insert this text into the information table. table.insert(information, v); end end [/code] This is probably causing a lot of issues. You should find a way to move logic like this so that it is only called when the data is changed, rather than per frame.
Thanks for that Willox, unfortunate thats the only way I could think of. Might have to make something that if something changed on the player it gets updated. No idea how to do it but ill take a look at it.
Sorry, you need to Log In to post a reply to this thread.