Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.0k views
in Technique[技术] by (71.8m points)

lua - (gLua) Derma not working. attempt to call method 'Close' (a nil value)

Error at function ContextClose()

And all derma menu not working

attempt to call method 'Close' (a nil value)

I've create derma menu and it worked in garrysmodlua folder,
then I moved flies to addon floder and it stopped working.

cl_init.lua

AddCSLuaFile("pbfhud.lua")
include("pbfhud.lua")

pbfhud.lua

AddCSLuaFile()

local statBarW, statBarH = ScrW() / 400, ScrH() / 10
local barW, barH = ScrW() / 4, ScrH() / 10
local btnW, btnH = ScrW() / 4, ScrH() / 20

surface.CreateFont( "NotDermaDefault", {
    font = "Arial",
    extended = false,
    size = 25,
    weight = 500,
    blursize = 0,
    scanlines = 0,
    antialias = true,
    underline = false,
    italic = false,
    strikeout = false,
    symbol = false,
    rotary = false,
    shadow = false,
    additive = false,
    outline = false,
} )
local function DrawInventorySlot(  )
    
end

local function DrawBar(x, y, w, h, color, text, val, name)
    local rtext = text .. ": " .. val
    local tW, tH = surface.GetTextSize(rtext)
    tH = tH / 2
    if (name == true) then
       tW = tW / 7
    end

    draw.RoundedBox(0, x, y, w, h, color)
    draw.SimpleText(rtext, "NotDermaDefault", barW / 2 - tW / 7, barH / 2 - tH + y, Color( 255, 255, 255, 255 ), TEXT_ALIGN_CENTER, TEXT_ALIGN_TOP)

end

function ContextOpen()
    gui.EnableScreenClicker( true )
    menu = vgui.Create("DFrame")
    menu:SetSize(ScrW(), ScrH() + 100)
    menu:ShowCloseButton(false)
    menu:SetTitle("")
    menu:Center()
    menu:SetDraggable(false)
    menu:SetBackgroundBlur(true)
    menu:SetMouseInputEnabled(true)
    menu.Paint = function()
        draw.RoundedBox(0, 0, 0, ScrW(), ScrH(), Color(0,0,0,100))
        local blur = Material("pp/blurscreen") 
        local function blurPanel(panel, amount) 
            local x, y = panel:LocalToScreen(0, 0) 
            local scrW, scrH = ScrW(), ScrH() 
            surface.SetDrawColor(255, 255, 255) 
            surface.SetMaterial(blur) 
            for i = 1, 6 do
                blur:SetFloat("$blur", (i / 3) * (amount or 6)) 
                blur:Recompute() 
                render.UpdateScreenEffectTexture() 
                surface.DrawTexturedRect(x * -1, y * -1, scrW, scrH) 
            end 
        end
        blurPanel(menu, 5)
        draw.RoundedBox(0, 0, 0, ScrW() / 4, ScrH(), Color(0,0,0,50))
        -- Stats
        DrawBar(0, 0, barW, barH, Color(235, 235, 235, 140), "Имя", LocalPlayer():getDarkRPVar("rpname"), true )
        DrawBar(0, statBarH, LocalPlayer():Health() * statBarW, barH, Color(255, 50, 43, 140), "Здоровье", LocalPlayer():Health(), false )
        DrawBar(0, statBarH * 2, LocalPlayer():Armor() * statBarW, barH, Color(43, 128, 255, 140), "Броня", LocalPlayer():Armor(), false )
        DrawBar(0, statBarH * 3, barW, barH, Color(255, 149, 43, 140), "Работа", LocalPlayer():getDarkRPVar("job"), false )
        DrawBar(0, statBarH * 4, barW, barH, Color(0, 138, 11, 140), "Зарплата", LocalPlayer():getDarkRPVar("salary"), false )

        -- Inventory
        draw.RoundedBox(0, barW, 0, ScrW() / 1.7, ScrH(), Color(0,0,0,50))

    end
        -- Buttons
    --fb = vgui.Create("DButton", menu)
    --fb:SetSize(btnW, btnH)
    --fb:SetPos(0, btnH * 12)
    --fb:SetText("")

end

function ContextClose()
    menu:Close()
    gui.EnableScreenClicker( false )
end

hook.Add ("OnContextMenuOpen", "Context", ContextOpen)
hook.Add ("OnContextMenuClose", "Context", ContextClose)

Files location

..addonspbf_hudluaautorunclientcl_init.lua
..addonspbf_hudluaautorunclientpbfhud.lua
question from:https://stackoverflow.com/questions/65857905/glua-derma-not-working-attempt-to-call-method-close-a-nil-value

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

SOLVED

I used same identifiers for hooks. If you have same problem just change hook indentifier:

From:

hook.Add ("OnContextMenuOpen", "Context", ContextOpen)
hook.Add ("OnContextMenuClose", "Context", ContextClose)

To:

hook.Add ("OnContextMenuOpen", "Context_open", ContextOpen)
hook.Add ("OnContextMenuClose", "Context_close", ContextClose)

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...