How to create a HUD toggle bind in Source engine games?
Solution 1:
I am not sure, if all Source engine games do it this way, but in Team Fortress 2 I use the autoexec.cfg file together with a few additional files. >Reference to a relevant Portal 2 question
Of course, you can also use the syntax described below directly in the console as well - it just won't be persistent that way.
In the games config directory (e.g. Team Fortress 2/tf/cfg) you can find (or create if it isn't there) a file called autoexec.cfg. You should not change the existing config.cfg, but you can have a look into that file for further reference.
In this autoexec.cfg file we can add commands that will be automatically called on launch of the game. You should be able to add a line for your keybind in here. The syntax may look like the following:
bind "h" "incrementvar r_drawviewmodel 0 1 1"
bind "j" "incrementvar cl_drawhud 0 1 1"
host_writeconfig
This will toggle the r_drawviewmodel command each time you press the "h"-Button. The incrementvar keyword will count upwards from 0 until it reaches 1 by increments of 1 - that's how that works. It works the same on on the "j"-key with cl_drawhud. If you want to do both at the same time, you can use a semicolon to bind multiple command to one key:
bind "h" "incrementvar r_drawviewmodel 0 1 1; incrementvar cl_drawhud 0 1 1"
host_writeconfig
The last line is sometimes needed, sometimes not - it can be frickly and different from game to game in my experience.
In certain games sv_cheats might need to be set to 1 for this to work (I just encountered this problem in CS:GO).
You may also want to make sure to save the file in UTF-8 encoding, if it is not working. When using the Windows Notepad, there is a dropdown menu in the save file interface that reads ANSI by default.
Solution 2:
You can use the following commands in your Source game (tested in Half-Life 2: Episode 2). Paste and enter one line at a time:
alias noHUD "HUD_toggle1"
alias HUD_toggle1 "cl_drawhud 0; r_drawviewmodel 0; alias noHUD HUD_toggle2"
alias HUD_toggle2 "cl_drawhud 1; r_drawviewmodel 1; alias noHUD HUD_toggle1"
bind "h" noHUD
The names of the functions eventually don't really matter.