SQF Syntax: Difference between revisions

From Bohemia Interactive Community
Jump to navigation Jump to search
m (Mentioned SQF meaning Status Quo Function)
(Rewrote everything)
Line 1: Line 1:
[[SQF syntax]] (where SQF stands for Status Quo Function) was introduced in [[Operation Flashpoint: Resistance Version History|OFP: Resistance]] and is the common [[Syntax|syntax]] since [[Armed Assault]]. An alternative syntax is the [[SQS syntax]] (deprecated since Armed Assault).
__TOC__
'''Status Quo Function''' is the successor of '''[[SQS_syntax|Status Quo Script]]''' which is deprecated since [[Armed Assault]].
SQF was first introduced in [[Operation_Flashpoint:_Resistance_Introduction|Operation Flashpoint: Resistance]].


== Rules ==
An SQF expression has to be terminated via either {{Inline code|;}} or {{Inline code|,}}.
Example:
_num = 10;
_num = _num + 20; [[systemChat]] _num;
In the above example, there are three expressions.
# {{Inline code|_num {{=}} 10}}
# {{Inline code|_num {{=}} _num + 20}}
# {{Inline code|[[systemChat]] _num}}
All get separated, not via a new line but the {{Inline code|;}}.


Binding rules:
SQF also allows [[Array|arrays]] and [[Block|codeblocks]] to have expressions inside of them.


* Curled braces ({ }) group code to '''[[Block|blocks]]'''
An [[Array|array]] will always start with {{Inline code|[}} and ends with {{Inline code|]}}. The [[Array|array]] may contain another [[Data_Types|value type]] including other [[Array|arrays]].
* [[Statement|Statements]] (thus also blocks) are followed by '''semicolons''' (;)


The latter rule tells the game engine where one statement ends and the next starts.
A [[Block|codeblock]] is a separate [[Data_Types|value type]] containing numerous SQF expressions. It will be executed when used with eg. [[call]] and can be assigned to a [[Variables|variable]] just like any other [[Data_Types|value type]]. [[Block|Codeblocks]] start with {{Inline code|<nowiki>{</nowiki>}} and end with {{Inline code|<nowiki>}</nowiki>}}


Example:
== Language Structure ==
The SQF Language is fairly simple in how it is built.
In fact: there are barely any actual language structures at all.
 
The functionality is provided via so called ''[[:Category:Scripting_Commands_Arma_3|operators]]'' (or more commonly known [[:Category:Scripting_Commands_Arma_3|scripting commands]]).
Those [[:Category:Scripting_Commands_Arma_3|operators]] are one of the following types: [[#nular|Nular]], [[#unary|Unary]],  or [[#binary|Binary]].
 
=== Comments ===
A comment is additional text that gets ignored when a script is parsed.
They exists they exist for the to allow you, the coder, to add additional info to your code so that if somebody else (including your future you) is looking into the code can understand what you did there.


STATEMENT 1;
In SQF, there are two kind of comments:
  STATEMENT 2;
  {{codecomment|// In-line comment that ends on new line}}
   
   
  BLOCK
  {{codecomment|/* Block Comment that can span above multiple lines and ends on the following character combination: */}}
{
A comment can occur anywhere but inside a [[String|string]].
    STATEMENT 3;
For example, the following would be valid:
    STATEMENT 4;
  1 + {{codecomment|/* Some random comment in an expression. */}} 1
  };


While [[SQS syntax]] is line based, SQF syntax is based on structured expressions. End-of-line has no special meaning - it is considered to be equivalent to space or tab, and is therefore not required, even when ending a statement.
It should be mentioned that there is a [[comment]] unary [[:Category:Scripting_Commands_Arma_3|operator]] that should not be used as it will actually be executed (thus taking time to execute) but does nothing besides consuming a [[String|string]].
There is no benefit in using it and the reason it exists is solely for backward Compatibility.
Another way to make a ''comment'' that way, is to just place a [[String|string]]: {{Inline code|[...]; "i can be considered as a comment but should not be used"; [...]}}


== Comments ==
''Comments are removed during the [[PreProcessor_Commands|preprocessing]] phase.'' This is important to know as that prevents usage in eg. a [[String|string]] that gets compiled using the [[compile]] unary [[:Category:Scripting_Commands_Arma_3|operator]] or when only using [[loadFile]].


A comment is any free text which is ignored by the game engine. In SQF syntax you can write comments using the command [[comment]].
=== <span id="nular">Nular Operators</span> ===
A nular operator is more or less a computed [Variables|variable]]. Each time accessed, it will return the current state of something.
It is tempting to think of a nular [[:Category:Scripting_Commands_Arma_3|operator]] as nothing more but a magic [[Variables|global variable]], but it is important to differentiate!


Example:
Consider following example in a mission with eg. 5 units:
{{codecomment|// Put the result of [[allUnits]] into a [[Variables|variable]].}}
_unitsArray = [[allUnits]];
{{codecomment|// Display the current [[Array|array]] size using [[systemChat]].}}
[[systemChat]] [[str]] [[count]] _unitsArray;
{{codecomment|// Create a new unit in the player group.}}
[[group]] [[player]] [[createUnit]] ["B_RangeMaster_F", [[position]] [[player]], [], 0, "FORM"];
{{codecomment|// Output the [[Array|array]] size again}}
[[systemChat]] [[str]] [[count]] _unitsArray;
{{codecomment|// Output the size of [[allUnits]]}}
[[systemChat]] [[str]] [[count]] [[allUnits]];
 
Now, what would the output of this look like?
System: 5
System: 5
System: 6
 
As you can see, {{Inline code|_unitsArray}} was not automatically updated as it would have been if it was not generated each time. If [[allUnits]] was just a [[Variables|global variable]], our private [Variables|variable]] should have had reflected the change as [[Data_Types|value types]] are passed by reference.
The reason for this is because [[allUnits]] and other nular operators just return the current state of something and do not return a reference to eg. an [[Array|array]] containing all units.
It is generated each time, which is why some of theese operators are more expensive to run then ''just'' using a [Variables|variable]].
 
=== <span id="unary">Unary Operators</span> ===
The unary [[:Category:Scripting_Commands_Arma_3|operators]] are [[:Category:Scripting_Commands_Arma_3|operators]] that expect an argument on their right side ({{Inline code|unary &lt;argument&gt;}}). They always will take the first argument that occurs.


  comment "This is a comment";
A common mistake would be the following:
  {{codecomment|// Create some [[Array|array]] containing three arrays.}}
_arr = [[1, 2, 3, 4, 5], [1, 2, 3, 4], [1, 2]];
{{codecomment|// Wrongly use the [[select]] operator to get the count of the third array.}}
[[count]] _arr [[select]] 2; <span style="color: Red;">// Error</span>


Now, what went wrong?


If a file is loaded with [[preprocessFile]], [[execVM]] or [[spawn]], you may also define C-like comments (does not work for [[loadFile]]):
Let's put some brackets in the right places to make the mistake understandable:
([[count]] _arr) [[select]] 2; <span style="color: Red;">// Error</span>


; Line comment
Due to the nature of Unary [[:Category:Scripting_Commands_Arma_3|operators]], count instantly consumes our [[Variables|variable]] ''_arr'' and returns the number ''3''.
: A line comment starts with <tt>//</tt> and makes the rest of the line a comment.
The ''3'' then is passed to [[select]] which does not knows what to do with a number as left argument and thus errors out.


; Block comment
To do it correctly, one would have to put the {{Inline code|_arr select 2}} in brackets.
: A block comment starts with <tt>/*</tt> and ends with <tt>*/</tt>. All text in between is considered a comment.
The correct code thus would be:
{{codecomment|// Create an [[Array|array]] containing three [[Array|arrays]].}}
_arr = [[1, 2, 3, 4, 5], [1, 2, 3, 4], [1, 2]];
{{codecomment|// Use Brackets to correctly get count of the third [[Array|array]].}}
[[count]] (_arr [[select]] 2); {{codecomment|// Good :) will evaluate to 2}}


Examples:
=== <span id="binary">Binary Operators</span> ===
Binary [[:Category:Scripting_Commands_Arma_3|operators]] expect two arguments ({{Inline code|&lt;1st argument&gt; binary &lt;2nd argument&gt;}}) and are executed according to their [[#precedence|precedence]]. If their [[#precedence|precedence]] is equal, they are executed left to right.


  // This is a line comment
As example, we will look into the following expression:
  {{codecomment|// Create a nested [[Array|array]] with 5 levels.}}
_arr = [[[[[1]]]]];
   
   
  /*
  {{codecomment|// Receive the nested number with some random math expressions.}}
This is
_arr [[select]] 0 [[select]] 1 [[a_minus_b|-]] 1 [[select]] 15 [[a_/_b|/]]  3 [[a_minus_b|-]] 5 [[select]] 0 [[select]] 10 [[a_*_b|*]] 10 [[a_plus_b|+]] 4 [[a_*_b|*]] 0 [[a_minus_b|-]] 100{{codecomment|// Evaluates to 1.}}
a very long
 
  block comment
Now, let us analyze why this is happening for the first few expressions:
  */
# _arr is loaded
# 0 is loaded
# [[select]] is executed with the result of 1. & 2.
# 1 is loaded
# 1 is loaded
# [[a_minus_b|-]] is executed with the result of 4. & 5.
# [[select]] is executed with the result of 3. & 6.
# ...
 
If we now would put brackets at the correct spots, the expression will get clearer:
  ((((_arr [[select]] 0) [[select]] (1 [[a_minus_b|-]] 1)) [[select]] ((15 [[a_/_b|/]] 3) [[a_minus_b|-]] 5)) [[select]] 0) [[select]] (((10 [[a_*_b|*]] 10) [[a_plus_b|+]] (4 [[a_*_b|*]] 0)) [[a_minus_b|-]] 100)


== Language Constructs ==
As you can see the [[a_*_b|*]] and [[a_/_b|/]] are executed first which matches their [[#precedence|precedence]]. Afterwards, the [[a_plus_b|+]] and [[a_minus_b|-]] [[:Category:Scripting_Commands_Arma_3|operators]] will get executed followed by our [[select]] operator, which are executed from the left to the right.


Read the article [[Control Structures]] for information about the control structures available in SQF syntax.
== <span id="precedence">Rules Of Precedence</span> ==
{|
|11 '''Highest Precedence'''
|''ALL NULAR OPERATORS, VARIABLES, VALUES OR BRACED EXPRESSIONS''
|-
|10
|''ALL UNARY OPERATORS''
|-
|9
|<code>[[select|#]]</code>
|-
|8
|<code>[[a_^_b|^]]</code>
|-
|7
|<code>[[a_*_b|*]] [[a_/_b|/]] [[mod|%]] [[mod]] [[atan2]]</code>
|-
|6
|<code>[[a_plus_b|+]] [[a_minus_b|-]] [[max]] [[min]]</code>
|-
|5
|<code>[[else]]</code>
|-
|4
|''ALL BINARY OPERATORS''
|-
|3
|<code>[[==]] [[!=]] [[a_greater_b|&gt;]] [[a_less_b|&lt;]] [[a_less=_b|&gt;=]] [[a_less=_b|&lt;=]] [[config_greater_greater_name|&gt;&gt;]]</code>
|-
|2
|<code>[[and|&&]] [[and]]</code>
|-
|1 '''Lowest Precedence'''
|<code>[[a or b|<nowiki>||</nowiki>]] [[a or b|or]]</code>
|}


== See also ==
== See also ==
 
* [[Control Structures]]
* [[SQS syntax]]
* [[SQS syntax]]
* [[Function]]
* [[Function]]
Line 63: Line 169:
* [[SQS to SQF conversion]]
* [[SQS to SQF conversion]]


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

Revision as of 16:05, 23 April 2018

Status Quo Function is the successor of Status Quo Script which is deprecated since Armed Assault. SQF was first introduced in Operation Flashpoint: Resistance.

An SQF expression has to be terminated via either ; or ,. Example:

_num = 10;
_num = _num + 20; systemChat _num;

In the above example, there are three expressions.

  1. _num = 10
  2. _num = _num + 20
  3. systemChat _num

All get separated, not via a new line but the ;.

SQF also allows arrays and codeblocks to have expressions inside of them.

An array will always start with [ and ends with ]. The array may contain another value type including other arrays.

A codeblock is a separate value type containing numerous SQF expressions. It will be executed when used with eg. call and can be assigned to a variable just like any other value type. Codeblocks start with { and end with }

Language Structure

The SQF Language is fairly simple in how it is built. In fact: there are barely any actual language structures at all.

The functionality is provided via so called operators (or more commonly known scripting commands). Those operators are one of the following types: Nular, Unary, or Binary.

Comments

A comment is additional text that gets ignored when a script is parsed. They exists they exist for the to allow you, the coder, to add additional info to your code so that if somebody else (including your future you) is looking into the code can understand what you did there.

In SQF, there are two kind of comments:

// In-line comment that ends on new line

/* Block Comment that can span above multiple lines and ends on the following character combination: */

A comment can occur anywhere but inside a string. For example, the following would be valid:

1 + /* Some random comment in an expression. */ 1

It should be mentioned that there is a comment unary operator that should not be used as it will actually be executed (thus taking time to execute) but does nothing besides consuming a string. There is no benefit in using it and the reason it exists is solely for backward Compatibility. Another way to make a comment that way, is to just place a string: [...]; "i can be considered as a comment but should not be used"; [...]

Comments are removed during the preprocessing phase. This is important to know as that prevents usage in eg. a string that gets compiled using the compile unary operator or when only using loadFile.

Nular Operators

A nular operator is more or less a computed [Variables|variable]]. Each time accessed, it will return the current state of something. It is tempting to think of a nular operator as nothing more but a magic global variable, but it is important to differentiate!

Consider following example in a mission with eg. 5 units:

// Put the result of allUnits into a variable.
_unitsArray = allUnits;

// Display the current array size using systemChat.
systemChat str count _unitsArray;

// Create a new unit in the player group.
group player createUnit ["B_RangeMaster_F", position player, [], 0, "FORM"];

// Output the array size again
systemChat str count _unitsArray;

// Output the size of allUnits
systemChat str count allUnits;

Now, what would the output of this look like?

System: 5
System: 5
System: 6

As you can see, _unitsArray was not automatically updated as it would have been if it was not generated each time. If allUnits was just a global variable, our private [Variables|variable]] should have had reflected the change as value types are passed by reference. The reason for this is because allUnits and other nular operators just return the current state of something and do not return a reference to eg. an array containing all units. It is generated each time, which is why some of theese operators are more expensive to run then just using a [Variables|variable]].

Unary Operators

The unary operators are operators that expect an argument on their right side (unary <argument>). They always will take the first argument that occurs.

A common mistake would be the following:

// Create some array containing three arrays.
_arr = [[1, 2, 3, 4, 5], [1, 2, 3, 4], [1, 2]];

// Wrongly use the select operator to get the count of the third array.
count _arr select 2; // Error

Now, what went wrong?

Let's put some brackets in the right places to make the mistake understandable:

(count _arr) select 2; // Error

Due to the nature of Unary operators, count instantly consumes our variable _arr and returns the number 3. The 3 then is passed to select which does not knows what to do with a number as left argument and thus errors out.

To do it correctly, one would have to put the _arr select 2 in brackets. The correct code thus would be:

// Create an array containing three arrays.
_arr = [[1, 2, 3, 4, 5], [1, 2, 3, 4], [1, 2]];

// Use Brackets to correctly get count of the third array.
count (_arr select 2); // Good :) will evaluate to 2

Binary Operators

Binary operators expect two arguments (<1st argument> binary <2nd argument>) and are executed according to their precedence. If their precedence is equal, they are executed left to right.

As example, we will look into the following expression:

// Create a nested array with 5 levels.
_arr = [[[[[1]]]]];

// Receive the nested number with some random math expressions.
_arr select 0 select 1 - 1 select 15 /  3 - 5 select 0 select 10 * 10 + 4 * 0 - 100// Evaluates to 1.

Now, let us analyze why this is happening for the first few expressions:

  1. _arr is loaded
  2. 0 is loaded
  3. select is executed with the result of 1. & 2.
  4. 1 is loaded
  5. 1 is loaded
  6. - is executed with the result of 4. & 5.
  7. select is executed with the result of 3. & 6.
  8. ...

If we now would put brackets at the correct spots, the expression will get clearer:

((((_arr select 0) select (1 - 1)) select ((15 /  3) - 5)) select 0) select (((10 * 10) + (4 * 0)) - 100)

As you can see the * and / are executed first which matches their precedence. Afterwards, the + and - operators will get executed followed by our select operator, which are executed from the left to the right.

Rules Of Precedence

11 Highest Precedence ALL NULAR OPERATORS, VARIABLES, VALUES OR BRACED EXPRESSIONS
10 ALL UNARY OPERATORS
9 #
8 ^
7 * / % mod atan2
6 + - max min
5 else
4 ALL BINARY OPERATORS
3 == != > < >= <= >>
2 && and
1 Lowest Precedence || or

See also