Description
Disables the implicit inheritance of local variables, as defined in the parent scope.
By default, any local variables defined in a parent scope are available to the lower scope code.
After using privateAll, parent scope variables can no longer be accessed implicitly. However, it is still possible to explicitly access parent scope variables using the import command.
Syntax
privateAll
Returns: Nothing
Examples
Example 1
_a = 1;
_b = 2;
_c = 3;
4 call {
// _a, _b and _c from the parent scope are accessible at this point
systemChat str [_a, _b, _c]; // prints [1, 2, 3]
privateAll; // _a, _b and _c from the parent scope can no longer be accessed
systemChat str [_a, _b, _c]; // prints [Any, Any, Any]
import ["_a", "_b"]; // _a and _b are now defined as private variables in the current scope; _c is still not defined
systemChat str [_a, _b, _c, _this]; // prints [1, 2, Any, 4]
_a = _a + 1; // changes the private _a variable in the current scope, but doesn't change the parent scope _a
};
systemChat str _a; // _a is still 1
Example 2
private _a = /* ... */;
private _b = /* ... */;
private _c = /* ... */;
{
_x call {
privateAll;
import ["_a", "_b"];
call _this;
};
} forEach CustomEventHandlers;
- Because of the import-statement, code from
CustomEventHandlerscan use_aand_bwithout using params (even if_aand_bwere provided via_this, which is not the case here). - Code from
CustomEventHandlerscan only see_aand_b, but no other variables such as_c,_xor_forEachIndex. - Code from
CustomEventHandlerscan modify_aand_b(e.g._a = _b / 2), but only within its own scope (i.e.CustomEventHandlers # (n + 1)will not see any changes thatCustomEventHandlers # nmay have made to_aand_b). - The values of
_a,_band_cafter the forEach-loop are guaranteed to be the same as before the forEach-loop.
Important
There is one important exception to points 3 and 4: Modifications by reference (e.g.
_a pushBack 123where_ais an Array) are applied in all scopes. This can be avoided by copying_abefore calling theCustomEventHandlerscode:private _a = [/* ... */]; // Note that _a is an array. private _b = /* ... */; private _c = /* ... */; { _x call { privateAll; import ["_a", "_b"]; _a = +_a; // Copy the array so that the code from CustomEventHandlers can no longer modify the original array. call _this; }; } forEach CustomEventHandlers;