Basic tutorial 8
Creating a toggle.
Like we discussed in the previous tutorial, a toggle is nothing more than a loop. This is best explained by using an example. Let's say I still use *NoobZor as my gaming name, but I have also decided to join a clan, which uses the [A] tag. So I also sometimes want to play with [A]NoobZor.
Now let's write our first real script. We want to be able to switch between these 2 names using one key. Again let's just use x in this example. We start by using 2 variables to store these 2 names in:
set name1 "name ^1*^4N^1oob^4Z^1or"
set name2 "name ^1[^4A^1]^4N^1oob^4Z^1or"
Now we have 2 situations we can switch between: variable name1 and variable name2.
What we want is when we press x the first time, it has to execute name1. The second time we press x, it should execute name2. The third time it has to execute name1 again, and so on. If we want to execute name1 the first time, you probably would think that binding x to variable name1 would do:
bind x "vstr name1"
Unfortunately this is not true, cause with this bind name2 can never be executed. So we need the the keybind to switch between the 2 variables and for that we need 1 more variable. Let's call this variable nameChanger.
We will bind x to this new variable nameChanger and want this new variable nameChanger to alternate between name1 and name2. This is done by the following construction:
bind x "vstr nameChanger"
set name1 "name ^1*^4N^1oob^4Z^1or; set nameChanger vstr name2"
set name2 "name ^1[^4A^1]^4N^1oob^4Z^1or; set nameChanger vstr name1"
set nameChanger "vstr name1"
If you look at this construction you will see that when name1 is executed, the variable nameChanger is then binded to the variable name2:
set name1 "name ^1*^4N^1oob^4Z^1or; set nameChanger vstr name2"
This means the next time x is pressed and nameChanger is executed, it will no longer execute name1, but will now execute name2. The same thing goes for variable name2. When this variable is executed, the variable nameChanger is binded to name1 again:
set name2 "name ^1[^4A^1]^4N^1oob^4Z^1or; set nameChanger vstr name1"
Now as you can see I added 1 more line of code to the script:
set nameChanger "vstr name1"
This line can be seen as an initial value. Without this line the script would not know with which variable it should start the first time we press x. With this line we tell the script, that it should execute name1 the first time we press x.
So if you look at the construction of a toggle in general, it should always look like this:
bind key "vstr mainVariable"
set variable1 "command1; command2; ...; set mainVariable vstr variable2"
set variable2 "command1; command2; ...; set mainVariable vstr variable1"
set mainVariable "vstr variable1"
Of course you can work with more variables than 2. With 3 variables it would look like this:
bind key "vstr mainVariable"
set variable1 "command1; command2; ...; set mainVariable vstr variable2"
set variable2 "command1; command2; ...; set mainVariable vstr variable2"
set variable3 "command1; command2; ...; set mainVariable vstr variable3"
set mainVariable "vstr variable1"
So you can make the toggle with any amount of variables you want.
2 important notes:
* To keep the script clear, I mostly put numbers behind my variables. Like I did in my example: name1 and name2. This is not obligatory. You can use whatever name you like for your variables.
* I used a certain sequence for the lines of code, but there is no rule for this. You can determine the sequence yourself. This would work just as well:
set variable1 "command1; command2; ...; set mainVariable vstr variable2"
set variable2 "command1; command2; ...; set mainVariable vstr variable1"
set mainVariable "vstr variable1"
bind key "vstr mainVariable"
As you can see, I now placed the line with the key bind at the end. The only important thing is that it makes sense to you! So that when you read your script later on, you still understand how it works.
One more example to make sure, it's absolutely clear.
Now we want to be able to change the sensitivity of the mouse with one key. Again let's take x. The values for the sensitivity the scripts needs to alternate between, are: 1, 1.5 and 2. The script should look like this:
bind x "vstr sens"
set sens1 "sensitivity 1; set sens vstr sens2"
set sens2 "sensitivity 1.5; set sens vstr sens3"
set sens2 "sensitivity 2; set sens vstr sens1"
set sens "vstr sens1"
|