privateAll: Difference between revisions
Jump to navigation
Jump to search
(Created page with "{{RV|type=command |game1= arma3 |version1= 2.16 |gr1= Variables |descr= Disables the implicit inheritance of local variables, as defined in the parent scope.<br> By default...") |
(Added custom EH system example) |
||
Line 27: | Line 27: | ||
}; | }; | ||
systemChat str _a; // _a is still 1</sqf> | systemChat str _a; // _a is still 1</sqf> | ||
|x2= <sqf> | |||
private _a = /* ... */; | |||
private _b = /* ... */; | |||
private _c = /* ... */; | |||
{ | |||
_x call { | |||
privateAll; | |||
import ["_a", "_b"]; | |||
call _this; | |||
}; | |||
} forEach CustomEventHandlers; | |||
</sqf> | |||
# Because of the [[import]]-statement, code from <var>CustomEventHandlers</var> can use <var>_a</var> and <var>_b</var> without using [[params]] (even if <var>_a</var> and <var>_b</var> were provided via <sqf inline>_this</sqf>, which is not the case here). | |||
# Code from <var>CustomEventHandlers</var> can only see <var>_a</var> and <var>_b</var>, but no other variables such as <var>_c</var>, <sqf inline>_x</sqf> or <sqf inline>_forEachIndex</sqf>. | |||
# Code from <var>CustomEventHandlers</var> can modify <var>_a</var> and <var>_b</var> (e.g. <sqf inline>_a = _b / 2</sqf>), but only within its own scope (i.e. <sqf inline>CustomEventHandlers # (n + 1)</sqf> will not see any changes that <sqf inline>CustomEventHandlers # n</sqf> may have made to <var>_a</var> and <var>_b</var>). | |||
# The values of <var>_a</var>, <var>_b</var> and <var>_c</var> after the [[forEach]]-loop are guaranteed to be the same as before the [[forEach]]-loop. | |||
{{Feature|Important|There is one important exception to points 3 and 4: Modifications by reference (e.g. <sqf inline>_a pushBack 123</sqf> where <var>_a</var> is an [[Array]]) are applied in all scopes. This can be avoided by copying <var>_a</var> before calling the <var>CustomEventHandlers</var> code: | |||
<spoiler text="Show Code"> | |||
<sqf> | |||
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; | |||
</sqf></spoiler>}} | |||
|seealso= [[import]] [[private]] [[Variables#Scopes]] | |seealso= [[import]] [[private]] [[Variables#Scopes]] | ||
}} | }} |
Revision as of 20:50, 30 October 2023
Description
- 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. - Groups:
- Variables
Syntax
- Syntax:
- privateAll
- Return Value:
- 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:
- 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).
- Code from CustomEventHandlers can only see _a and _b, but no other variables such as _c, _x or _forEachIndex.
- 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).
- The values of _a, _b and _c after the forEach-loop are guaranteed to be the same as before the forEach-loop.
Additional Information
- See also:
- import private Variables#Scopes
Notes
-
Report bugs on the Feedback Tracker and/or discuss them on the Arma Discord or on the Forums.
Only post proven facts here! Add Note