Functions - SQF: Difference between revisions

From Bohemia Interactive Community
Jump to navigation Jump to search
mNo edit summary
(Clarifications.)
Line 1: Line 1:
'''Description:'''
'''Description:'''


While script syntax (see [[exec]]) is line based, functions (see [[call]], [[execVM]], [[compile]], [[then]], [[do]]) are based on structured expressions and end-of-line has no special meaning, it is considered to be equivalent to space or semicolon and is therefore required even when ending line.
While Sqs script syntax (see [[exec]]) is line based, functions (see [[call]], [[execVM]], [[compile]], [[then]], [[do]]) are based on structured expressions and end-of-line has no special meaning, it is considered to be equivalent to space or semicolon and is therefore required even when ending line.




'''Note:''' Scripts can do some things that are not possible in functions.<br>
'''Note:''' Sqs Scripts can do some things that are not possible in functions.
 
Scripts can wait suspended until some condition is met, they can also use goto to change execution point at any time.
Scripts can wait suspended until some condition is met, they can also use goto to change execution point at any time.


Line 15: Line 16:




Result of the last expression evaluated is returned as a function result.<br>
Result of the last expression evaluated is returned as a function result.
 
This can be nothing when a function returns no value.
This can be nothing when a function returns no value.


Line 21: Line 23:
<b>Example 1</b> (max.sqf)
<b>Example 1</b> (max.sqf)


comment "Return maximum of first and second argument";<br>
comment "Return maximum of first and second argument";
[[private variableNameList|private]] {"_a","_b"};<br>
[[private variableNameList|private]] {"_a","_b"};
_a = _this [[select]] 0;<br>
_a = _this [[select]] 0;
_b = _this [[select]] 1;<br>
_b = _this [[select]] 1;
if (_a>_b) [[then]] {_a} [[else]] {_b}
if (_a>_b) [[then]] {_a} [[else]] {_b}




<b>Example 2</b> (infantrySafe.sqf)
<b>Example 2</b> (infantrySafe.sqf)


comment "Switch all infantry units to safe mode";<br>
comment "Switch all infantry units to safe mode";
{<br>
{
[[if]] ([[vehicle]] _x == _x) [[then]]<br>
  [[if]] ([[vehicle]] _x == _x) [[then]]
{<br>
  {
_x [[setBehaviour]] "safe"<br>
  _x [[setBehaviour]] "safe"
}<br>
  }
} [[forEach]] _this
} [[forEach]] _this




Due to line-based nature of scripts it is not possible to create multiline string constants in them.<br>
Due to line-based nature of Sqs scripts it is not possible to create multiline string constants in them.<br>
To overcome this limitation you can store multiline in separate files and load them using [[loadFile]] or [[preprocessFile]] functions (the second uses C-like preprocessor with // or /* */ comments and #define macros).<br>
To overcome this limitation you can store multiline in separate files and load them using [[loadFile]] or [[preprocessFile]] functions (the second uses C-like preprocessor with // or /* */ comments and #define macros).<br>
Recommended file extension for functions is '''.SQF''' (as opposed to '''.SQS''' used for scripts).  
Recommended file extension for functions is '''.SQF''' (as opposed to '''.SQS''' used for scripts).  

Revision as of 21:16, 19 July 2006

Description:

While Sqs script syntax (see exec) is line based, functions (see call, execVM, compile, then, do) are based on structured expressions and end-of-line has no special meaning, it is considered to be equivalent to space or semicolon and is therefore required even when ending line.


Note: Sqs Scripts can do some things that are not possible in functions.

Scripts can wait suspended until some condition is met, they can also use goto to change execution point at any time.


Main language contructs used in functions are:

  • if..then..else
  • while..do
  • Curled braces
  • Multiple commands (including assigment commands) are delimited with a semicolon.


Result of the last expression evaluated is returned as a function result.

This can be nothing when a function returns no value.


Example 1 (max.sqf)

comment "Return maximum of first and second argument";
private {"_a","_b"};
_a = _this select 0;
_b = _this select 1;
if (_a>_b) then {_a} else {_b}


Example 2 (infantrySafe.sqf)

comment "Switch all infantry units to safe mode";
{
 if (vehicle _x == _x) then
 {
  _x setBehaviour "safe"
 }
} forEach _this


Due to line-based nature of Sqs scripts it is not possible to create multiline string constants in them.
To overcome this limitation you can store multiline in separate files and load them using loadFile or preprocessFile functions (the second uses C-like preprocessor with // or /* */ comments and #define macros).
Recommended file extension for functions is .SQF (as opposed to .SQS used for scripts).