Here is my code:
[lua]
function mainMenu()
local mainMenu = vgui.Create( "DFrame" )
mainMenu:SetSize( 620, 710 )
mainMenu:SetTitle( "Herb Roleplaying" )
mainMenu:SetDraggable( false )
mainMenu:ShowCloseButton ( true )
mainMenu:MakePopup()
mainMenu:Center()
end
usermessage.Hook("ToggleMenu", mainMenu)
[/lua]
how would I make this toggle? i can turn it on with f1 by sending the usermessage ToggleMenu, but I cannot close it using f1.
because it need a
[lua]
mainMenu:SetVisible(false)
[/lua]
When you wanna close that
or you can use concommands and bind a key (the concommand must be toggleable)
I did that.
[lua]
if menuOpen == true then
menuOpen = false
elseif menuOpen == false then
menuOpen = true
end
local mainMenu = vgui.Create( "DFrame" )
mainMenu:SetSize( 620, 710 )
mainMenu:SetTitle( "Herb Roleplaying" )
mainMenu:SetDraggable( false )
mainMenu:ShowCloseButton ( true )
mainMenu:MakePopup()
mainMenu:Center()
mainMenu:SetVisible( menuOpen )
[/lua]
This function is being called from hitting f1. It sets menuOpen to true but then it wont let me set it back.
Set it back to false?
Just give the player a variable for when the menu is open/closed. If is it
already opened, close it, and vice versa.
Isn't that what I did?
Can you please give me an expample?
It isn't set to false initially, it is nil.
[lua]
if (menuIsOpen) then
menuIsOpen = false;
else
menuIsOpen = true;
end;
[/lua]
This is probably completly wrong, ive never done key hooks but what i do for some of my panels is you have it create the panel, or close it. I belvie you can also do the IsVisible(), instead of setting up a variable saying that its been opened.
[lua]
CreatePanel()
local mainMenu = vgui.Create( "DFrame" )
mainMenu:SetSize( 620, 710 )
mainMenu:SetVisbile(true)
mainMenu:SetTitle( "Herb Roleplaying" )
mainMenu:SetDraggable( false )
mainMenu:ShowCloseButton ( true )
mainMenu:MakePopup()
mainMenu:Center()
end
GM:KeyPress( pl, key )
if key == "f1" then
if mainMenu:IsVisible() == true then
mainMenu:close()
else
CreatePanel()
end
end
end
[/lua]
Is CreatePanel a function to open mainMenu elsewhere, can you paste all the related code?
The KeyPress hook requires you to use IN_ keys only. [b][url=wiki.garrysmod.com/?title=Gamemode.KeyPress]Gamemode.KeyPress [img]http://wiki.garrysmod.com/favicon.ico[/img][/url][/b]
Try this:
[code]
GM:KeyPress( pl, key )
if key == KEY_F1 then
if mainMenu:IsVisible() == true then
mainMenu:close()
else
CreatePanel()
end
end
end
[/code]
btw don't do that stupid if thing. Swapping it is much easier:
[CODE]swapme = !swapme --or
swapme = not swapme[/CODE]
it just does not want to work. It pops up but it won't close.
My code:
[lua]
local menuOpen = false
function GetKeyPress()
if input.IsKeyDown(KEY_F1) then
mainMenu()
end
end
hook.Add( "Think", "GetKeyPress", GetKeyPress )
function mainMenu()
menuOpen = !menuOpen
local mainMenu = vgui.Create( "DFrame" )
mainMenu:SetSize( 620, 710 )
mainMenu:SetTitle( "Derp" )
mainMenu:SetDraggable( false )
mainMenu:ShowCloseButton ( true )
mainMenu:MakePopup()
mainMenu:Center()
mainMenu:SetVisible( menuOpen )
[/lua]
[editline]21st March 2012[/editline]
What the code seems to be doing is drawing another mainMenu on top of the one that is already open.
[QUOTE=Duskling;35236329]It seems like it just keeps creating new menus[/QUOTE]
Yes that is exactly what it is doing. Try this.
[B]Edited to reflect a code update. My old example had a bug.[/B]
[lua]
// Create a variable above the functions
local mainMenu = nil
local visible = true
function GetKeyPress()
if input.IsKeyDown(KEY_F1) then
OpenMainMenu()
end
end
hook.Add( "Think", "GetKeyPress", GetKeyPress )
function OpenMainMenu()
// Toggle the visibility
visible = !visible
// NOTE: Updated this. I had it saying "menu" before which was wrong.
if( mainMenu == nil ) then
// Remove the "local" declaration and use the 'global (wthin scope)' variable.
mainMenu = vgui.Create( "DFrame" )
mainMenu:SetSize( 620, 710 )
mainMenu:SetTitle( "Derp" )
mainMenu:SetDraggable( false )
mainMenu:ShowCloseButton ( true )
mainMenu:MakePopup()
mainMenu:Center()
// Since this is the first time we are opening the menu, set the visibility to true.
mainMenu:SetVisible( true )
// And store the fact that it tis currently visible
visible = true
else
// If the menu already exists, just toggle the visibility
mainMenu:SetVisible ( visible )
end
end
[/lua]
MakePopup stops keypress
[lua]
Hook 'GetKeyPress' Failed: [HerbRP\gamemode\cl_mainmenu.lua:78] attempt to index upvalue 'mainMenu' (a function value)
[/lua]
...Strange...
[QUOTE=Duskling;35236835][lua]
Hook 'GetKeyPress' Failed: [HerbRP\gamemode\cl_mainmenu.lua:78] attempt to index upvalue 'mainMenu' (a function value)
[/lua]
...Strange...[/QUOTE]
Ah crap, my bad, sorry. Change your function name from "mainMenu" to something else like "OpenMainMenu"
[lua]
function GetKeyPress()
if input.IsKeyDown(KEY_F1) then
// Changed here
OpenMainMenu()
end
end
hook.Add( "Think", "GetKeyPress", GetKeyPress )
// Changed here
function OpenMainMenu()
// Toggle the visibility
visible = !visible
// NOTE: Updated this. I had it saying "menu" before which was wrong.
if( mainMenu == nil ) then
[/lua]
[B]Shizer, one other issue.... Give me a moment to refresh my old example and just re-use that.[/B]
Okey, I will wait ;3
[QUOTE=Duskling;35252580]Okey, I will wait ;3[/QUOTE]
Ah oops sorry. I already fixed it. Take a look at my code samples again (all of them should have all the fixed code). Basically the first bug you experienced was because the function was called "mainMenu" and there was also a variable called "mainMenu" which caused a conflict. The second bug I had in my code was just a stupid mismatching variable name >_<. Sorry for that. Try it again and let me know if it works!
Here is what the code does.
It creates a global(ish) variable called mainMenu. If that variable has nothing in it, the program creates the DermaMenu, assigns the menu to the variable, and then displays the derma screen to the user. Next time you call that function, it will see the menu already exists. So instead of recreating it, the program simply toggles the visibility flag. Thus achieving the functionality you want =)
I hope it works for you this time!
Thanks alot, this seems to work, somewhat.
I am having a problem where if you press down f1 it will quickly swap between open and closed. Is there a way to make it so that it doesn't run every second, only when f1 key is down?
[editline]26th March 2012[/editline]
I tried this:
[lua]
function GetKeyPress(ply, key)
if key == KEY_F1 then
OpenMainMenu()
end
end
hook.Add( "KeyPress", "GetKeyPress", GetKeyPress )
[/lua]
but it didn't seem to generate a response, not even an error.
Glad it works :D
You could try[B] KeyRelease[/B]. That would make it so it only happens when they let go (which consequently means only once). You could maybe do something with [B]BindPress[/B] as well though I have no experience so I don't know off the top of my head.
[B][URL="http://maurits.tv/data/garrysmod/wiki/wiki.garrysmod.com/indexcad9.html?title=Gamemode.KeyRelease"]KeyRelease[/URL][/B]
[B][URL="http://maurits.tv/data/garrysmod/wiki/wiki.garrysmod.com/index4d65.html?title=Gamemode.PlayerBindPress"]PlayerBindPress[/URL][/B]
[B]Edit[/B]
Or if you want to be complicated you could change the visibility state and then track whether "KeyRelease" has been activated. If it hasn't been activated, just return out of your ShowMenu function. This is probably not the best solution though. Just an idea.
Sorry, you need to Log In to post a reply to this thread.