jsut's scripts

Creating a Keybind

One common thing that scripters have to do is create keybinds. This is a relatively simple process in Tribes 2. There are two things that need to be done to create a working keybind. You have to write the function that is going to be called on keypress, and you have to add you function into the list of available keybinds.

A keybind function should have this prototype:

function function_name(%val) {}
The variable %val indicates whether this they key is being pressed or released. The function is called twice for each keypress, once on the way down, and once on the way up.

The list of functions that are available to be used as keybind is stored within the game using two arrays, and a counter. The following code snippit will add a function to the list:

$RemapName[$RemapCount]="Your Keybind's Display Name";
$RemapCmd[$RemapCount]="yourFunctionName";
$RemapCount++;
$RemapName is an array of the display names, $RemapCmd is the functions, and $RemapCount is the counter.

So lets look at a trivial script that will add a keybind. The following script adds a keybind that will turn on trace when you hit a key, and turn it off when you hit that key again.

$jsutTrace = false;

function jsutTrace(%val) {
    if (!%val)
        return;

    trace(!$jsutTrace);
    $jsutTrace = !$jsutTrace;
}

package jsuttrace {

    function setUpFavPrefs(){
        $RemapName[$RemapCount]="Toggle Trace";
        $RemapCmd[$RemapCount]="jsutTrace";
        $RemapCount++;

        return parent::setUpFavPrefs();
    }
}

activatePackage("jsuttrace");

We'll go over it line by line. The first line sets a global variable $jsutTrace to the value of false. This variable is going to be used to indicate whether trace is on or off. Line three is the declaration of the keybind function. Notice it uses the prototype mentioned above. if (!%val) says only execute the next line if they key is on it's way up. return ends the function if it's executed. This if statement keeps anything from happening on key up. The next two lines turn trace on or off, based on it's current state, and update the current state.

The rest of the script is the package. Functions that appear in here are sub-classing (sort of) functions that are provided by Tribes2. setUpFavPrefs() is a function in tribes two that gets executed once, every time you startup your game. It gets executed after the default keybind stuff is setup, so it's a great function to use to add your keybinds. The three following lines add the keybind as discussed earlier. The name that will show in the list is "Toggle Trace", and the function that will get called when you press that key will be jsutTrace. parent::setUpFavPrefs() runs the original version of that function provided by Tribes2, or possibly other overwritten versions from other scripts. If you don't call your parent::, you stand the chance of breaking Tribes2 significantly (try not calling DispatchLaunchMode sometime). The final line of script activates the package.

To see this script by itself click here