ok, so I'm making a SWEP with a primary function to create a vgui frame. For the most part, it works, although occasionally it creates 2, and sometimes 3 of them, underneath the first.
This code loks like the following:
[code]function SWEP:PrimaryAttack()
local DFrame = vgui.Create("DFrame")
DFrame:SetSize(512,512)
DFrame:SetTitle("Your Pocket")
DFrame:SetVisible(true)
DFrame:SetDraggable(true)
DFrame:ShowCloseButton(true)
DFrame:SetDeleteOnClose(true)
DFrame:MakePopup()
DFrame:Center()
end [/code]
Any help would be greatly appreciated.
Thank you.
I'm presuming you'd have to make a check.
[lua]
function SWEP:PrimaryAttack()
if( DFrame ) then DFrame:Remove() end
local DFrame = vgui.Create("DFrame")
DFrame:SetSize(512,512)
DFrame:SetTitle("Your Pocket")
DFrame:SetVisible(true)
DFrame:SetDraggable(true)
DFrame:ShowCloseButton(true)
DFrame:SetDeleteOnClose(true)
DFrame:MakePopup()
DFrame:Center()
end
[/lua]
or you can
[lua]
if( DFrame ) then return end
[/lua]
Or just make it before the check.
[lua]function SWEP:PrimaryAttack()
local DFrame
if( DFrame ) then DFrame:Remove() end
DFrame = vgui.Create("DFrame")
DFrame:SetSize(512,512)
DFrame:SetTitle("Your Pocket")
DFrame:SetVisible(true)
DFrame:SetDraggable(true)
DFrame:ShowCloseButton(true)
DFrame:SetDeleteOnClose(true)
DFrame:MakePopup()
DFrame:Center()
end
[/lua]
[QUOTE=CowThing;19231856]Or just make it before the check.
[lua]function SWEP:PrimaryAttack()
local DFrame
if( DFrame ) then DFrame:Remove() end
DFrame = vgui.Create("DFrame")
DFrame:SetSize(512,512)
DFrame:SetTitle("Your Pocket")
DFrame:SetVisible(true)
DFrame:SetDraggable(true)
DFrame:ShowCloseButton(true)
DFrame:SetDeleteOnClose(true)
DFrame:MakePopup()
DFrame:Center()
end
[/lua][/QUOTE]
Still will duplicate.
[lua]function SWEP:PrimaryAttack()
local DFrame
if DFrame:IsVisible() then DFrame:Remove() end
DFrame = vgui.Create("DFrame")
DFrame:SetSize(512,512)
DFrame:SetTitle("Your Pocket")
DFrame:SetVisible(true)
DFrame:SetDraggable(true)
DFrame:ShowCloseButton(true)
DFrame:SetDeleteOnClose(true)
DFrame:MakePopup()
DFrame:Center()
end[/lua]
Don't hold me on this, I'm very new to Lua.
You have to remove the local, or store the frame in the SWEP table.
Or make it a member of the SWEP? Would self.DFrame work? Doesn't clog up the global table that way. Also the variable name would need to change if it was made global, DFrame is far too generic.
Sorry, you need to Log In to post a reply to this thread.