Function which creates vgui

Hello, I’m trying to make a function which creates some vgui but it doesn’t work.
This is my code :



	local function ConfDoorAddButton(text, x, y)
        function base:PaintOver(w, h)
            surface.SetFont("ConfDoors_Text")
            draw.SimpleText(text, "ConfDoors_Text", x, y, white)
        end

        local button = vgui.Create("DButton", base)
        local status = true
        button:SetText("")
        surface.SetFont("ConfDoors_Text")
        button:SetSize(75, 20)
        button:SetPos(confdoors.w - button:GetWide() - 10, 17.5 + SwarzoxGetTextSize(text, "height"))

        function button:Paint(w, h)
            SwarzoxSwitchButton(0, 0, status)
        end

        function button:DoClick()
            if status == true then
                status = false
                LocalPlayer():ChatPrint("La porte ne se ferme plus toute seule")
            else
                status = true
                LocalPlayer():ChatPrint("La porte se ferme toute seule")
            end
        end
	end

    ConfDoorAddButton("Fermer cette porte automatiquement", 10, 35)


I wanna have 2 vgui if I put the function ConfDoorAddButton() 2 times

Seems like you didn’t define “base” frame.
UPD. Probably you have defined “base” frame before. If so, it should be visible when this function is being called.

That function does not include whatever ‘base’ is supposed to be. Try adding ‘base’ as an argument for your function, or better yet, don’t use a function at all (if possible).

[editline]13th August 2017[/editline]

ninja’d

This is the entire code :



confdoors = confdoors or {}
local white = Color(255, 255, 255)
local green = Color(0, 255, 0)
local red = Color(255, 0, 0)
local black = Color(0, 0, 0)

local function SwarzoxBlack(a)
    black.a = a

    return black
end

local blur = Material("pp/blurscreen")

local function SwarzoxBlur(panel, amount, heavyness)
    local x, y = panel:LocalToScreen(0, 0)
    local scrW, scrH = ScrW(), ScrH()
    surface.SetDrawColor(255, 255, 255)
    surface.SetMaterial(blur)

    for i = 1, (heavyness or 3) do
        blur:SetFloat("$blur", (i / 3) * (amount or 6))
        blur:Recompute()
        render.UpdateScreenEffectTexture()
        surface.DrawTexturedRect(x * -1, y * -1, scrW, scrH)
    end
end

local function SwarzoxSwitchButton(x, y, on)
    surface.SetDrawColor(DrawBlack(120))
    surface.DrawRect(x, y, 75, 20)
    surface.SetDrawColor(DrawBlack(160))
    surface.DrawRect(x + 2, y + 2, 71, 16)

    if on then
        green.a = 120
        surface.SetDrawColor(green)
        surface.DrawRect(x + 2, y + 2, 32.5, 16)
        draw.SimpleText("OFF", "ConfDoors_Text", x + 32.5 + (16.25 / 2), y - 1, white)
    else
        red.a = 120
        surface.SetDrawColor(red)
        surface.DrawRect(x + 41, y + 2, 32.5, 16)
        draw.SimpleText("ON", "ConfDoors_Text", x + 2 + (16.25 / 2), y - 1, white)
    end
end

local function SwarzoxGetTextSize(text, type)
    if type == "height" or type == "h" then
        local height = select(2, surface.GetTextSize(text))

        return height
    elseif type == "weight" or type == "w" then
        local weight = select(1, surface.GetTextSize(text))

        return weight
    end
end

local BASE = {}

function BASE:Init()
    self:MakePopup(true)
    self:SetSize(confdoors.w, confdoors.h)
    self:SetPos(ScrW() / 2 - confdoors.w / 2, ScrH() / 2 - confdoors.h / 2)
end

function BASE:Paint(w, h)
    SwarzoxBlur(self, 5)
    surface.SetDrawColor(SwarzoxBlack(140))
    surface.DrawRect(0, 0, w, h)
    surface.SetDrawColor(SwarzoxBlack(140))
    surface.DrawRect(0, 0, w, 7)
    surface.DrawRect(0, 21, w, 7)
    surface.DrawRect(0, self:GetTall() - 7, w, 7)
end

vgui.Register("ConfDoorsBaseMenu", BASE)

function ConfDoorsMenu()
    confdoors.w = 350
    confdoors.h = 400
		
    local base = vgui.Create("ConfDoorsBaseMenu")

	local function ConfDoorAddButton(text, x, y)
        function base:PaintOver(w, h)
            surface.SetFont("ConfDoors_Text")
            draw.SimpleText(text, "ConfDoors_Text", x, y, white)
        end

        local button = vgui.Create("DButton", base)
        local status = true
        button:SetText("")
        surface.SetFont("ConfDoors_Text")
        button:SetSize(75, 20)
        button:SetPos(confdoors.w - button:GetWide() - 10, 17.5 + SwarzoxGetTextSize(text, "height"))

        function button:Paint(w, h)
            SwarzoxSwitchButton(0, 0, status)
        end

        function button:DoClick()
            if status == true then
                status = false
                LocalPlayer():ChatPrint("La porte ne se ferme plus toute seule")
            else
                status = true
                LocalPlayer():ChatPrint("La porte se ferme toute seule")
            end
        end
	end

    ConfDoorAddButton("Fermer cette porte automatiquement", 10, 35)
	
    local close = vgui.Create("DButton", base)
    close:SetSize(100, 35)
    close:SetPos(base:GetWide() / 2 - close:GetWide() / 2, base:GetTall() - close:GetTall() - 17)
    close:SetTextColor(white)
    close:SetFont("ConfDoors_Text")
    close:SetText("Close")

    function close:Paint(w, h)
        surface.SetDrawColor(SwarzoxBlack(140))
        surface.DrawRect(0, 0, w, h)
    end

    function close:DoClick()
        base:Remove()
    end
end


As you can see ‘base’ is defined and when I call ConfDoorAddButton() it adds the vgui but when I call it 2 times the last vgui disappears