Pretty much what the title says. I want to make my vgui popup when I press F1, does anyone know how and can teach me? Thank you.
Here you go:
If you need more help, just tell us
I think he’ll probably need a bit more help than poining to the right function.
You want something to happen when something else happens. This can be called an Event though the terminology sometimes varies.
Suppose there’s a Sand Worm travelling in the direction you want to go, you’ll stick your hook in so it will pull you along.
Now you want to make some gui happen when you press F1 so you want to hook F1 with the gui.
To do this you use the **[Hook.Add
http://wiki.garrysmod.com/favicon.ico](http://wiki.garrysmod.com/?title=Hook.Add)** function. The first parameter is the event you want to hook, the second is a unique identifier, it can be helpful to put a memorable one in. The third parameter is the function itself.
The hook given above is the one corresponding to when F1 is pressed. Therefore you know that you have to start the hook like this
The unique identifier is up to you but it’s best to describe what it does and give it a prefix so it’s less likely to conflict with some other mod.
I’m guessing CWB might be a handy one to prefix with as it can be associated with your name. So our hook now looks like this:
[lua]hook.Add( “ShowHelp”, “CWB_ShowVGUI”[/lua]
Now you need to add the function itself. This can either be defined before you do the hook as a local function, any time if you’re giving it a global function or during the hooking with an anonymous function.
I tend to prefer anonymous functions because it helps associating it with the hook and to me looks tidier. If you’re not going to use the function anywhere else then it’s probably the best approach.
ShowHelp is a serverside function and has the player as the first parameter. So as part of the hook you need to take into account the player parameter like so.
[lua]hook.Add( “ShowHelp”, “CWB_ShowVGUI”, function(ply)
– code to make the player show the gui.
Unfortunately now we’re getting into client-server interaction territory. To start with I’ll show you how you can do this, however at the end I will show you how to do this all clientside.
To make the Client do something you have to send a Usermessage, which is the way that the server interacts with its clients. This is done with the umsg and usermessage libraries.
Here is what you have to do with comments explaining what happens
[lua]-- SERVERSIDE, this code has to go in a file that the server reads
– start the hook definition
hook.Add( “ShowHelp”, “CWB_ShowVGUI”, function(ply)
– say that we want to send a message to player ‘ply’ and that this message corresponds
– to the ‘CWB_ShowVGUI’ handler
– finish sending the usermessage, it’s done like this because you often want to send
– data to the client as well, in which case you use functions like umsg.String(‘string’)
– between the umsg.Start and umsg.End calls
– finish the hook definition
– CLIENTSIDE, this has to be in a file the client will read
– start defining the usermessage handler, it can take the usermessage as a parameter
– so you can retrieve data like str = um:ReadString()
usermessage.Hook( “CWB_ShowVGUI”, function(um)
– call the function to actually show the gui, you could replace the anonymous function
– with just the CWB_ShowVGUI function, however in the future you may want to make
– this conditional, such as if you sent a boolean you wouldn’t want to show the gui if
– you sent false
– here’s the function to make the GUI, if you want to call it something else then you have
– to change the name of the function in usermessage.Hook
– put your code to make the gui here
What this ends up doing is that when you press F1 clientside it sends this command to the server, then the server sends a message back telling the client to show the gui. This is sometimes wanted for authorization, but in this case we can skip this by using a different hook. The one that probably most applies is **[Gamemode.PlayerBindPress
http://wiki.garrysmod.com/favicon.ico](wiki.garrysmod.com/?title=Gamemode.PlayerBindPress)**. This is called when a key that is bound is pressed. You could make something happen when people press the jump button for example. Here we’re going to use it to tell when someon pressed the button bound to ShowHelp.
[lua]-- ply is the player that pressed the button, since this is always the same as
– LocalPlayer() this parameter is redundant
– bind is a string with the name of the bind that was pressed
– pressed is the current state, it’s true if the key was pressed, false if it was released
hook.Add(“PlayerBindPressed”, “CWB_ShowVGUI”, function(ply, bind, pressed)
– this gets called for every bind that gets pressed or released so we have to check
– if it was the showhelp bind and whether it was pressed rather than released
if bind == ‘gm_showhelp’ and pressed then
Thanks for that Fishface, this was very useful to me as well. :3: