How to create a HUD toggle bind in Source engine games

source-engine

HUD is disabled with the cl_drawhud 0 command and enabled again with cl_drawhud 1. Additionally r_drawviewmodel 0 and r_drawviewmodel 1 can be used to toggle the viewmodel. Instead of pasting the commands into the console every time I want to capture a screenshot I'd prefer a hotkey to toggle (H would be sensible) which can be achieved by using alias, I believe.

Best Answer

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.