X39 – User talk

From Bohemia Interactive Community
Jump to navigation Jump to search
(Started rewriting https://community.bistudio.com/wiki/SQF_syntax)
 
mNo edit summary
Line 1: Line 1:
__TOC__
__TOC__
<code style="display: inline;">Status Quo Function</code> is the successor of <code style="display: inline;">[[SQS_syntax|Status Quo Script]]</code> which is deprecated since [[Armed Assault]].
<code style="display: inline; margin: 0px; padding: 2px;">Status Quo Function</code> is the successor of <code style="display: inline; margin: 0px; padding: 2px;">[[SQS_syntax|Status Quo Script]]</code> which is deprecated since [[Armed Assault]].


== Language Structure ==
== Language Structure ==
Line 11: Line 11:
=== Nular Operators ===
=== Nular Operators ===
A Nular Operator is more or less a computed variable. Each time accessed, it will return the current state of something.
A Nular Operator is more or less a computed variable. Each time accessed, it will return the current state of something.
However, it will not automatically update when its state has changed.
It is tempting to think of a Nular Operator as nothing more but a magic [[Variables|Global Variable]], but it is important to differentiate!


Consider following example on a map with eg. 5 units:
Consider following example in a mission with eg. 5 units:
  <span style="color: #008000">//Put the result of [[allUnits]] into a [[Variables|variable]].</span>
  {{codecomment|//Put the result of [[allUnits]] into a [[Variables|variable]].}}
  _unitsArray = [[allUnits]];
  _unitsArray = [[allUnits]];
  <span style="color: #008000">//Display the current array size using [[systemChat]].</span>
  {{codecomment|//Display the current array size using [[systemChat]].}}
  [[systemChat]] [[str]] [[count]] _unitsArray;
  [[systemChat]] [[str]] [[count]] _unitsArray;
  <span style="color: #008000">//Create a new unit in the player group.</span>
  {{codecomment|//Create a new unit in the player group.}}
  [[group]] [[player]] [[createUnit]] ["B_RangeMaster_F", [[position]] [[player]], [], 0, "FORM"];
  [[group]] [[player]] [[createUnit]] ["B_RangeMaster_F", [[position]] [[player]], [], 0, "FORM"];
  <span style="color: #008000">//Output the array size again</span>
  {{codecomment|//Output the array size again}}
  [[systemChat]] [[str]] [[count]] _unitsArray;
  [[systemChat]] [[str]] [[count]] _unitsArray;
  <span style="color: #008000">//Output the size of [[allUnits]]</span>
  {{codecomment|//Output the size of [[allUnits]]}}
  [[systemChat]] [[str]] [[count]] [[allUnits]];
  [[systemChat]] [[str]] [[count]] [[allUnits]];
 
Now, what would the output of this look like?
Now, what would the output of this look like?
  System: 5
  System: 5
Line 30: Line 30:
  System: 6
  System: 6


As you can see, <code style="display: inline;">_unitsArray</code> was not automatically updated.
As you can see, <code style="display: inline; margin: 0px; padding: 2px;">_unitsArray</code> was not automatically updated. If [[allUnits]] was a [[Variables|Global Variable]], our private variable should have had reflected the change.
The reason for this is because [[allUnits]] and other Nular commands just return the current state of something and do not return a reference to eg. an [[Array]] containing all units.
The reason for this is because [[allUnits]] and other Nular commands 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 commands are extensive
It is generated each time, which is why some of theese commands are extensive


=== Unary Operators ===
=== Unary Operators ===
The Unary Operators are operators that expect an argument on their right side (<code style="display: inline; margin: 0px; padding: 2px;">unary &lt;argument&gt;</code>). They always will take the first argument that occurs.
A common mistake would be the following:
{{codecomment|//Create some array containing three arrays.}}
_arr = [[1, 2, 3, 4, 5], [1, 2, 3, 4], [1, 2]];
{{codecomment|//Wrongly use the [[select]] command to get the count of the third array.}}
[[count]] _arr [[select]] 2; {{codecomment|//Error}}
Now, what went wrong?
Let's put some brackets in the right places to make the mistake understandible:
([[count]] _arr) [[select]] 2; {{codecomment|//Error}}
Due to the nature of Unary operators, count instantly consumes our [[Variables|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 <code style="display: inline; margin: 0px; padding: 2px;">_arr select 2</code> in brackets.
The correct code thus would be:
{{codecomment|//Create some array containing three arrays.}}
_arr = [[1, 2, 3, 4, 5], [1, 2, 3, 4], [1, 2]];
{{codecomment|//Use Brackets to correctly get count of the third array.}}
[[count]] (_arr [[select]] 2); {{codecomment|//Good :)}}
=== Binary Operators ===
=== Binary Operators ===
== Rules Of Precedence ==
== Rules Of Precedence ==

Revision as of 11:40, 23 April 2018

Status Quo Function is the successor of Status Quo Script which is deprecated since Armed Assault.

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). Those operators are one of the following types: Nular, Unary or Binary.

Nular Operators

A Nular Operator is more or less a computed 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. If allUnits was a Global Variable, our private variable should have had reflected the change. The reason for this is because allUnits and other Nular commands 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 commands are extensive

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 command 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 understandible:

(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 some 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 :)


Binary Operators

Rules Of Precedence

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