First, why write scripts and what do they do for you?
I've added scripts to my basic configuration for three main reasons:
After reading through this basic explanation of script writing you should be comfortable with writing your own basic scripts. Visit The Bind:Arena to see some examples of some really cool scripts and Commander Keen's Quake 3 Arena Console Page for all of the different console commands and variables you might need while writing your scripts.
Scripts have to be saved in a separate .cfg file from your q3config.cfg. The reason is that Quake overwrites the q3config file every time you start a game or change settings and therefore will delete your scripts and comments. I put all of my scripts into an autoexec.cfg. If you don't know how to put one together, visit the Tweaking Your Controls:Making an Autoexec section for a quick lesson.
As you write scripts, use //comments to help annotate what your script does. It will help better organize scripts in your configuration and if you want to give your scripts to other people, it makes it pretty clear what it does. Quake will not execute anything that follows a pair of double-backslashes (//).
You can also use the Echo command (more on this later) to give yourself in-game messages when a script has been activated. We'll discuss this later, but you can go to some of the examples section and look at the zoom toggle and the demo recording script to see what in-game messages I give myself when the script is activated.
The syntax is:
The syntax is:
You can string a whole bunch of commands together to be executed all at once. This bind for example, will take a remove all the clutter from your screen (e.g., icons, guns, etc) , take a screenshot, and then return your HUD to the way it was.
The process is:
Here's what it looks like when it's all put together:
Try this out and play around a little. You'll be writing your own scripts in no time. Go to the Examples section to see a couple I've written.
The syntax is:
Look through your configuration for variables that take either a 0 or 1 value. Those are the commands that can be set up on a toggle.
The syntax is:
Download a copy of my Q3:A autoexec.cfg if you want to see how my bindings and scripts are set up.
Script Organization
Example of //
Basic Binds
Binding a key to perform a single action is the easiest place to start. We'll start by binding the x-key to select the railgun (weapon 7).
bind [key] "[command]"
Since we want to bind x to the railgun, we will substitute x for [key] and weapon 7 for [command]. This should give us:
bind x "weapon 7"
That was simple enough, let's move on.
Multiple Binds
We can also bind a key to execute multiple commands. Remember that Quake executes commands in the order you've specified so we have to be a little careful. For example, we are going to bind a key to select the best choice between the rocket launcher (weapon 5) and the shotgun (weapon 3).
bind [key] "[command 1]; [command 2]"
You have to separate the commands with a semicolon (;) so that Quake recognizes where each command starts and stops. We're going to use the x-key again for [key], weapon 3 for [command 1], and weapon 5 for [command 2]. This should give us:
bind x "weapon 3; weapon 5"
When you hit x, the bind selects the first weapon (e.g., weapon 3) then immediately selects the second weapon (e.g., weapon 5). In effect, if you have both weapons in your inventory, it selects the last one in the sequence. If you only have one of the weapons, it will only find and therefore activate that weapon. This is why we want to make sure we write weapon 3 first since given the choice between the two, we always want to end up with the rocket launcher.
bind F11 "cg_draw2d 0;cg_drawgun 0; wait; wait; wait; screenshot; toggle cg_draw2d; toggle cg_drawgun"
Recursive Scripts
Everything is clear so far, right? Now, instead of only selecting the best weapon, what if we want to toggle back and forth between two weapons like the railgun and the rocket launcher or make it possible to zoom to 3x or 6x magnification? This requires a script, which I will call the recursive script. By recursive, I mean that you can cycle through the script an infinite number of times. The intent is to execute one string of commands when you hit the bound key, then re-bind the key to execute new string of commands the next time you use the key.
Step 1 - Name your script and add a short description of what it does
Add your name and description after a comment (// - double-backslash) so that Quake doesn't try to recognize it as a command. Assigning a good descriptive name will help you find your script later, and if you decide to share it with others, helps them understand what they're getting.
// 3x - 6x zoom toggle
Step 2 - Write commands for each 'step' in the script
set [scriptline(n)] "[command]; set [nextscriptline] vstr [scriptline(n+1)]; echo [comment]"
Here's an example for a 3x - 6x zoom toggle:
set zoomtoggle1 "cg_zoomfov 15; sensitivity 25; set nextzoomtoggle vstr zoomtoggle2; echo 6x magnification"
set zoomtoggle2 "cg_zoomfov 30; sensitivity 17; set nextzoomtoggle vstr zoomtoggle1; echo 3x magnification"
Step 3 - Assign dynamic variable to first 'step' in sequence
After you have finished writing the basic set of commands, use
set [nextscriptline] vstr [scriptline(n+1)]
This tells the computer that [nextscriptline] should start by executing [scriptline(n+1)]. Notice however, that every time you execute your script, the value attached to [nextscriptline] changes. Here's an example for a 3x - 6x zoom toggle:
set nextzoomtoggle "vstr zoomtoggle1"
Step 4 - Bind key to execute dynamic variable
The last step is to bind a key to execute your new script. We learned the syntax in the first step, Simple Binds. The final line in the zoom toggle looks like:
Here's an example for a 3x - 6x zoom toggle:
bind b "vstr nextzoomtoggle"
All this does is bind the key to execute the string of commands associated with [nextscriptline].
Toggles
Any command that can be set to either on or off, can be set up on a toggle. For example, you can set Always Run to either on (1) or off (0), therefore I can create a toggle that lets me switch between always on or always off. This has a couple of advantages:
bind [key] "toggle [command]"
Here are a couple of examples that I use in my autoexec.cfg
bind SHIFT "toggle cl_run"
bind F2 "toggle r_fastsky" //Speed toggle
bind F3 "toggle cg_drawFPS" //Displays Frames per Second
bind F4 "toggle cg_drawTimer" //Displays timer
bind F5 "toggle cg_thirdperson" //Shows model in thirdperson view
bind F6 "toggle cg_drawgun" //Shows gun
Other Executables
I use these to clean up my autoexec.cfg. Basically, I have a file containing all of my favorite servers, one with all of my communications binds and scripts, and a third with my demo recording/handling binds. Having them in a separate file lets me share them with teammates, team comm binds for example, or pass along my favorite servers without having to send the entire autoexec. The other reason for this is config organization. If your configuration to too large, Quake 3 will have problems executing the whole thing. Seperate configs set up as executables enable you to reduce the overall size of your primary autoexec.
exec [filename.cfg]
Here are a couple of examples that I use in my autoexec.cfg
You can also combine this with a bind command, for example to exec a new set of key bindings for a mod. I use this technique with any mod that requires an extensive set of new key binds such as Urban Terror, Titanium, or Pwrweapons. This way, when I enter a game, I simple hit the bound key, and I'm ready to play. Here's an example.
bind 7 "exec terror.cfg"