There are 5 kinds of variables in Arma: private, private special, public local, public local special, public global. Each variable kind has own scope of existence you have to be aware of.

Private variables

The engine automatically interprets any variable name that starts with underscore as a private variable. You can use standard alphanumeric characters A to Z, a to z, 0 to 9 and _ for variable names. So _hello, _HELLO_WORLD2000, _____hello, _123, ____123_______hello are all valid and will all be considered private. Private variables are created where they are defined within a scope and get deleted once the scope is finished. Therefore any private variable defined first time within the scope will be undefined outside of the scope.

//_i is not defined yet so it is nil here if true then { //beginning of a scope _i = 1; //define _i //_i is 1 and defined here }; //end of scope //_i is nil here since its scope has ended

If you have child scope within your parent scope in which your private variable is defined, it will also be defined inside the child scope and the child of that child and so on. So if you change private variable within child scope and want its value in parent scope make sure it is defined in parent scope first.

_i = 1; if true then { //_i is 1 and defined here if true then { //_i is 1 and defined here if true then { //_i is 1 and defined here }; }; };

But what if you want to make sure the value of a variable you are using within a scope is exclusive to this scope? Then you need to use private command. You can either define a single variable as private or an array of variables.

private "_a"; private "_b"; private "_c"; //is the same as private ["_a","_b","_c"];

Using private command ensures that if there is a variable of the same name defined in parent scope, it will not be affected and that the variable that you just explicitly made private is undefined within the child scope until you define it.

hint format ["%1", _i]; //"any" _i = 1; hint format ["%1", _i]; //"1" if true then { hint format ["%1", _i]; //"1" private "_i"; hint format ["%1", _i]; //"any" _i = 2; hint format ["%1", _i]; //"2" } hint format ["%1", _i]; //"1"

Private special variables

Have a look at the list of private special variables. With certain commands the engine would automatically create a special variable that would only exist within the scope of that command then assign special value to it also depending on command. Because the engine will first use private command on those special variables, they will exist only within the scope of the command code.

//on first iteration { { hint str _x; //_x is 1 } forEach _x; //_x is [1,2,3] } forEach [[1,2,3],[4,5,6],[7,8,9]];

As you can see in the above example _x in the parent loop is not the same _x in the child loop. This makes it safe to have nested loops. If however you need the value of the parent _x inside child loop you can assign _x to another variable then pass it into the child loop.

//on first iteration { _x2 = _x; { hint str _x; //_x is 1 hint str _x2; //_x2 is [1,2,3] } forEach _x; } forEach [[1,2,3],[4,5,6],[7,8,9]];

There is nothing much else to say about private special variables except that to be on a safe side you should avoid making your own variables with the same names. Also to summarise this part, it will not hurt to take extra precaution and explicitly set the variables of your script of function as private (unless there is a reason not to), especially if your function is called using call command. The private variable extractor is just the tool for the job.

_function = { _var = _var + 1; }; _var = 10; call _function; hint str _var; //_var is 11 _var = 100; //_var is defined elsewhere function = { private "_var"; _var = _this select 0; _var = _var + 1; _var }; hint str ([10] call function); //result is 11, however _var is still 100

This is it for Part 1, looks like it is going to be Part 2.

Enjoy,
KK