Пользователь пока ничего не рассказал о себе

Наибольший вклад в теги

Все теги (1)

Лучшие ответы пользователя

Все ответы (1)
  • Как оптимизировать худ (GLua)?

    @hawww
    Это происходит из-за того, что ты создаешь новый vgui объект (панель DImage) каждый кадр (хук HUDPaint вызывается каждый фрейм). Могу предложить два варианта решения:
    1. Вынести создание DImage за пределы хука (вне функции ShowHud)
    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)

    В этом случае при перезагрузке кода или повторном запуске предыдущие иконки не удаляются, но создаются новые. Если это мешает, то можно сделать переменные глобальными, удалять прошлый DImage до создания нового:
    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. Использовать текстуру или материал + библиотеку surface
    Отрисовка изображений этим способом упрощается.
    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 в переменную вне хука (согласно вики)

    Как-то так:
    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)
    Ответ написан
    Комментировать