Difference between revisions of "Function"

From Bohemia Interactive Community
Jump to navigation Jump to search
m (Introduction)
Line 66: Line 66:
[[Category: Scripting_Topics ]]
[[Category: Scripting_Topics]]
[[Category: File extensions | SQF]]

Revision as of 21:13, 9 November 2006


Functions were first introduced into a OFP: Resistance patch. What is a function you might ask? A function is much like a regular scripting command except that you can fine tune the function to suit your needs kind of like a customized command. A function is simply a chunk of code that does something, the function can then return a value to the point which 'called' that function or it can simply return Nothing.


Function files are denoted with the file extension .SQF (as opposed to .SQS used for scripts)



While Sqs script syntax is line based, functions (see call, execVM, compile, then, do) are based on structured expressions. 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.

Functions in ArmA

Starting with Armed Assault there exist scripts which use the same structured expression syntax as functions, but can wait suspended, using sleep or waitUntil. While SQS scripts still work, they are considered deprecated and wherever possible, structured syntax scripts should be used instead.

Functions in OFP

In Operation Flashpoint .sqs scripts could 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.

Language Constructs

Main language contructs used in functions are:

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


This can be Nothing when a function returns no value.



comment "Return maximum of first and second argument"
private {"_a","_b"};
_a = _this select 0;
_b = _this select 1;

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).