Identifier: Difference between revisions

From Bohemia Interactive Community
Jump to navigation Jump to search
m (Fix wording)
m (private → local, public → global)
Line 12: Line 12:
* _player
* _player
|
|
* <span style="color: red">1</span>variable
* {{Color|red|1}}variable
* _my<span style="color: red">#</span>localVar
* _my{{Color|red|#}}localVar
* _guy1<span style="color: red">&amp;</span>2var
* _guy1{{Color|red|&amp;}}2var
* <span style="color: red">player</span>
* {{Color|red|player}}
|}
|}
Binding rules for identifiers:
Binding rules for identifiers:
Line 27: Line 27:
== Recommendations ==
== Recommendations ==


It is recommended to write '''private variable identifiers''' in {{Wikipedia|Camel_case|camel case}} syntax. This makes identifiers more readable:
It is recommended to write '''local variable identifiers''' in {{Wikipedia|Camel_case|camel case}} syntax. This makes identifiers more readable:
<code>[[private]] _myVariableName = 5;</code>
<code>[[private]] _myVariableName = 5;</code>


It is also recommended to prefix '''public variable identifiers''' with '''your''' [[OFPEC tags|tag]] in order to avoid any potential conflict between addons, scripts and missions:
It is also recommended to prefix '''global variable identifiers''' with '''your''' [[OFPEC tags|tag]] in order to avoid any potential conflict between addons, scripts and missions:
<code>{{cc|<span style{{=}}"color: purple; font-weight: bold">Tag</span>_identifier}}
<code>{{cc|<span style{{=}}"color: purple; font-weight: bold">Tag</span>_identifier}}
<span style="color: purple; font-weight: bold">UNIQUETAG</span>_player = [[player]];</code>
<span style="color: purple; font-weight: bold">UNIQUETAG</span>_player = [[player]];</code>

Revision as of 13:28, 7 April 2020

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


Rules

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 invaild)
  • Identifiers of user-defined 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;

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;

A less encountered naming format is separating all sub-words with underscores: _my_variable_name = 5;


See also