Portal 2
评价数不足
Using .cfg files to execute commands in Source
由 Noggisoggi 制作
Can be used for every Source Engine game.
   
奖励
收藏
已收藏
取消收藏
Introduction
.cfg (configuration) files are scripts (a set of commands executed in sucession); like the developer console, but without output.

A more in-depth version can be found in Valve Developer Community; this guide is more of a beginner-friendly version.
Configuration file location/creation
Firstly, locate the cfg folder in your game's directory as this is the folder most Source games use.

It should be in the <game + mod> folder.

<Steam installation directory>/steamapps/common/<game name>/
In the common folder:
  • Half-Life 2/hl2/cfg/
  • Half-Life 2/episodic/cfg/
  • Half-Life 2/ep2/cfg/
  • Portal/hl2/cfg/
    or
  • Portal/portal/cfg/
    (Directory for the HL2 files remain the same as Portal is based off it)
  • Portal 2/portal2/cfg/
  • Garry's Mod/garrysmod/cfg/
  • Team Fortress 2/tf2/cfg/


Make sure you have file extensions enabled in your file manager.
    Windows (Explorer):
  • Open Control Panel
  • View by: Large icons (or small) > Folder Options > Switch to the 'View' tab > Advanced settings > Disable (uncheck) 'Hide extensions for known file types'

macOS (Finder, assuming you're running 10.14 or older)
  • Have the Finder focused
  • On the menu bar: Finder > Preferences > Advanced > Enable (check) 'Show all filename extensions'

Linux
Most file mangers have it enabled by default & if you're using the terminal, no additional configuration is required, apart from knowledge on how to use it. If it's disabled by default, you might be able to enable it by looking around in your file manager's settings.

In any case, create a file ending in .cfg; name doesn't matter.
Put commands you want in this file.
Executing .cfg files
Type in the console (filename must be enclosed in quotes if it has spaces):
exec <name>
Commands in cfg files are executed as if they were done from the console itself.
This is more apparent if you're using action commands (+jump, +attack, etc).
You see, when binding it to a key & using it, instead of jumping (+jump for this example), you would jump everytime after unpausing the game, which is the same result when you put the same command directly into the console.

To undo this action, instead of using '+' as the prefix, use '-' instead; that'll stop the action of whatever is put into it, say with the jump example.

Also there's autoexec.cfg, a script that's executed upon boot up of the game.
Some commands don't work with it upon boot, and in that case, use this:
exec autoexec
Bonus Use Case 1: Additional Chapters
You might have also seen the "chapterxx" (the 'xx' being the number) in the cfg folder. Those, of course, are chapters in the New Game menu & they usally execute:
map <mapname>
But, of course, they can be modified too.

For this example, in Portal, I enabled accessing the credits from the New Game chapter selection menu - it loads up a save from the map escape_02, wherein the credits sequence is about to play.


Modifications made
The crucial part, in Portal/portal/chapter12.cfg:
load credits

In portal/custom/credits:
materials\vgui\chapters\chapter12.vtf
This one's only for the chapter preview.

Modifying the chapter title in the menu:
Line 20 in portal/resource/portal_english.txt:
"Portal_Chapter12_Title" "Credits"
Bonus Use Case 2: Advanced Key Remapping
This use case is for those who want to add more functions to your already existing keys.
To better explain what I mean, take a numeric keypad on a keyboard for example.
You might have the number keys (or rather, their alternate function) bound to equipping weapons based on their category, but suppose you want to swap said keys' function for another one.
While you can utilize the bind command to do this, it can get messy; something like this:
bindtoggle kp_end mat_fullbright; bindtoggle kp_downarrow cl_showfps; ...
can inevitably become a bit difficult to modify:
bind <function-swap-key> "bind kp_end ...
Hence, config files can decrease the complexity and tediousness of writing key remapper scripts.
Reference the Bind page on VDC if you want to learn more.
Conclusion
Obviously, config files can be used for a lot more if you know the Source Engine in-depth.
The Valve Developer Community is an excellent resource for it.
Anyways, hopefully you found this guide as somewhat useful, and thanks for reading this far.