Writing CFG Mods Tutorial 6 - Escaping With Backslash

Escaping With Backslash

 

Escaping, in reference to coding, usually means to ignore the following character or characters, or to do something different with them. In this case, we will be escaping quotes.

 

Let's get started!

 

bind button_rshldr "vstr perks"
set perks "setperk walkerneo specialty_bulletaccuracy;vstr perks2"
set perks2 "setperk walkerneo specialty_rof;vstr perks3"
set perks3 "setperk walkerneo specialty_bulletpenetration"

 

If you've made it this far, this shouldn't seem too complicated. Specialty_rof is double tap, and the other two should be pretty obvious. When I press the right shoulder, it's going to execute the command string in perks, which is going to give me steady aim and then execute the command string in perks2, which is going to give me double tap and so on. What if my gamertag was actually walker neo, with a space in the middle and I put:

 

bind button_rshldr "vstr perks"
set perks "setperk walker neo specialty_bulletaccuracy;vstr perks2"
set perks2 "setperk walker neo specialty_rof;vstr perks3"
set perks3 "setperk walker neo specialty_bulletpenetration"

 

Remember that the usage of setperk is setperk <name> <perk>. To better explain, let's call the different parts of commands terms. By comamnds I don't mean set or bind, I'm talking about the full commands, like set player_sustainammo "1", or bind button_rshldr "noclip". In the command set player_sustainammo "1", there are three terms. The command, the dvar, and the value. The command is set, the dvar is player_sustainammo, and the value is "1". In the command bind button_rshldr "noclip", we also have three terms: the command, the button, and the value. Bind is the command, button_rshldr is the button, and "noclip" is the value. When the game sees setperk, it's looking for two more terms; the name of the player to give the perk and the name of the perk. If I did

 

bind button_rshldr "setperk walker neo specialty_bulletaccuracy"

 

 

We want to name term to be walker neo. Unfortunately, when we put a space after walker, the game will think that that's the end of the name term and will think that neo is the perk. The result will be a message saying "player walker is not in the party". Or, if somehow there was a player named walker in the party, it would say that neo isn't a valid perk. The solution is to do

 

bind button_rshldr "setperk \"walker neo\" specialty_bulletaccuracy"

 

We want to use quotes so that the game sees walker neo as ONE term, but we already opened quotes. Like I said before, if you use quotes inside of quotes, it won't have the effect you probably wanted; it will close the first quotes. Using the backslash before the quotes tells the game to open a new set of quotes instead of closing the first set. ONLY use quotes with backslashes if you are already in quotes. If you have a line of code like:

 

setperk \"walker neo\" specialty_bulletaccuracy

 

 

...it won't work. If you aren't in quotes, then you can use them without worrying about closing another pair:

 

setperk "walker neo" specialty_bulletaccuracy

 

As if that wasn't good enough, there's that other use I mentioned in the advanced vstr tutorial, the one for colors. Remember I did:

 

bind dpad_right "vstr sun_color"
set sun_color "setfromdvar r_lighttweaksuncolor colors;vstr change_colors"
set colors "0 0 1 1"
set change_colors "vstr red"
set red "set colors 1 0 0 1;set change_colors vstr green"
set green "set colors 0 1 0 1;set change_colors vstr blue"
set blue "set colors 0 0 1 1;set change_colors vstr red"

 

That's quite lengthy and can be replaced with:

 

bind dpad_right "vstr sun_color"
set sun_color "setfromdvar r_lighttweaksuncolor colors;vstr change_colors"
set colors "0 0 1 1"
set change_colors "toggle colors \"1 0 0 1\" \"0 1 0 1\" \"0 0 1 1\" "

 

The first three lines are the exact same as before, but instead of using vstr to toggle colors, we actually used toggle. You can see how this wouldn't work without the escaped quotes because it would see each 0 and 1 as value terms to be toggled. By putting the escaped quotes around the colors, it sees each color as a different value term.

 

Similarly, we can redo the control set code. Where as before we had:

 

bind button_rshldr "vstr rb"
bind button_lshldr "vstr lb"
bind button_back "vstr toggle_controls"
set rb "noclip"
set lb "god"
set toggle_controls "vstr controls_2"
set controls_2 "set rb notarget;set lb toggle timescale 1 2 .2;set toggle_controls vstr controls_1"
set controls_1 "set rb noclip;set lb god;set toggle_controls vstr controls_2"

 

We can easily replace it with

 

bind button_rshldr "vstr rb"
bind button_lshldr "vstr lb"
bind button_back "vstr toggle_controls"
set rb "noclip"
set lb "god"
set toggle_controls "toggle rb noclip notarget;toggle lb god \"toggle timescale 1 2 .2\" "

 

 

 

 

 

 

Give yourself a pat on the back, you deserve it! Another tutorial completed!

 

Click here to go to the next tutorial, Advanced Use of Escaping.

 

 

Or, as always, you can draw the line here and head back to the tutorials page and get started on some actual modding.