Panel Parenting Not Working Properly

I feel exceedingly stupid about this, but my panels are not following each other around when I parent them, nor are they having their positions set relative to its parent.

Here is my code:


local PANEL = {}

function PANEL:Init()
 background = vgui.Create("DFrame",self)
 background:SetPos(ScrW()/2-200,ScrH()/2-200)
 background:SetSize(400,400)
 background:ShowCloseButton(true)
 background:SetDraggable(true)
 background:SetBackgroundBlur(true)
 background:SetVisible(true)
 background:MakePopup()
 background:SetTitle("Test")
 
 sizex,sizey = background:GetSize()
 posx,posy = background:GetPos()
 dpanel = vgui.Create("DPanel",background)
 dpanel:SetParent(background)
 dpanel:SetPos(posx+10,posy+30)
 dpanel:SetSize(sizex*0.95,sizey*0.9)
 dpanel:SetVisible(true)
 dpanel:MakePopup(true)
 
end
vgui.Register("PanelTest", PANEL, "Panel")




function panel_command()
RPGMenu = vgui.Create("PanelTestl")
end
concommand.Add("test_panel",panel_command())


I don’t know what is wrong. If I was unclear about the issue, please let me know.

That code shouldn’t even work.



local PANEL = {}

function PANEL:Init()
	-- We don't create a panel inside a panel, and center it in NON LOCAL coordinates.
	-- We use self as a base DFrame
	self:SetSize( 400, 400 )
	-- Close button is shown by default
	-- The panel is draggable by default
	self:SetBackgroundBlur( true )
	-- The panel is visible by default
	self:SetTitle( "Test" )
	self:MakePopup()
	self:Center() -- Replaces the background:SetPos nonsense you were doing

	-- ALWAYS localize your panels inside PANEL functions
	local dpanel = vgui.Create( "DPanel", self )
	-- dpanel:SetParent( background ) -- You don't need to call SetParent if you used second argument in vgui.Create
	dpanel:SetPos( 10, 30 ) -- After we have parented the panel, we don't need to get the position of parent, we are woring in local coordinates
	dpanel:SetSize( self:GetWide() - 10, self:GetTall() - 40 ) -- Easier way to make offset size, this will leave 10 px margins from sides and bottom, 30px margin from top
	-- Again, panels are visible by default
	-- dpanel:MakePopup(true) -- You only need to call make pop up on the parent panel
	self.dpanel = dpanel -- Save the localized panel variable to the PANEL table,
	-- so you can access it later in other PANEL functions by doing self.dpanel:SetSize( 10, 10 )
 
end
vgui.Register("PanelTest", PANEL, "DFrame") -- Changed base element to DFrame.

function panel_command()
    RPGMenu = vgui.Create("PanelTest") -- You had an extra character there.
end
concommand.Add( "test_panel", panel_command ) -- You pass the function as panel_command, not as panel_command(), that will call it

Sorry about the whole “that code shouldn’t work” part. I changed some variables so it would be easier to read :S, that isn’t exactly what my code looks like.

Thanks by the way, works like a charm. I still have stuff to learn about VGUI :stuck_out_tongue: