Toggle network cvars script for Team Fortress 2

Gaming Programming

While playing Team Fortress 2, I’ve found that some servers are set up for higher bandwidth usage allowing for a smoother game. To really take advantage of that, typically a few client side cvars need to be changed. However, many servers also run the default settings on their server and you’ll need to set the cvars back.

Conveniently, the source engine allows TF2 players the ability to create client side scripts / configs that will automate these changes. I have taken the liberty of creating a config that will let you bind a key to a “nextrate” command which will toggle the cvars between their High and Low rate settings.

First off, I am only changing the values for two CVARS, there may be more and they would be simple to add, but for the sake of this discussion I will be changing “rate” and “cl_updaterate” cvars.

The Low settings are:

rate: 30000
cl_updaterate: 20

The High settings are:

rate: 60000
cl_updaterate: 100

All that really needs to be done is, when connecting to a High rate server, in the console type:

rate 60000;cl_updaterate 100;

And when connecting to a Low rate server, in the console type:

rate 30000;cl_updaterate 20;

but who wants to type that everytime…. So, without further ado, enter the script:

alias ShowHighRateMsg echo rate set to 60000, cl_updaterate set to 100
alias ShowLowRateMsg echo rate set to 30000, cl_updaterate set to 20
alias SetHighrate "rate 60000; cl_updaterate 100;alias nextrate SetLowrate;ShowHighRateMsg;"
alias SetLowrate "rate 30000; cl_updaterate 20;alias nextrate SetHighrate;ShowLowRateMsg"
alias nextrate SetLowRate
nextrate

This uses the alias command which lets you set chains of console commands together and reference later by the name you give the alias.

Above five aliases are created:

1. ShowHighRateMsg – Simply echoes the text we want the user to see in the console after executing the nextrate command to set the High Rate settings

2. ShowLowRateMsg – Simply echoes the text we want the user to see in the console after executing the nextrate command to set the Low Rate settings

3. SetHighRate – Actually sets the cvars for High Rate server use. It also sets the alias for “nextrate” to SetLowRate. This is what implements the Toggle functionality. The user keeps calling “nextrate” and nextrate is pointed to different aliases every time it is called. It also displays the setting changes it is making in the console for the user to see if they bring up the console.

4. SetLowRate – Actually sets the cvars for Low Rate server use. It also sets the alias for “nextrate” to SetHighRate. This is what implements the Toggle functionality. The user keeps calling “nextrate” and nextrate is pointed to different aliases every time it is called. It also displays the setting changes it is making in the console for the user to see if they bring up the console.

5. nextrate – This declares the nextrate alias and by default points it to SetLowRate. This is the alias that you bind to a key. To bind it, it must exist first which is why it is by default pointed to SetHighRate (it assumes you play on more Low Rate servers then High Rate, besides it’s the default value from Valve so, hard to go wrong.)

The final line of the script is a call to nextrate, which as mentioned set to SetLowRate, so this actually execuste the nextrate alias and sets the cvars to Low Rate server use.

You could then bind a key to nextrate and execute it during the game. For example, in the console type:

bind m nextrate

Which will bind your M key to the nextrate alias. Pressing M at this point will execute the nextrate alias whenever it is pressed.

At this point, were you to paste the above script line by line into the console window of TF2, you could call nextrate over and over again and see it change. Well again, who’s going to paste all that in every time?

So you have 2 options at this point. Open up the AutoExec.cfg file in your TF2 cfg directory:

C:program Files (x86)steamsteamapps<YOUR STEAM NAME>team fortress 2tfcfg

and paste the entire script in there. You could also bind it to the key of your choice by adding the bind command from earlier here.

The other option, and I think the cleanest, is to create a new text file called “ServerRateToggle.cfg” in the cfg directory (same place as the AutoExec.cfg) and paste the script into the new file. Then open the AutoExec.cfg and add the following line at the bottom:

exec ServerRateToggle.cfg

now, whenever the AutoExec.cfg file is parsed by TF2 (in this case, every time you start the game) it will execute the Rate toggle script which will reset the values to their defaults. You could also add the bind command in the autoexec.cfg after the call to exec ServerRateToggle and it will be bound forever more for your convenient use.

Here is a link to the ServerRateToggle.cfg to avoid copy and paste errors 🙂

Enjoy, and let me know if you come up with anything better or more commands that need to be added to the cvars I’m currently setting!