Hiding/Removing Dimage

I’m wanting to create a menu using my own assets. I’m wanting the menu to appear whenever the user presses “O” and then disappear when the user lets go of “O”. I’m displaying the image using a DImage which someone suggested using on another topic. This is my script:


function CreateMenu(active)
	panel = vgui.Create("DPanel")
	panel:SetPos(ScrW() * 0.3, ScrH() * 0.13)
	panel:SetSize(600, 600)
	panel:MakePopup()
	panel:SetDrawBackground(false)
	if active == true then		
		MenuBorder = vgui.Create("DImage", panel)
		--MenuBorder:SetPos(ScrW() * 0.3, ScrH() * 0.13)
		MenuBorder:SetSize(150, 150)
		MenuBorder:SetImage("circle.png")	
	else
		panel:Remove()
	end
end

hook.Add("HUDPaint", "MenuButton", function()
	if input.IsKeyDown(KEY_O) then
		CreateMenu(true)
		--gui.EnableScreenClicker(true)
	else
		CreateMenu(false)
		--gui.EnableScreenClicker(false)
	end
end)

I’m having the DImage as a parent of DPanel as I want to remove the menu when the user lets go of the button, I couldn’t find anyway of removing the DImage so I thought I would try hiding the image using Remove() but it doesn’t seem to work and I’m not sure why. It creates the menu when i press the “O” button but stays there and never gets removed.

Could anyone help me?

You should check if it has already been created as what you’re doing will open it more than once and if they let go it won’t close.

Try something like this:



local panel = nil -- this should be defined before all that stuff

local function CreateMenu(active)
	if active then
		if panel ~= nil then return end
		panel = vgui.Create("DPanel")
		panel:SetPos(ScrW() * 0.3, ScrH() * 0.13)
		panel:SetSize(600, 600)
		panel:MakePopup()
		panel:SetDrawBackground(false)
		local MenuBorder = vgui.Create("DImage", panel)
		--MenuBorder:SetPos(ScrW() * 0.3, ScrH() * 0.13)
		MenuBorder:SetSize(150, 150)
		MenuBorder:SetImage("circle.png")	
	else
		if panel == nil then return end
		panel:Remove()
		panel = nil
	end
end

hook.Add("HUDPaint", "MenuButton", function()
	if input.IsKeyDown(KEY_O) then
		CreateMenu(true)
	else
		CreateMenu(false)
	end
end)


That should work, but I don’t get why you want to create an invisible DPanel… you can make DImages popups as well (I think)