{"name":"privateAll","operator":null,"title":"privateAll","type":"command","url":"https://community.bistudio.com/wiki/privateAll","revid":369773,"updated":"2024-10-08T15:08:58Z","groups":["Variables"],"status":"ok","replaced_by":[],"games":[{"game":"arma3","name":"Arma 3","version":"2.18"}],"since":{"game":"arma3","name":"Arma 3","version":"2.18"},"arma3_since":"2.18","branch":null,"locality":{"argument":null,"effect":null,"server_exec":false},"exec":null,"description":"Disables the implicit inheritance of local variables, as defined in the parent scope.\n\nBy default, any local variables defined in a parent scope are available to the lower scope code.\n\nAfter 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.","syntaxes":[{"syntax":"privateAll","since":null,"effect":null,"exec":null,"params":[],"returns":{"type":"Nothing","types":["Nothing"],"description":null,"text":"Nothing"},"form":"nullary","operator":null,"left":null,"right":null}],"examples":[{"text":"```sqf\n_a = 1;\n_b = 2;\n_c = 3;\n4 call {\n    // _a, _b and _c from the parent scope are accessible at this point\n    systemChat str [_a, _b, _c];             // prints [1, 2, 3]\n    privateAll;                             // _a, _b and _c from the parent scope can no longer be accessed\n    systemChat str [_a, _b, _c];             // prints [Any, Any, Any]\n    import [\"_a\", \"_b\"];                     // _a and _b are now defined as private variables in the current scope; _c is still not defined\n    systemChat str [_a, _b, _c, _this];     // prints [1, 2, Any, 4]\n    _a = _a + 1;                             // changes the private _a variable in the current scope, but doesn't change the parent scope _a\n};\nsystemChat str _a; // _a is still 1\n```","code":["_a = 1;\n_b = 2;\n_c = 3;\n4 call {\n\t// _a, _b and _c from the parent scope are accessible at this point\n\tsystemChat str [_a, _b, _c]; \t\t\t// prints [1, 2, 3]\n\tprivateAll; \t\t\t\t\t\t\t// _a, _b and _c from the parent scope can no longer be accessed\n\tsystemChat str [_a, _b, _c]; \t\t\t// prints [Any, Any, Any]\n\timport [\"_a\", \"_b\"]; \t\t\t\t\t// _a and _b are now defined as private variables in the current scope; _c is still not defined\n\tsystemChat str [_a, _b, _c, _this]; \t// prints [1, 2, Any, 4]\n\t_a = _a + 1; \t\t\t\t\t\t\t// changes the private _a variable in the current scope, but doesn't change the parent scope _a\n};\nsystemChat str _a; // _a is still 1"]},{"text":"```sqf\nprivate _a = /* ... */;\nprivate _b = /* ... */;\nprivate _c = /* ... */;\n\n{\n    _x call {\n        privateAll;\n        import [\"_a\", \"_b\"];\n        call _this;\n    };\n} forEach CustomEventHandlers;\n```\n\n1. Because of the import-statement, code from `CustomEventHandlers` can use `_a` and `_b` without using params (even if `_a` and `_b` were provided via `_this`, which is not the case here).\n1. Code from `CustomEventHandlers` can only see `_a` and `_b`, but no other variables such as `_c`, `_x` or `_forEachIndex`.\n1. Code from `CustomEventHandlers` can modify `_a` and `_b` (e.g. `_a = _b / 2`), but only within its own scope (i.e. `CustomEventHandlers # (n + 1)` will not see any changes that `CustomEventHandlers # n` may have made to `_a` and `_b`).\n1. The values of `_a`, `_b` and `_c` after the forEach-loop are guaranteed to be the same as before the forEach-loop.\n\n> [!IMPORTANT]\n> There is one important exception to points 3 and 4: Modifications by reference (e.g. `_a pushBack 123` where `_a` is an Array) are applied in all scopes. This can be avoided by copying `_a` before calling the `CustomEventHandlers` code:\n>\n> ```sqf\n> private _a = [/* ... */]; // Note that _a is an array.\n> private _b = /* ... */;\n> private _c = /* ... */;\n>\n> {\n>     _x call {\n>         privateAll;\n>         import [\"_a\", \"_b\"];\n>         _a = +_a; // Copy the array so that the code from CustomEventHandlers can no longer modify the original array.\n>         call _this;\n>     };\n> } forEach CustomEventHandlers;\n> ```","code":["private _a = /* ... */;\nprivate _b = /* ... */;\nprivate _c = /* ... */;\n\n{\n\t_x call {\n\t\tprivateAll;\n\t\timport [\"_a\", \"_b\"];\n\t\tcall _this;\n\t};\n} forEach CustomEventHandlers;","private _a = [/* ... */]; // Note that _a is an array.\nprivate _b = /* ... */;\nprivate _c = /* ... */;\n\n{\n\t_x call {\n\t\tprivateAll;\n\t\timport [\"_a\", \"_b\"];\n\t\t_a = +_a; // Copy the array so that the code from CustomEventHandlers can no longer modify the original array.\n\t\tcall _this;\n\t};\n} forEach CustomEventHandlers;"]}],"multiplayer":null,"problems":null,"aliases":[],"see_also":["import","private","Variables"],"notes":[],"categories":["Arma 3: New Scripting Commands","Arma 3: Scripting Commands","Command Group: Variables","Introduced with Arma 3 version 2.18","Scripting Commands"]}