Identifier: Difference between revisions

From Bohemia Interactive Community
Jump to navigation Jump to search
m (Text replacement - "[[OFPEC tags" to "[[Scripting Tags")
 
(17 intermediate revisions by 5 users not shown)
Line 1: Line 1:
An '''identifier''' is a name given to a [[Variables|variable]] that the scripter can choose: It is the name that ''identifies'' the variable.
__NOTOC__
An [[Identifier]] is the name given to a [[Variables|variable]] that the scripter can choose: It is the name that ''identifies'' the variable.


== Rules ==
== Rules ==
 
{| class="wikitable float-right"
|+ Regex: {{hl|[a-zA-Z_][a-zA-Z0-9_]*}}
! Valid
! Invalid
|-
|
* variable1
* _my_local_variable
* _1variable
* _player
|
* {{Color|red|1}}variable
* _my{{Color|red|#}}localVar
* _guy1{{Color|red|&}}2var
* {{Color|red|player}}
|}
Binding rules for identifiers:
Binding rules for identifiers:
* Identifiers may consist of any ASCII text characters (a-z, A-Z), numbers (0-9) and underscores (_).
* Identifiers '''must not''' start with a number (e.g "9myVariable" is invalid).
* Identifiers of [[Variables|local variables]] '''must''' start with an underscore.
* Identifiers are '''case-insensitive'''.
* Identifiers '''cannot be the same as reserved words''' (e.g [[:Category:Scripting Commands|commands]] and [[:Category:Functions|functions]]).


* Identifiers may consist of any ASCII characters (a-z, A-Z), numbers (0-9) and underscores (_)
* Identifiers '''must not''' start with a number (f.i. "9myVariable")
* Identifiers of [[Variables|local variables]] '''must''' start with an underscore


'''Examples of valid identifiers:'''
== Recommendations ==
 
myVariable1
_localVariable95
_23Variable


'''Examples of invalid identifiers:'''
It is recommended to write '''local variable identifiers''' in {{Link|https://en.wikipedia.org/wiki/Camel_case|camel case}} syntax. This makes identifiers more readable:
<sqf>private _myVariableName = 5;</sqf>


123Variable
{{Feature|important|
9_vA#riable
{{Link|https://en.wikipedia.org/wiki/Snake_case}} is sometimes encountered but is not recommended and should be avoided:
_this&variable
<sqf>_my_variable_name = 5;</sqf>
}}


== Recommendations ==


It is recommended to write identifiers in camel-case-syntax. That means that all sub-words in the identifier are started with an upper-case character. This makes identifiers better readable.
It is also recommended to prefix '''global variable identifiers''' with '''your''' [[Scripting Tags|tag]] in order to avoid any potential conflict between addons, scripts and missions:
{{cc|'''{{Color|purple|Tag}}'''_identifier}}
<span style="color: purple; font-weight: bold">UNIQUETAG</span>_player = [[player]];


'''Example:'''


myCamelCaseIdentifier


Another but longer method is to split all sub-words with underscores.
== See also ==


'''Example:'''
* [[Variables]]


my_splitted_identifier


[[Category: ArmA: Scripting]]
[[Category: Syntax]]
[[Category: Scripting Topics]]

Latest revision as of 12:01, 2 October 2024

An Identifier is the name given to a variable that the scripter can choose: It is the name that identifies the variable.

Rules

Regex: [a-zA-Z_][a-zA-Z0-9_]*
Valid Invalid
  • variable1
  • _my_local_variable
  • _1variable
  • _player
  • 1variable
  • _my#localVar
  • _guy1&2var
  • player

Binding rules for identifiers:

  • Identifiers may consist of any ASCII text characters (a-z, A-Z), numbers (0-9) and underscores (_).
  • Identifiers must not start with a number (e.g "9myVariable" is invalid).
  • Identifiers of local variables must start with an underscore.
  • Identifiers are case-insensitive.
  • Identifiers cannot be the same as reserved words (e.g commands and functions).


Recommendations

It is recommended to write local variable identifiers in camel case syntax. This makes identifiers more readable:

private _myVariableName = 5;

Snake case is sometimes encountered but is not recommended and should be avoided:
_my_variable_name = 5;


It is also recommended to prefix global variable identifiers with your tag in order to avoid any potential conflict between addons, scripts and missions:

// Tag_identifier
UNIQUETAG_player = player;


See also