From Bohemia Interactive Community
Jump to navigation Jump to search
The information on this page is very outdated and often misleading. The content of this page most likely will radically change in the future.



Types of function


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.


// Code here.. 

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.

//InlineFunction as Variable
MyVariableName ={
   // Code here..
// InlineFunction as command/function parameter
_myTemporaryVariable =player addEventHandler ["killed", {diag_log "I am providing an inline function (code) to this builtin command! Hurray for science!"}];

Anatomy of a function



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.

params ["_parameterOne", "_parameterTwo"];
//Inline Function
myInlineFunction ={
    params ["_parameterOne", "_parameterTwo"];

Parameters passed to functions are passed before the function, rather than after (such as the mathematical or c-like syntax f(x)).

//Array variable as parameter
_myTempParams = [_parameterOne, _parameterTwo];
_myTempVariableTwo = _myTempParams call myInlineFunction;
//Array as parameter
_myTempVariable = [_parameterOne,_parameterTwo] call myInlineFunction;

Return Values

The value of the last executed statement in a function is returned to the calling instance.

my_fnc = {
    if (_this > 0) exitWith {
        _this + 1
    _this - 1

hint str (5 call my_fnc); //6
hint str (-5 call my_fnc); //-6

In the first example "_this + 1" is the last executed statement in my_fnc, in the second example it is "_this - 1". Traditionally the returning statement is written without ";" after it. Have it or don't have it, it is up to you, doesn't make a blind bit of difference:

my_fnc = {
    a = 1;
    b = 2;
    c = a + b;
    c //<- fine

my_fnc = {
    a = 1;
    b = 2;
    c = a + b;
    c; //<- also fine

More examples:

private _myName = _this select 0;

private _returnMe = "FAIL";

if (_myName == "Test") then {
    _returnMe = "PASS";
myCodeReturnValue ={
    private _myName = _this select 0;
    private _returnMe = "FAIL";

    if (_myName == "Kaboom") then {
         _returnMe = "PASS";


_myCalledVariable = ["Kaboom"] call myCodeReturnValue; // "PASS"
_myCalledVariableFail = ["Blah"] call myCodeReturnValue; // "FAIL"
value = call compile preprocessFile "return.sqf";
// value is now RETURN_VALUE

call compile preprocessFile "return.sqf";
// valid, but RETURN_VALUE is not saved anywhere


Function Execution Diagram in scheduled environment

Executing Instance : script, function or game engine

Functions can be executed from several points in the game:



Example (Operation Flashpoint):

myFunction1 = loadFile "myFunction1.sqf";
myFunction2 = preprocessFile "myFunction2.sqf";

call myFunction1;
[1, 2] call myFunction2;

Example (Armed Assault):

myFunction1 = compile loadFile "myFunction1.sqf";
myFunction2 = compile preprocessFile "myFunction2.sqf";

_result1 = call myFunction1;
_result2 = [1, 2] call myFunction2;





Example (Armed Assault):

myFunction1 = compile loadFile "myFunction1.sqf";
myFunction2 = compile preprocessFile "myFunction2.sqf";

_param spawn myFunction1;
[1, 2] spawn myFunction2;


Example 1: max.sqf



//"Return maximum of first and second argument";
params ["_a", "_b"];
[_b, _a] select (_a > _b)

alternative max.sqf (big boys code :))

(_this select 0) max (_this select 1)

executing script:

fMax = compile preprocessFile "max.sqf";
maxValue = [3,5] call fMax;

//maxValue is now 5

Example 2: infantrySafe.sqf


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

Example 3: Inline Function

An inline-function can be created in any script:



name player call FNC_sayhello


call FNC_helloall

See also