P
P
PaffosONE2020-04-04 20:10:43
Lua
PaffosONE, 2020-04-04 20:10:43

How to optimize hud (GLua)?

Please tell me, 2 minutes after connecting to the server, the fps drops from 120 to 16.
Tell me how to optimize the thin.
The code:

surface.CreateFont( "HUD", {
  font = "TargetID",
  extended = false,
  size = 18,
  weight = 600,
  blursize = 0,
  scanlines = 0,
  antialias = true,
  underline = false,
  italic = false,
  strikeout = false,
  symbol = false,
  rotary = false,
  shadow = false,
  additive = false,
  outline = false,
})

local function ShowHud()
local ply = LocalPlayer()
local heart = ply:Health()
local armor = ply:Armor()
local money = ply:getDarkRPVar("money")
local salary = ply:getDarkRPVar("salary")
draw.RoundedBox(0,0,0,2000,45,Color(45,45,45,225))



draw.DrawText("Здоровья:" .. ' ' .. heart .. '%',"HUD",50,12,Color(255,255,255),0)
draw.DrawText("Броня:" .. ' ' .. armor .. '%',"HUD",210,12,Color(255,255,255),0)
draw.DrawText("Деньги:" .. ' ' .. money .. '$',"HUD",355,12,Color(255,255,255),0)
draw.DrawText("Зарплата:" .. ' ' .. salary .. '$',"HUD",525,12,Color(255,255,255),0)

local img_health = vgui.Create('DImage')
img_health:SetSize(20,20)
img_health:SetPos(24,12)
img_health:SetImage("//garrysmod/materials/icon16/heart.png")

local img_armor = vgui.Create('DImage')
img_armor:SetSize(20,20)
img_armor:SetPos(325,12)
img_armor:SetImage("//garrysmod/materials/icon16/money.png")
end

hook.Add("HUDPaint","paintedhud",ShowHud)

Answer the question

In order to leave comments, you need to log in

1 answer(s)
H
hawww, 2020-04-10
@PaffosONE

This is because you are creating a new vgui object (DImage panel) every frame (the HUDPaint hook is called every frame). I can offer two solutions:
1. Move the creation of DImage outside the hook (outside the ShowHud function)

local img_health = vgui.Create('DImage')
img_health:SetImage("//garrysmod/materials/icon16/heart.png")
img_health:SetSize(20,20)

local img_armor = vgui.Create('DImage')
img_armor:SetImage("//garrysmod/materials/icon16/money.png")
img_armor:SetSize(20,20)

local function ShowHud()    
    if ValidPanel(img_health) then
        img_health:SetPos(24,12)
    end
    
    if ValidPanel(img_armor) then
        img_armor:SetPos(325,12)
    end
end
hook.Add("HUDPaint","paintedhud",ShowHud)

In this case, when the code is reloaded or restarted, the previous icons are not deleted, but new ones are created. If this interferes, then you can make the variables global, delete the past DImage before creating a new one:
if ValidPanel(DarkRPHUD_Img_Health) then
    DarkRPHUD_Img_Health:Remove()
    DarkRPHUD_Img_Health = nil
end

if ValidPanel(DarkRPHUD_Img_Armor) then
    DarkRPHUD_Img_Armor:Remove()
    DarkRPHUD_Img_Armor = nil
end

DarkRPHUD_Img_Health = vgui.Create('DImage')
DarkRPHUD_Img_Health:SetImage("//garrysmod/materials/icon16/heart.png")
DarkRPHUD_Img_Health:SetSize(20,20)

DarkRPHUD_Img_Armor = vgui.Create('DImage')
DarkRPHUD_Img_Armor:SetImage("//garrysmod/materials/icon16/money.png")
DarkRPHUD_Img_Armor:SetSize(20,20)

local function ShowHud()    
    if ValidPanel(DarkRPHUD_Img_Health) then
        DarkRPHUD_Img_Health:SetPos(24,12)
    end
    
    if ValidPanel(DarkRPHUD_Img_Armor) then
        DarkRPHUD_Img_Armor:SetPos(325,12)
    end
end
hook.Add("HUDPaint","paintedhud",ShowHud)

2. Use texture or material + surface library
Rendering images in this way is simplified.
local img_health = Material( "icon16/heart.png" )
local img_armor = Material( "icon16/money.png" )

local function ShowHud()    
    surface.SetDrawColor( 255, 255, 255 ) -- устанавливается цвет (r, g, b, a)

    surface.SetMaterial( img_health ) -- устанавливается материал, созданный заранее
    surface.DrawTexturedRect( 24, 12, 20, 20 ) -- x, y, width, height

    surface.SetMaterial( img_armor ) -- устанавливается материал, созданный заранее
    surface.DrawTexturedRect( 325, 12, 20, 20 ) -- x, y, width, height
end
hook.Add("HUDPaint","paintedhud",ShowHud)

surface.SetMaterial( Material( "icon16/heart.png" ) ) -- тоже возможно, но советуется выносить создание Material в переменную вне хука (согласно вики)

Something like this:
surface.CreateFont( "HUD", {
  font = "TargetID",
  extended = false,
  size = 18,
  weight = 600,
})

local img_health = Material( "icon16/heart.png" )
local img_armor = Material( "icon16/money.png" )

local function ShowHud()
    local ply = LocalPlayer()
    local heart = ply:Health()
    local armor = ply:Armor()
    local money = ply:getDarkRPVar("money")
    local salary = ply:getDarkRPVar("salary")
    draw.RoundedBox(0,0,0,2000,45,Color(45,45,45,225))

    draw.DrawText("Здоровья:" .. ' ' .. heart .. '%',"HUD",50,12,Color(255,255,255),0)
    draw.DrawText("Броня:" .. ' ' .. armor .. '%',"HUD",210,12,Color(255,255,255),0)
    draw.DrawText("Деньги:" .. ' ' .. money .. '$',"HUD",355,12,Color(255,255,255),0)
    draw.DrawText("Зарплата:" .. ' ' .. salary .. '$',"HUD",525,12,Color(255,255,255),0)

    surface.SetDrawColor( 255, 255, 255 ) -- устанавливается цвет (r, g, b, a)

    surface.SetMaterial( img_health ) -- устанавливается материал, созданный заранее
    surface.DrawTexturedRect( 24, 12, 20, 20 ) -- x, y, width, height

    surface.SetMaterial( img_armor ) -- устанавливается материал, созданный заранее
    surface.DrawTexturedRect( 325, 12, 20, 20 ) -- x, y, width, height
end
hook.Add("HUDPaint","paintedhud",ShowHud)

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question