private: Difference between revisions

From Bohemia Interactive Community
Jump to navigation Jump to search
No edit summary
(Add privateAll seeAlso link)
 
(73 intermediate revisions by 9 users not shown)
Line 1: Line 1:
{{Command|= Comments
{{RV|type=command
____________________________________________________________________________________________


| ofp |= Game name
|game1= ofp
|version1= 1.00


|1.00|= Game version
|game2= ofpe
____________________________________________________________________________________________
|version2= 1.00


| Sets a variable to the innermost scope. The variable has to be [[Variables#Local_Variables|local]]. |= Description
|game3= arma1
____________________________________________________________________________________________
|version3= 1.00


| '''private''' variableName |= Syntax
|game4= arma2
|p1= variableName: [[String]] |= Parameter 1
|version4= 1.00
| [[Nothing]] |= Return value


|s2= '''private''' variableNameList |=Syntax
|game5= arma2oa
|p21= variableNameList: [[Array]] of [[String]]s |= Parameter 1
|version5= 1.50
|r2= [[Nothing]] |= Return value
____________________________________________________________________________________________
|x1= <code>[[private]] "_varname";</code> |= Example 1
|x2= <code>[[private]] ["_varname1", "_varname2"];</code> |= Example 2


| [[Variables#Scope|Scope]] |= See also
|game6= tkoh
|version6= 1.00


|game7= arma3
|version7= 0.50
|gr1= Variables
|descr= Sets a variable to the innermost scope (see {{Link|#Example 3}}). See also [[param]] and [[params]].
{{Feature|informative|[[private]] variables '''must''' start with an underscore: {{hl|c= [[private]] '''{{Color|red|_}}'''myVar1 = "myVar";}} - see [[Identifier]].}}
{{Feature|warning|'''Always''' make your local variables '''private''' (through [[private]] or [[params]]) in order to avoid [[Variables#Scopes|overwriting a local variable of the same name]].}}
|s1= [[private]] variableName
|p1= variableName: [[String]] - e.g <sqf inline>"_myVar"</sqf>
|r1= [[Nothing]]
|s2= [[private]] variableNameList
|p21= variableNameList: [[Array]] of [[String]]s - e.g <sqf inline>["_target", "_damage"]</sqf>
|r2= [[Nothing]]
|s3= [[private]] _identifier = value
|s3since= arma3 1.54
|p41= _identifier: underscored [[Identifier|variable name]], for example <sqf inline>_myVar</sqf>
|p42= value: [[Anything]]: value to assign to the variable
|r3= [[Nothing]]
|x1= <sqf>
private _varname = "this is my new variable"; // since {{arma3}} v1.54
// identical, but less performant
private "_varname";
_varname = "this is my new variable";
</sqf>
|x2= <sqf>
private ["_varname1", "_varname2"];
_varname1 = "variable 1";
_varname2 = "variable 2";
</sqf>
|x3= <sqf>
_lol = 123; call { hint str [_lol] }; // [123]
_lol = 123; call { private "_lol"; hint str [_lol] }; // [any]
</sqf>
|x4= <sqf>
_myvar = 123;
systemChat str [_myvar]; // [123]
call {
systemChat str [_myvar]; // [123]
private "_myvar";
systemChat str [_myvar]; // [any]
_myvar = 345;
systemChat str [_myvar]; // [345]
};
systemChat str [_myvar]; // [123]
</sqf>
|seealso= [[param]] [[params]] [[privateAll]] {{Link|Variables#Scopes}}
}}
}}


<h3 style="display:none">Notes</h3>
{{Note
<dl class="command_description">
|user= Faguss
<!-- Note Section BEGIN -->
|timestamp= 20100804133000
<dd class="notedate">Posted on Sep 24, 2009 15:04
|text= The higher scope is also the script from which the function has been called.<br>
<dt class="note">'''[[User:ColonelSandersLite|ColonelSandersLite]]'''
in '''script2.sqf''':
<dd class="note"><br>
<sqf>_a = 2;</sqf>
The example provided is fairly worthless without a context.<br>
in '''script1.sqf''':
Using the private command allows you to declare a variable in the current scope, without regards to variables in a higher scope with the same name.  Note that if you try to declare a variable without an underscore (meaning it's global) with the private command, it will cause an error.  Specifically: "Error Local variable in global space".<br>
<sqf>
Here's a code example with output for your benefit.<br>
_a = 1;
<code>
call compile preprocessFileLineNumbers "script2.sqf";
_foo = 10;
hint format ["%1", _a];
if (true) then
</sqf>
{
Game will display 2.<br>
    private ["_foo"];
Inserting <sqf inline>private "_a"</sqf> in the function prevents the change and so number 1 will be displayed on the screen.
    _foo = 5;
}}
    player sideChat format ["%1", _foo];
};
player sideChat format ["%1", _foo];
</code>
In this example, the first sidechat (innermost) returns 5 while the second sidechat (outermost) returns 10.<br>
<code>
if (true) then
{
    private ["_bar"];
    _bar = 5;
    player sideChat format ["%1", _bar];
};
</code>
In this example, the private command does nothing and is simply a waste of code, assuming there is no higher level code to interfere with the if statement.<br>
<dd class="notedate">Posted on August 4, 2010
<dt class="note">'''[[User:Faguss|Faguss]]'''
<dd class="note">The higher scope is also the script from which the function has been called.<br>
If you've got in the script:
<code>
_a = 1;
call compile loadFile "function.sqf";
hint format ["%1", _a];
</code>
And in the <i>function.sqf</i>:
<code>
_a = 2;
</code>
Game will display 2.<br><br>
Inserting <i>private "_a"</i> in the function prevents the change and so number 1 will be displayed on the screen.
<!-- Note Section END -->
</dl>


<h3 style="display:none">Bottom Section</h3>
{{Note
|user= DreadedEntity
|timestamp= 20150225170600
|text= Recursive loops require the use of [[private]]. Without it, your variables will be overwritten.
}}


[[Category:Scripting Commands|PRIVATE]]
{{Note
[[Category:Scripting Commands OFP 1.99|{{uc:{{PAGENAME}}}}]]
|user= 654wak654
[[Category:Scripting Commands OFP 1.96|PRIVATE]]
|timestamp= 20180131103700
[[Category:Scripting Commands ArmA|PRIVATE]]
|text= This command is ''similar'' to javascript's {{Link|https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/let|let}} keyword.<br>
[[Category:Scripting Commands ArmA2|{{uc:{{PAGENAME}}}}]]
'''EDIT:''' in the way that it scopes the variable to the innermost scope. Otherwise, let and private can behave differently - [[User:Lou Montana|Lou Montana]] ([[User talk:Lou Montana|talk]])
[[Category:Scripting Commands Arma 3|{{uc:{{PAGENAME}}}}]]
}}
[[Category:Scripting_Commands_Take_On_Helicopters|{{uc:{{PAGENAME}}}}]]
 
<!-- CONTINUE Notes -->
<dl class="command_description">
<dd class="notedate">Posted on February 25, 2015 - 17:06 (UTC)</dd>
<dt class="note">[[User:DreadedEntity|DreadedEntity]]</dt>
<dd class="note">
Recursive loops require the use of [[private]]. Without it, your variables will be overwritten.
</dd>
</dl>
<!-- DISCONTINUE Notes -->

Latest revision as of 15:12, 11 July 2024

Hover & click on the images for description

Description

Description:
Sets a variable to the innermost scope (see Example 3). See also param and params.
private variables must start with an underscore: private _myVar1 = "myVar"; - see Identifier.
Always make your local variables private (through private or params) in order to avoid overwriting a local variable of the same name.
Groups:
Variables

Syntax 1

Syntax:
private variableName
Parameters:
variableName: String - e.g "_myVar"
Return Value:
Nothing

Syntax 2

Syntax:
private variableNameList
Parameters:
variableNameList: Array of Strings - e.g ["_target", "_damage"]
Return Value:
Nothing

Syntax 3

Syntax:
private _identifier = value
Parameters:
_identifier: underscored variable name, for example _myVar
value: Anything: value to assign to the variable
Return Value:
Nothing

Examples

Example 1:
private _varname = "this is my new variable"; // since Arma 3 v1.54 // identical, but less performant private "_varname"; _varname = "this is my new variable";
Example 2:
private ["_varname1", "_varname2"]; _varname1 = "variable 1"; _varname2 = "variable 2";
Example 3:
_lol = 123; call { hint str [_lol] }; // [123] _lol = 123; call { private "_lol"; hint str [_lol] }; // [any]
Example 4:
_myvar = 123; systemChat str [_myvar]; // [123] call { systemChat str [_myvar]; // [123] private "_myvar"; systemChat str [_myvar]; // [any] _myvar = 345; systemChat str [_myvar]; // [345] }; systemChat str [_myvar]; // [123]

Additional Information

See also:
param params privateAll 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
Faguss - c
Posted on Aug 04, 2010 - 13:30 (UTC)
The higher scope is also the script from which the function has been called.
in script2.sqf:
_a = 2;
in script1.sqf:
_a = 1; call compile preprocessFileLineNumbers "script2.sqf"; hint format ["%1", _a];
Game will display 2.
Inserting private "_a" in the function prevents the change and so number 1 will be displayed on the screen.
DreadedEntity - c
Posted on Feb 25, 2015 - 17:06 (UTC)
Recursive loops require the use of private. Without it, your variables will be overwritten.
654wak654 - c
Posted on Jan 31, 2018 - 10:37 (UTC)
This command is similar to javascript's let keyword.
EDIT: in the way that it scopes the variable to the innermost scope. Otherwise, let and private can behave differently - Lou Montana (talk)