试图调用全局' addButtons' (零值)

时间:2016-06-20 21:30:40

标签: lua

我需要帮助解决一个非常烦人的错误,我已经使用我的GMOD游戏模式。我正在尝试制作自定义菜单,但我收到此错误:

[ERROR] gamemodes/tdm/gamemode/custom_menu.lua:20: attempt to call global 'addButtons' (a nil value)
  1. gameMenu - gamemodes/tdm/gamemode/custom_menu.lua:20
   2. unknown - gamemodes/tdm/gamemode/custom_menu.lua:32
    3. include - [C]:-1
     4. unknown - gamemodes/tdm/gamemode/cl_init.lua:3

这是我的代码:

custom_menu.lua

local Menu

function gameMenu()
    if(Menu == nil) then
        Menu = vgui.Create("DFrame")
        Menu:SetSize(750, 500)
        Menu:SetPos(ScrW() / 2 - 325, ScrH() / 2 - 250)
        Menu:SetTitle("Gamemode Menu")
        Menu:SetDraggable(true)
        Menu:ShowCloseButton(false)
        Menu:SetDeleteOnClose(false)
        Menu.Paint = function()
            surface.SetDrawColor(60, 60, 60, 255)
            surface.DrawRect(0, 0, Menu:GetWide(), Menu:GetTall())

            surface.SetDrawColor(40, 40, 40, 255)
            surface.DrawRect(0, 24, Menu:GetWide(), 1)
        end

        addButtons(Menu)
        gui.EnableScreenClicker(true)
    else
        if(Menu:IsVisible()) then
            Menu:SetVisible(false)
            gui.EnableScreenClicker(false)
        else
            Menu:SetVisible(true)
            gui.EnableScreenClicker(true)
        end
    end
end
concommand.Add("open_game_menu", gameMenu())

function addButtons(Menu)
    local playerButton = vgui.Create("DButton")
    playerButton:SetParent(Menu)
    playerButton:SetText("")
    playerButton:SetSize(100, 50)
    playerButton:SetPos(0, 25)
    playerButton.Paint = function()
        --Color of entire button
        surface.SetDrawColor(50, 50, 50, 255)
        surface.DrawRect(0, 0, playerButton:GetWide(), playerButton:GetTall())

        --Draw Bottom and Right borders
        surface.SetDrawColor(40, 40, 40, 255)
        surface.DrawRect(0, 49, playerButton:GetWide(), 1)
        surface.DrawRect(99, 0, 1, playerButton:GetTall())

        --Draw Text
        draw.DrawText("Player", "DermaDefaultBold", playerButton:GetWide() / 2, 17, Color(255, 255, 255, 255), 1)
    end

    playerButton.DoClick = function(playerButton)
        local playerPanel = Menu:Add("PlayerPanel")
    end
    local shopButton = vgui.Create("DButton")
    shopButton:SetParent(Menu)
    shopButton:SetText("")
    shopButton:SetSize(100, 50)
    shopButton:SetPos(0, 75)
    shopButton.Paint = function()
        --Color of entire button
        surface.SetDrawColor(50, 50, 50, 255)
        surface.DrawRect(0, 0, shopButton:GetWide(), shopButton:GetTall())

        --Draw Bottom and Right borders
        surface.SetDrawColor(40, 40, 40, 255)
        surface.DrawRect(0, 49, shopButton:GetWide(), 1)
        surface.DrawRect(99, 0, 1, shopButton:GetTall())

        --Draw Text
        draw.DrawText("Shop", "DermaDefaultBold", shopButton:GetWide() / 2, 17, Color(255, 255, 255, 255), 1)
    end

    shopButton.DoClick = function(shopButton)
        local shopPanel = Menu:Add("ShopPanel")
    end
end



--Player Panel

PANEL = {} -- Create an empty panel

function PANEL:Init() --Initialize the panel
    self:SetSize(650, 475)
    self:SetPos(100, 25)
end

function PANEL:Paint(w, h)
    draw.RoundedBox(0, 0, 0, w, h, Color(0, 0, 0, 255))
end

vgui.Register("PlayerPanel", PANEL, "Panel")

--End Player Panel

--Shop Panel

PANEL = {} -- Create an empty panel

function PANEL:Init() --Initialize the panel
    self:SetSize(650, 475)
    self:SetPos(100, 25)
end

function PANEL:Paint(w, h)
    draw.RoundedBox(0, 0, 0, w, h, Color(255, 255, 255, 255))
end

vgui.Register("ShopPanel", PANEL, "Panel")

--End Shop Panel

init.lua:

AddCSLuaFile( "shared.lua" )
AddCSLuaFile( "cl_init.lua" )
AddCSLuaFile( "testhud.lua" )

local open = false

include ( 'shared.lua' )

local open = false

function GM:PlayerInitialSpawn(ply)
    if(ply:GetPData("playerLvl") == nil) then
        ply:SetNWInt("playerLvl", 1)
    else
        ply:SetNWInt("playerLvl", ply:GetPData("playerLvl"))
    end
    if(ply:GetPData("playerExp") == nil) then
        ply:SetNWInt("playerExp", 0)
    else
        ply:SetNWInt("playerExp", ply:GetPData("playerExp"))
    end
    if(ply:GetPData("playerMoney") == nil) then
        ply:SetNWInt("playerMoney", 0)
    else
        ply:SetNWInt("playerMoney", ply:GetPData("playerMoney"))
    end
end

function GM:OnNPCKilled(npc, attacker, inflictor)

    attacker:SetNWInt("playerMoney", attacker:GetNWInt("playerMoney") + 100)

    attacker:SetNWInt("playerExp", attacker:GetNWInt("playerExp") + 100)

    checkForLevel(attacker)

end

function GM:PlayerDeath(victim, inflictor, attacker)

    attacker:SetNWInt("playerMoney", attacker:GetNWInt("playerMoney") + 100)

    attacker:SetNWInt("playerExp", attacker:GetNWInt("playerExp") + 100)

    checkForLevel(attacker)

end

function GM:PlayerLoadout(ply)
    ply:Give("m9k_l85")
    ply:Give("m9k_colt1911")
    ply:Give("m9k_knife")
    ply:Give("m9k_m61_frag")

    ply:GiveAmmo(500, "ar2", true)
    ply:GiveAmmo(100, "pistol", true)

    return true
end

function checkForLevel(ply)
    local expToLevel = (ply:GetNWInt("playerLvl") * 100) * 2
    local curExp = ply:GetNWInt("playerExp")
    local curLvl = ply:GetNWInt("playerLvl")

    if(curExp >= expToLevel) then
        curExp = curExp - expToLevel

        ply:SetNWInt("playerExp", curExp)
        ply:SetNWInt("playerLvl", curLvl + 1)
    end
end

function GM:PlayerDisconnected(ply)
    ply:SetPData("playerExp", ply:GetNWInt("playerExp"))
    ply:SetPData("playerLvl", ply:GetNWInt("playerLvl"))
    ply:SetPData("playerMoney", ply:GetNWInt("playerMoney"))
end

function GM:ShutDown()
    for k, v in pairs(player.GetAll()) do
        v:SetPData("playerExp", v:GetNWInt("playerExp"))
    v:SetPData("playerLvl", v:GetNWInt("playerLvl"))
    v:SetPData("playerMoney", v:GetNWInt("playerMoney"))
    end
end

function GM:ShowSpare2(ply)
    ply:ConCommand("open_game_menu")
end

谢谢!

-Graham

1 个答案:

答案 0 :(得分:0)

你打电话

concommand.Add("open_game_menu", gameMenu())

此时调用addButtons(Menu) nil,因为函数定义在concommand.Add("open_game_menu", gameMenu())之后。{/ p>

一切都在那里。您只需阅读错误消息。

更改脚本中的顺序以修复错误。