Function
A function contains code which usually consists of an input, processing and output part. Functions were first introduced in the Operation Flashpoint: Resistance patch.
The main advantages of functions are:
- Improved legibility
- By writing the code once and re-using it by calling the function
- Easier debugging
- If your function contains an error, you only have to fix it in one place
Types of function
Functions-as-files
- See calling functions for how a function-as-file is called.
Functions as files are functions stored within a file. These are usually used for larger and more complex functions. The code is evaluated in the same way, however, there are additional commands which must include the file before the function itself can be called.
Inline functions
Inline functions are functions are technically code which is often stored within a variable or declared as a function parameter. Inline functions operate the same way as functions-as-files as both are evaluated in the same way, but the difference is that inline functions are stored within parentheses {}, whereas functions-as-files do not require these.
Anatomy of a function
When scripting, there are two types of functions: functions-as-files and inline functions. Functions-as-files are instances where the a whole file itself is used to house a function, whereas inline functions are either contained within a variable or as a parameter of a function. Some built-in functions require functions-as-files, whereas most will support both.
Parameters (input)
Parameters for functions are available to the function via the magic variable _this. Declaration of parameters can be done using the params command. Common practice for defining parameters is done via the use of private variables and defined variables.
Parameters passed to functions are passed before the function, rather than after (such as the mathematical or c-like syntax f(x)).
Return Values (output)
The value of the last executed statement in a function is returned to the calling instance.
In the first example "_this + 1" is the last executed statement in my_fnc, in the second example it is "_this - 1".
More examples:
Execution
Functions can be executed from several points in the game:
- Other scripts
- Other functions
- Scripting lines in the Mission Editor
- Event Handlers in addon config files
Functions are first loaded as String from a file via preprocessFile or loadFile. They are then executed via the call or spawn command. Since Armed Assault the loaded String needs to be compiled in order to convert it to Code, which is required for call or spawn.
Call
Example (Operation Flashpoint):
Example (Armed Assault):
Functions executed using call are run within the executing instance, which waits for the result of the function. Unlike scripts, functions halt all other game engine processes until the function has completed its instructions. This means functions run faster than scripts, and the result of functions is immediate and unambiguous. It can also mean that if a function takes too long to run it will have an adverse effect on game play - large functions or CPU intensive functions can cause the game to seize up until it completes. When creating a functions you want the function to be short and sweet to achieve the best results.
Spawn
Functions may also be executed using spawn, but then the function result is not accessible, making it behave more like a procedure. Spawned functions will run asynchronously or alongside the executing instance. This helps prevent large CPU intensive functions from seizing up the game.
Example (Armed Assault):
Examples
Example 1: max.sqf
In this example the function returns maximum of first and second argument.
max.sqf
alternative max.sqf (big boys code :))
executing script:
Example 2: infantrySafe.sqf
In this example the function returns no value and switches all units to safe mode.
Example 3: Inline Function
An inline-function can be created in any script:
This function can then be called (in other scripts, functions, unit's init lines, trigger activation fields, etc.) via:
In case the function doesn't require any arguments you can just call the function.