Showing posts with label team fortress 2. Show all posts
Showing posts with label team fortress 2. Show all posts

Wednesday, July 28, 2010

Simple If Then Else Scripting in TF2

Source: https://cagricelebi.com/blog/simple-if-then-else-scripting-in-tf2/

Not only Team Fortress 2, all Half-Life 2 games actually. Yeap, it's easy. A friend of mine asked me something like this
- I want to make the "g" key make the fov 90 on a press and 75 on the other press. It should toggle.

The pseudo code is like this at the first sight,
if (on keypress g) {
    if (fov==90) {
        fov=130;
        break;
    }
    if (fov==130) {
        fov=90;
        break;
    }
}


But as far as I know, there is no if then else logic on that platform. There is only bindings or aliases for the time being. So the algorithm is a bit more complicated, like calling functions with a global variable,

method1{
fov=90;
on keypress g: call method2;
}

method2{
fov=130;
on keypress g: call method1;
}

on keypress g: call method1;


This works like changing the binding of keypress g, which calls a method. The methods do 2 jobs; 1st changing the fov setting, 2nd changing which method to be called on keypress g.

The final code is like this.
alias ws1 "fov_desired 75; bind g ws2"
alias ws2 "fov_desired 90; bind g ws1"
bind g ws1


Here, each time you press g, the fov is toggled between 90 and 75.

You may code different if/else statements like this approach. The algorithm is a bit complex but that is the only choice with the current system.

I hope it works.