Function for when a panel is closed?

Is there a way to apply a function when the ‘X’ on a panel is clicked?

[lua]Menu.OnClose = function()
–do stuff here
end
[/lua]


frame = vgui.Create( "DFrame" )
frame:SetPos( 0, 0 )
frame:SetSize( 295, 295 )
frame:SetTitle( "" )
frame:SetVisible( true )
frame:SetDraggable( false )
frame:ShowCloseButton( true )
frame:MakePopup()
frame.OnClose = function()
--Stuff goes here
end

So, in theory, this is how it should look?

Should work yes.

Or:
[lua]
function frame:OnClose()
–stuff

end
[/lua]

Hrm… Neither of those seem to do anything. Like if I put a self.Weapon:EmitSound the sound will not be played. Is there some kind of different thing that needs to be done for sweps?

Where are you defining self.Weapon?

Swep? How’re you using it?

I’m using it as an attachments menu, when the use and primary keys are pressed, the panel is drawn.

Show us the entire code.




function SWEP:Think()

if self.owner:KeyDown(IN_USE) and self.owner:KeyPressed(IN_ATTACK) then
	self:SetIronsights(true, self.Owner)
	self:Att()
end
end

function SWEP:Att()

self.IronSightsPos = self.AttSightsPos
self.IronSightsAng = self.AttSightsAng
self.Weapon:EmitSound( Sound("weapons/scar-l/SCARL_ClothMovement3.wav" ) )

if CLIENT then

frame = vgui.Create( "DFrame" )
frame:SetPos( 0, 0 )
frame:SetSize( 295, 295 )
frame:SetTitle( "Weapon Mods" )
frame:SetVisible( true )
frame:SetDraggable( false )
frame:ShowCloseButton( true )
frame:MakePopup()
frame.OnClose = function()
self:SetIronsights(false, self.Owner)
self.Weapon:EmitSound( Sound("weapons/scar-l/SCARL_ClothMovement3.wav" ) )
end

PropertySheet = vgui.Create( "DPropertySheet", frame )
PropertySheet:SetPos( 1, 30 )
PropertySheet:SetSize( 295, 295 )

ItemSheetOne = vgui.Create("DCollapsibleCategory", DermaPanel)
ItemSheetOne:SetPos( 25,50 )
ItemSheetOne:SetSize( 200, 50 )
ItemSheetOne:SetExpanded( 1 )
ItemSheetOne:SetLabel( "Top Rail" )

Category = vgui.Create( "DPanelList" )
Category:SetAutoSize( true )
Category:SetSpacing( 2 )
Category:EnableHorizontal( false )
Category:EnableVerticalScrollbar( true )

ItemSheetOne:SetContents( Category )

ContentOne = vgui.Create( "DButton" ) 
ContentOne:SetText( "Doctor" ) 
ContentOne.DoClick = function ()

self.Weapon:EmitSound( Sound("weapons/scar-l/SCARL_BoltRelease.wav" ) )
self.Owner:GetViewModel():SetBodygroup( 1, 0 )
end

Category:AddItem( ContentOne ) 

ContentTwo = vgui.Create( "DButton" ) 
ContentTwo:SetText( "Scope" ) 
ContentTwo.DoClick = function ()

self.Weapon:EmitSound( Sound("weapons/scar-l/SCARL_BoltRelease.wav" ) )
self.Owner:GetViewModel():SetBodygroup( 1, 1 )
end  
 
Category:AddItem( ContentTwo )

	PropertySheet:AddSheet("Available Attachments:", ItemSheetOne, "gui/silkicons/wrench", false, false, "Commands")
end
end


I have the weapon going into a twisted irons position, as to imitate the way Crysis does weapon attachments.

DoClick works just fine if anyone would like to know.

Instead of using the think hook to check, why not just do:

[lua]
function SWEP:PrimaryAttack()
if ( self.Owner:KeyDown(IN_USE) ) then
– Your stuff here.
end;
end;
[/lua]

I think that would just do the exact same thing as using think.

But it’s better to not use Think hook if you don’t need to.

I can sort out which one is more efficient once closing the panel actually does anything.

[lua]MsgN(“THIS WORKS!”)[/lua]
Put this besides your sound code in the frame:OnClose function and tell us whether you see the message in the console or not.
:pwn:

Nothing.

That’s weird.

Um, do what I did: Place an (almost) invisible DButton over the frame’s closing button.
I admit, I’ve had trouble with this before.
I just wanted to see if I’m the only one.

Alright, I’ll see what I can do.