Binding a VGUI to F1-F4

From GMod Wiki

Jump to: navigation, search
Binding a VGUI to F1-F4
Page white text.png Description:How to make a VGUI popup with F1, F2, F3 or F4
link=User:WakeboarderCWB Original Author:WakeboarderCWB
Calendar.png Created:7th March 2010 (19:20)
Table edit.png Updated:22nd September 2011

Contents

Introduction

Hey. In this tutorial I will teach you how to make a VGUI popup with F1, F2, F3 or F4. I expect you to know how to make a VGUI.

Let's get started...

Clientside

First, we need to make a Derma VGUI (Virtual Graphical User Interface). Create your Derma clientside. NewerClient.png This is my code:

local function MyMenu()
    local Menu = vgui.Create("DFrame")
    Menu:SetPos(ScrW() / 2 - 400, ScrH() / 2 - 400)
    Menu:SetSize(800, 700)
    Menu:SetText("My Menu")
    Menu:SetDraggable(false)
    Menu:ShowCloseButton(true)
    Menu:MakePopup()
 
    local Text = vgui.Create("DLabel",Menu)
    //You can leave out the parentheses if there is a single string as an argument.
    Text:SetText "You opened the menu!"
	Text:SizeToContents()
    Text:Center()
end
usermessage.Hook("MyMenu", MyMenu)

Okay, now we have finished creating our clientside Derma and User Message hook, we can make the code to make it popup with F1 or F2 etc.


Serverside

Now, assuming you're making a gamemode, open init.lua and put this in there:

function GM:ShowHelp( ply ) -- This hook is called everytime F1 is pressed.
    umsg.Start( "MyMenu", ply ) -- Sending a message to the client.
    umsg.End()
end --Ends function

Note that if you are not making a gamemode you will have to use hook.Add instead. Example Below.

function MyMenu( ply ) --Start the function
    umsg.Start( "MyMenu", ply ) --Send the contents of "MyMenu" to the client
    umsg.End()
end --End the function
hook.Add("ShowHelp", "MyHook", MyMenu) --Add the hook "ShowHelp" so it opens with F1

Conclusion

This is the simplest way of how to do it, below are the hook names for F1 - F4:

GM:ShowHelp --F1
GM:ShowTeam --F2
GM:ShowSpare1 --F3
GM:ShowSpare2 --F4

NOTE: An alternative method would be to use NewerClient.png input.IsKeyDown with the IN_KEYS enums.

Personal tools
Namespaces
Variants
Actions
Navigation
Lua Scripting
Functions
Hooks
Toolbox