Closing ShowSpare1 Menu

Hey there friends,

I’ve made a simple menu using this code:

function Menu (ply)
    umsg.Start( "OpenMenu", ply )

hook.Add("ShowSpare1", "MenuHook", Menu)

function MenuCL()

    gui.SetMousePos(ScrW() - 168, ScrH() - 255)

    // Super seecreet menu shit

usermessage.Hook("OpenMenu", MenuCL)

Now I didn’t actually put any IsValid’s to check if the menu is already open, because pressing F3 again does nothing. Is there a way I can get F3 to close the menu, similar to how DarkRP does it?


He is doing it client-side. He just put the code in one lua tag.

Send the hook like you’re doing each time F3 is pressed, as you’re doing it. Set a global / GAMEMODE.F3Menu = vgui.Create … when it opens. At the top of the code, check to see if it exists, if it does remove it and set it as nil which will close the menu. That will make F3 open it when you press it once, and the next time it will close it. Make sure you return; end after you call :Remove( ) and then = nil for the F3Menu variable.

The top part is the server, unless you’re implying I should do it client only.

Or just

[lua]function base:OnKeyCodePressed( KEY_F4 ) base:Close() end
function base:OnKeyCodePressed( KEY_F3 ) base:Close() end[/lua]

Nah, brandon mis-read the post, sorry for the confusion. What I’m saying is rewrite the client function to look something like this:

[lua]function MenuCL( )
local _p = LocalPlayer( );

// If we have not opened the menu -- or if we have Remove'd it and set it to nil, create it. This is where clicks go i % 2 == 1 of the time
if ( !_p.__ActiveMenu ) then
	gui.EnableScreenClicker( true );
	gui.SetMousePos( ScrW( ) - 168, ScrH( ) - 255 );

	// Create the primary screen under a known / localized / globally accessible variable - Then we can test for it, and close it if it's open.
	_p.__ActiveMenu = vgui.Create( "DPanel" );

	// Other than change the variable for the primary / parent panel, keep the rest of the menu stuff the same.
	// .......
	// This is where we remove the panel; this is where it goes on i % 2 == 0 clicks of the time.

	// Remove the menu
	_p.__ActiveMenu:Remove( );

	// Make it nil; this cleans up and lets it go back to the first segment of the if-statement again, so that the menu can be re-created.
	_p.__ActiveMenu = nil;


That’s not a bad idea, thanks.

Damn it Acecool, some of your coding practices are disgusting. Why are you appending the menu to the player object?! Why not just a local variable at the top of the file or something. And why are you manually enabling the mouse cursor, that fucks nearly everything using VGUI focus up.

If you notice, I kept his code the same and made the changes. I do things the way I do things because of my experiences.

Edit: I’m referencing a client-side menu related to the player, on the player object because it’s a simple spot until my vguid is done.

In my experiences if you have it localized and have the menu open, it works well - as long as you don’t change the script while its opened. Basically, if you have the menu open while the gamemode is reloaded it can cause errors or not work the way it should.

Auto-refresh won’t break the system I displayed; but you are correct in the sense of what was highlighted…

If you define a local variable above, it will unless you add a reference ability such as


If it’s defined in the script, it’s not needed.