PDA

View Full Version : How To Write TF2 Scripts


TrashedDT
December 16th, 2007, 04:57 PM
I got to tweaking around with some TF2 scripts and thought I'd share my successes with you. Also for you noobs I'm including a basic "how to" to get you started. As I test out new scripts that WORK I'll share them in this thread. Let me know if you find bugs and I'll try to fix them. BTW I know some people have mixed feelings about scripts, this is not the place to discuss it. Please kindly make a new thread. Thank you.

So first things first. Navigate to:

..\Program Files\Steam\Steamapps\<username>\team fortress 2\tf\cfg

ALL SCRIPTS MUST BE SAVED TO THIS FOLDER

Reference for keyboard key names
In this folder you will find config.cfg, which is the storage file for all of your keyboard control settings. It's a great reference for later if you don't know the "steam" name for a key on the keyboard. e.g. "; = SEMICOLON" You can just open up your options, map a key you want to know the name of, hit apply, and then check the config.cfg for the name associated with the key.

The Autoloading Config File
How do you get your scripts to autoload in the game? You use the autoexec.cfg. If you have vanilla TF2 (aka freshly installed) then you will not have the autoexec.cfg. So first you want to create it. Go to the "cfg" folder mentioned above and create a new text file and rename it autoexec.cfg. Now any scripts you write to this file will be automatically loaded when you play TF2. Later I'll discuss how you can create more cfg files to customize scripts for each class.

Soldier Uber Rocket Jump
To get you started lets add a script that makes it simple to make a perfect rocket jump as a soldier. In the autoexec.cfg file add the following code

//Rocket Jump
alias +rocketjump "+jump; +duck; wait; +attack"
alias -rocketjump "-jump; -attack; wait; wait; wait; -duck"
bind "f" "+rocketjump"

Now I'm going to break up the different parts of this code to give you an idea of what's going on.
Alias - this is a way of creating your own name for a task or group of tasks. It works as so: alias <createname> <task> *Notice: No Quotes*
alias "<createname>" "<task>; <task>; ....;<task>"
The quotes ' " ' are actual parts of the code. They can always be included, but are only necessary if your writing tasks with spaces inbetween them. So in the first line this script creates an alias called +rocketjump associated to the four commands: +jump, +duck, wait, and +attack. Because there is more than one command quotes must be used to include them in the alias. The plus '+' is used to signify that the action is started, like pressing and holding a key down on the keyboard. The minus '-' is used to signify that the action is stopped, like releasing your finger from a key. If you use an alias to define a custom action like +rocketjump you must define the -rocketjump as well. If you don't the game will never release the rocketjump command and you'll find yourself stuck in a loop. If you are attaching more than one task to an alias each task must be separated by a semicolon ';'.

The last line binds the alias to a key on the keyboard. In this case the quotes are optional, but it's always a good idea to include them just in case.

Can I Have A Script For Each Class?
You most certainly can! And it actually is better if you have a lot of scripts. Let's say you want to bind the bat of the scout to the right click of the mouse, but you don't want to interfere with the exploding of the demoman's sticky bombs. Separate scripts loaded at time of respawn resolve this problem.

To start off you need to create a "cfg" file for each class. Go to the cfg folder and create 9 text files. Rename each one to represent each class and use the extension ".cfg". The names I used were as follows: Scout.cfg, Soldier.cfg, Pyro.cfg, Demo.cfg, Heavy.cfg, Engineer.cfg, Medic.cfg, Sniper.cfg, and Spy.cfg. Now to use the following scripts in game open up your autoexec.cfg and copy and paste the following code into it:
//Class Config Switcher with Config Loader
alias scout "play vo\scout_battlecry01; join_class scout;exec scout.cfg; developer 1; echo ::Scout Config Loaded::; wait 200; developer 0"
alias soldier "play vo\soldier_battlecry01; join_class soldier; exec soldier.cfg; developer 1; echo ::Soldier Config Loaded::; wait 200; developer 0"
alias pyro "play vo\pyro_battlecry01; join_class pyro; exec pyro.cfg; developer 1; echo ::Pyro Config Loaded::; wait 200; developer 0"
alias demo "play vo\demoman_battlecry01; join_class demoman; exec demo.cfg; developer 1; echo :Demoman Config Loaded::; wait 200; developer 0"
alias heavy "play vo\heavy_battlecry01; join_class heavyweapons; exec heavy.cfg; developer 1; echo ::Heavy Config Loaded::; wait 200; developer 0"
alias engineer "play vo\engineer_battlecry01; join_class engineer; exec engineer.cfg; developer 1; echo ::Engineer Config Loaded::; wait 200; developer 0"
alias medic "play vo\medic_battlecry01; join_class medic; exec medic.cfg; developer 1; echo ::Medic Config Loaded::; wait 200; developer 0"
alias sniper "play vo\sniper_battlecry01; join_class sniper; exec sniper.cfg; developer 1; echo ::Sniper Config Loaded::; wait 200; developer 0"
alias spy "play vo\spy_battlecry01; join_class spy; exec spy.cfg; developer 1; echo ::Spy Config Loaded::; wait 200; developer 0"

bind KP_END "scout"
bind KP_DOWNARROW "soldier"
bind KP_PGDN "pyro"
bind KP_LEFTARROW "demo"
bind KP_5 "heavy"
bind KP_RIGHTARROW "engineer"
bind KP_HOME "medic"
bind KP_UPARROW "sniper"
bind KP_PGUP "spy"
This bit of code will bind each config file to the numeric keypad from 1-9 in the order of the file names I listed above in bold. *Note* This can be problematic for laptop users as often the numeric keypad is embedded among the letter keys and toggled with a function key. If this is the case simple replace the bindings to meet your needs. Again a great way to find the name of the binds is to assign them in the games option control menu and then look in the config.cfg file for the resulting name listed.

Now you can place whatever scripts you want into each class config file. I'm including my current config files in a zip file. I've been playing around with these for fun for the last week. Use at your own risk, they are a little buggy. If you have questions about a certain line I will answer in the subsequent posts, but not in this initial post.

Including commands in your scripts
Any command you type in the console can be executed in a script. Simply put the command on it's own line or separate commands with ';'. Every line and group separated by a semicolon in the above code is actually a command that you can type in the console.

How do I prevent my scout scripts from messing up my sniper scripts?
You may want to bind the right click for melee as scout and zoom as a sniper, but you find that some of your scripts get screwed up when you change class. There is hope! I call it default.cfg!!! Basically its just a copy of your config.cfg before binding anything special to it with extra lines of code removed. Here is how you make it.
1. Open TF2 and go to options.
2. Bind all your keys for general gameplay like you would if you had no scripts.
3. Save the settings and exit TF2.
4. Now go to your cfg folder and open config.cfg.
5. At the same time in the cfg folder create a new file called default.cfg.
6. Now, in the config.cfg file, highlight every line that starts with the word "bind". There is about 68 lines of binding.
7. Paste these into your default.cfg file and save it.

That's all there is to making the file. Now we need to implement it.

8. Open each of your class config files and add to the top of each the line:
exec default.cfg
exec autoexec.cfg //This can also be added depending on how your scripts
//are set up.

nero84
December 17th, 2007, 04:45 PM
heyy - can you make some scripts for spy, sniper, soldier, scout and pyro?

TrashedDT
December 19th, 2007, 04:51 PM
heyy - can you make some scripts for spy, sniper, soldier, scout and pyro?

You can make an individual config (.cfg) file for each class and run it when you spawn as this class. I'll be sharing those scripts also as I have time. But to get started just make a .cfg file named after each class.
ex. scout.cfg, soldier.cfg, demo.cfg, heavy.cfg, pyro.cfg, etc...

Put in each file the scripts that pertain to the specified class and when you want to load that script follow these steps:
1. bring up the console
2. type exec followed by the name of the file (no need to include the extension).
Ex. If you want to load your soldier script, type:
exec soldier

I will be including in the "how to" a script that binds the loading of each script to the numeric keypad. Stay tuned.

coll9947
January 17th, 2008, 12:39 PM
Does the always double jump script mess up the scout's double jump? If so, can I put auto-double-jump on a toggle button in my autoexec.cfg, or should I just put the auto-double-jump script into each class's .cfg except for scout? Currently I have the double jump script in my autoexec.cfg.

TrashedDT
January 17th, 2008, 03:07 PM
Does the always double jump script mess up the scout's double jump? If so, can I put auto-double-jump on a toggle button in my autoexec.cfg, or should I just put the auto-double-jump script into each class's .cfg except for scout? Currently I have the double jump script in my autoexec.cfg.

To answer your question, yes it does affect the double jump but it affects all the classes at the same time. The second jump of the scout is not actually affected. You just double jump as usual. What is affected is when you land. If you are still holding down the jump button when you land you will land in a squat and unable to jump again until you release the jump button and press it again.

[BDB] Puppy Stomper
January 17th, 2008, 03:33 PM
How can you disable a script once you've loaded it? For instance, if I use the rocket jump script on a soldier, it wants to do it for every other class I switch to. I believe the sniper script screws up a lot of things for me, since it seems to hold down the fire button and even use the right-click. If I'm any other class, it will rapid fire, and it will auto-detonate any stickies or auto-build a building, giving me no time to chose the right location or rotate it first.

I've given up, out of frustration, and only use the rocket jump script now, if even that.

HELP THE STOMPER OF PUPPIES!

TrashedDT
January 17th, 2008, 03:39 PM
Are you using different script files like I suggested in the first post? Read under the heading "Can I have a script for each class?" If any part of it's unclear ask away.

Worry
January 17th, 2008, 04:41 PM
This is a great collection of information Trashed. Thanks for that.

I'll give these a try on the new system. Problem with my current set up is I use the right mouse for jumping. Been doing it for so many years it would be hard to stop, but some of those scripts look like that would work best as you've set them (melee attack using right mouse).
Might be time to learn a whole new bind configuration.

[BDB] Puppy Stomper
January 17th, 2008, 04:44 PM
Yea, I have a different one for each class. But it seems that when I load more than one, they scripts just stack.

Mephisto
January 17th, 2008, 04:54 PM
i'm afraid to try the sniper unclick&shoot script cause i hear it messes up all your other regular shooting (and i play medic/demo most of the time...)

i think i might bind it to my mouse's 2nd thumb button...

TrashedDT
January 17th, 2008, 07:19 PM
Hey guys I have a solution for those problems, actually have had a solution, but forgot to add it to the tutorial. Unfortunately I don't have time to write it up tonight as I'm trying to finish C program homework before the scrim tonight. I'll have the solution up tomorrow tho.

TrashedDT
January 18th, 2008, 04:47 PM
Script tutorial has been updated. Let me know if you have any questions.

[IV]Paladyne
September 5th, 2008, 12:14 AM
Wow, old thread. :)

I used to be a big scriptweenie during my TFC/CS days, so I'll share a couple tricks I used to use. I haven't looked at Trashed's tutorial, so apologies if I duplicate anything. :P



First off, just bind a button to a bunch of -commands, the ones that STOP any continuous action like attacking, jumping, ducking, or moving. Something like this:

Bind "pause" "-attack; -attack2; -jump; -duck; -forward"

This is useful if you get stuck somehow and can't move (or stop moving), duck, jump, or shoot. Just hit the pause button (or whatever key you choose to bind it to) and whatever obnoxious behavior is holding you back gets cleared up immediately.



Second, you can figure out what keys you're going to be playing around with in your class-based configs, and rebind them to your default in all the other class configs. For example, suppose you're using Trashed's Minigun Toggle Spin script for the HWG (bound to the ALT key, for example), and you're getting annoyed because when you play sniper it'll keep zooming and unzooming when you hit ALT. In this case, just put a line in the sniper cfg to rebind ALT to whatever it is it normally does, and that should clear up most problems. Do the same with the other 7 classes, and you're good. Then, just use the reset button above to take care of any lingering trouble.



Third, you can create a new cfg file which is basically a duplicate of your general config file. Let's call it "reset.cfg" for now. Just type "exec reset" into the console and any changes that you may have made via the class specific config files get wiped out. I guess the only catch is that you have to keep it up to date, so if you decide to permanently change a key binding, change it in reset.cfg too to reflect that. Not that big a deal, really. ;)

From there, you can get increasingly crazy. When I was playing TFC and CS, I had my entire keyboard mapped out TWICE over - every key had some sort of command attached to it (mostly text macros for common sayings), and then a second command which was triggered by a config file that rebound every key to something else. I don't expect anybody to get that crazy, but it's useful to know it's at least possible.



It sounds much more complicated than it really is. All it takes is learning about a dozen console commands and opening up the cfg files in the cfg folder with any document editor. No sweat. ;)