Scripting
“Let's settle this like gentlemen!” This page has been marked for an open review. Please see the discussion pages for additional information, your opinion is welcome. |
Scripting is the use of configuration files to create new keybinds and aliases automating complex behaviors and console commands.
- Keybinds connect a key to a console command, such as choosing a weapon slot or saying prewritten text in chat.
- Aliases build new commands by executing a series of console commands whenever they are used. An alias might define jumping and crouching as a single jump-crouch command, which can then be bound to a key.
These scripts range in complexity from simple lists of commands to self-referential loops and nested aliases that redefine one another. With enough time and effort, you can use the tools to create almost any behavior available to the client at the press of a single key.
Contents
Basic Scripting Files
Team Fortress 2 automatically reads several game files and executes any keybinds and aliases inside. Here is the list of those files:
- config.cfg - This is the default config file, which the game edits whenever you use the in-game menus to bind keys or change video settings. While editing the file is safe, it's simpler to leave it alone.
- autoexec.cfg - This is the default config file for executing custom keybinds and aliases. You may put your keybinds directly into this file, but it is often easier to create separate .cfg files for different scripts, which you then execute inside this file.
- <class>.cfg - Each class has a config file which automatically runs whenever you switch to that class. You may put your keybinds directly into this file, but it is often easier to create separate .cfg files for different scripts, which you then execute inside these files.
Syntax
Here is a list of syntactic rules:
- For any keybind or alias to work, you need to follow this format:
- command <argument1> <argument2>
- command is the kind of scripting you want. You have three choices:
- bind - Used to bind commands to a particular key.
- alias - Used to execute commands when the alias is executed.
- exec - Used to execute commands automatically when a configuration file is loaded.
- <argument1> is either the key being bound or the name of the alias being created.
- <argument2> is either the command being bound or the commands executed by the alias.
- To run multiple commands, you must put the entire argument in quotation marks and separate the commands with semicolons, like so:
- alias "spraynpray" "+attack; wait 500; -attack"
- bind "mouse3" "spraynpray"
- When executed, the alias starts firing the player's weapon, continues for 500 physics frames or roughly five seconds, and then stops firing. The alias is keybound to execute when you press the middle mouse button.
Basic Commands
Most commands accept a single argument, which is whatever value you want to execute.
bind "q" "sv_alltalk 1"
A handful of important commands accept multiple arguments. Here are some examples:
- disguise X Y - This disguises the Spy. X is the class numbered 1 through 9 and Y is the team numbered 1 or 2.
- voicemenu X Y - This executes a voice command. X is the number of the primary menu. Y is the command position in that menu.
Example: "voicemenu 1 1" is the command for Spy, so:
bind "t" "voicemenu 1 1"
This command will make your character yell Spy whenever you hit the key.
Incrementvar
This command cycles through a range of specified values whenever it executes and applies those values to another command. This has the form of:
- command "<argument1>" "incrementvar <argument2> X Y Z"
- X and Y represent the range of values you want to execute with <argument2>.
- Z is the size of the step you want to make while you cycle through the range.
If you wanted a value range of 1 through 10 and you wanted to increment your value by 2 each time the command was executed, you'd make X Y Z into 1 10 2. Then each time it executes, <argument2> will execute with an incrementing value of 2, 4, 6, 8, 10, 2, 4, 6, 8, 10... and so forth.
Here is a real example of incrementvar in action:
- bind "n" "incrementvar net_graphpos 1 3 1"
This binds the key "n" to cycle to the next position in the 3 available net_graph positions.
The + and - commands
These commands have two states. While the bound key is pressed, the +command executes and continues to execute. When the key is released, the -command executes and continues to execute.
- bind "c" "+duck"
This +command is bound to "c" so whenever the key is pressed, the player ducks. Whenever the key is released, +duck is swapped to -duck and the player stops ducking.
In general, if a command or alias is bound to a key, a + tells the game to run the command as long as the key is held. Similarly, if a command or alias is bound to a key, a - tells the game to run the command when the key is released.
Wait Command
If you want the game to delay for a period of time during an alias, or if you want avoid client crashes caused by executing commands too quickly, use the wait command.
Wait tells the game to delay the next command in the script by one physics frame, something like a full turn in the TF2 rules. It accepts values such as "wait 50" which tells the game to wait fifty frames before the alias continues. "wait 100" is roughly a second on a 100 FPS server.
Some servers disable the wait command. While playing on them, many scripts will not function and some scripts will cause your game to crash.
Aliases
An alias is like a variable that stores a series of commands which can be executed as a group. A simple alias will take the form:
alias "aliasname" "command1; command2; command3"
You can then execute the commands through a bound key:
bind "key" "aliasname"
Whenever the key is pressed, command1, command2, and command3 are executed in order.
Complex Aliases
Aliases can execute any command including other aliases. Furthermore, an alias can execute an alias in order to redefine that alias's commands. This allows you to create loops. Here is a very simple example:
bind "key" namescroll
alias namescroll "firstname"
alias firstname "setinfo name "First_name"; alias namescroll secondname"
alias secondname "setinfo name "Second_name"; alias namescroll thirdname"
alias thirdname "setinfo name "Third-name"; alias namescroll firstname"
This is difficult to read, so break it down.
- The first line binds "key" to an alias "namescroll" which we have yet to define.
- The second line creates the alias "namescroll" which executes the command "firstname"
- The third line defines the "firstname" alias as two commands:
- The first command sets your in-game name to "First_name"
- The second command redefines the alias "namescroll" as an alias for executing the alias "secondname"
- The fourth line defines "secondname" in the same manner. A different in-game name is set, and then "namescroll" is redefined to another alias "thirdname"
- The fifth line defines "thirdname" just like the two before it, but with a twist. It redefines the "namescroll" alias as executing the "firstname" alias.
This completes a loop. Each press of the key executes a command and redefines the alias bound to the key, so that when it is next pressed it will execute a different command. If there were no loop, then the alias "namescroll" wouldn't always have another alias to execute with each keypress.
Noteworthy scripts
There are several notable scripts that have affected gameplay. Some of these are patched out. Others remain functional.
Patched Scripts
Pistol Script
Originally, the Pistol could be fired almost as fast as the human hand could press a key. Realizing this, some people created script sthat made the Pistol fire as rapidly as possible as long as you held down the fire button. This not only allowed the Scout and Engineer to fire faster than most people could manage, but it was easier to aim because you weren't mashing any keys.
Valve eventually leveled the playing field. They patched the Pistol to have a lower max firing rate, which was reached by default whenever a player held down the attack key. The Pistol script was no longer necessary.
Targe Turn Script
Originally, the keyboard commands to turn your viewpoint left and right were not treated the same as the mouse commands. The Chargin' Targe was the first weapon to create a restriction on how fast you could turn your viewpoint, so Valve put limits on mouse turnspeed but forgot to apply the same restrictions to the rarely used keyboard turns.
Players made scripts that rebound keys whenever a player charged, allowing them to turn at full speed. The most sophisticated examples temporarily rebound the mouse x-axis inputs to the keyboard turn commands, then unbound them when the player attacked at the end of the charge.
Valve eventually capped the keyboard turn commands like the mouse commands. The Turn scripts were made useless.
Air Crouching Script
Originally, you could crouch as many times as you liked in a single jump. By binding a script to crouch and uncrouch as rapidly as possible, you could exploit the engine and make it difficult to properly register shots on jumping targets.
Valve patched it so you could only crouch twice in a single jump.
Current Scripts
Weapon Hiding Scripts
There are a large number of scripts which toggle the weapon display option depending on which weapon slot is selected for a class. Players often make their guns invisible to clear up the screen, but make items like melee weapons, PDAs, watches and so forth visible because these models indicate important information. For example, the Spy relies heavily on his visible model to determine when he is cloaked, when his cloak is disturbed, and when the Dead Ringer activates. Other classes use the melee animations to judge when their attacks will hit.
Sentry Jumping Scripts
Since the Engineer update, Engineers have been able to pack up and carry their Sentry Guns. Simultaneously, the Wrangler allowed Engineers to Sentry jump with their Sentry rockets. With extremely fast inputs, it was discovered that a player could Sentry jump and successfully pack up their Sentry before they were launched away.
While a human can repeat this feat, it's difficult and sometimes inconsistent. Some players made scripts which could reliably execute the commands in the right order at the right speed. They can make a Sentry jump while carrying their Sentry every time.
Sensitivity and Control Scripts
Some players prefer different mouse sensitivities and control schemes for some classes. These scripts alter their control schemes and mouse settings on a per class or even per weapon basis.
External Links
- Steam Users' Forums - TF2 Scripting Tutorial. This covers the basics of scripting, using numerous useful, approachable examples.
- TF2 scripting tutorial - Warrior Nation Network. This is a much more advanced article, which requires pre-requisite knowledge of the basic scripting syntax.
- FPSBanana.com - TF2 scripts. Hundreds of scripting examples for TF2.
- Bindable keys covers all keys which are bindable, and the names relevant to scripting.