|
|
(11 intermediate revisions by 4 users not shown) |
Line 1: |
Line 1: |
| '''Description:'''
| | #REDIRECT [[Function]] |
| | |
| While script syntax (see [[exec]]) is line based, functions (see [[call]], [[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>
| |
| 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.<br>
| |
| This can be nothing when a function returns no value.
| |
| | |
| | |
| <b>Example 1</b> (max.sqf)
| |
| | |
| comment "Return maximum of first and second argument";<br>
| |
| [[private]] {"_a","_b"};<br>
| |
| _a = _this [[select]] 0;<br>
| |
| _b = _this [[select]] 1;<br>
| |
| if (_a>_b) [[then]] {_a} [[else]] {_b}
| |
| | |
| | |
| <b>Example 2</b> (infantrySafe.sqf)
| |
| | |
| comment "Switch all infantry units to safe mode";<br>
| |
| {<br>
| |
| [[if]] ([[vehicle]] _x == _x) [[then]]<br>
| |
| {<br>
| |
| _x [[setBehaviour]] "safe"<br>
| |
| }<br>
| |
| } [[forEach]] _this
| |
| | |
| | |
| Due to line-based nature of 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>
| |
| Recommended file extension for functions is '''.SQF''' (as opposed to '''.SQS''' used for scripts).
| |
| | |
| | |
| [[Category: Scripting_Topics ]] | |