Functions Library – Arma 3
Arma 3 Functions Library is pack of routine script functions available from anywhere in game. Main difference from older Functions Library is that it runs automatically and doesn't require Functions manager to be present.
Finding a Function
Before you can use a function, you first need to find it. The easiest way is to access the Functions Viewer:
- In editor, click on icon or press Ctrl + F
- In mission, access the debug console (automatically visible in pause menu of an editor mission) and click on FUNCTIONS button.
Once in the Functions Viewer, you can filter all available functions by location, projects and categories.
When you find the desired function, look at the code preview on the right. Every function has a header where you can find basic description of its functionality including required arguments, returned values and sometimes examples of use.
Selected functions have also detailed description here on Community Wiki, see Arma 3 Functions category.
Calling a Function
Functions can be launched in mission, intro and outro using this call or spawn commands:
_returnedValue = arguments call functionName; arguments spawn functionName;
Arguments
Arguments are data sent into the function, affecting its behavior.
They can be mandatory or optional.
- Mandatory arguments are required for function to run. When missing, the function usually stops and throws an error.
- Optional arguments allows more detailed configuration. If you dont send them, the function will use pre-defined default values.
For example, let's take a look at BIS_fnc_endMission, a function which ends a mission with animated closing shot. This is what the header says:
/*
Author: Karel Moricky
Description:
Ends mission with specific ending.
Parameter(s):
0 (Optional):
STRING - end name (default: "end1")
ARRAY in format [endName,ID], will be composed to "endName_ID" string
1 (Optional): BOOL - true to end mission, false to fail mission (default: true)
2 (Optional):
BOOL - true for signature closing shot (default: true)
NUMBER - duration of a simple fade out to black
Returns:
BOOL
*/
As you can see, all arguments are marked optional and you can call the function without them.
[] call BIS_fnc_endMission;
- This will result in successfull ending of type "end1", preceeded with the signature closing shot.
["end2"] call BIS_fnc_endMission;
- Set the ending type to "end2", while keeping the other arguments intact.
["end2",false,false] call BIS_fnc_endMission;
- Fail the mission without any effect, using "end2" type.
However, what should you do if you want to set the only last argument without affecting the previous ones? The solution is simple - put an empty variable nil on their place.
[nil,nil,false] call BIS_fnc_endMission;
- Disable the closing effects, but keep the other aguments intact (successful "end1").
Returned Value
Functions executed by call command can return back a value. Let's take a look at BIS_fnc_sideName:
/*
Author: Karel Moricky
Description:
Returns side name
Parameter(s):
0: SIDE or NUMBER - either side or side ID
Returns:
STRING
*/
The function returns a String - localized name of a side.
_westName = west call BIS_fnc_sideName;
- Variable _westName will now be "BLUFOR" (or other name, based on selected language)
Multiplayer
Functions executed using call or spawn command will run only on the computer which triggered them. If you'd wish to execute a function remotely on specific clients, use BIS_fnc_MP function.
[arguments,"functionName",target,isPersistent] call BIS_fnc_MP;
User Interface
Anywhere outside of running mission, refer to the functions stored in uiNamespace.
arguments call (uiNamespace getVariable "functionName");
Adding a Function
When writing a script, consider registering it into the Functions Library.
Main benefits uncludes:
- Automatic compilation upon mission start into a global variable - no need to remember direct paths to files.
- Anti-hack protection using compileFinal
- Listing in the Functions Viewer
- Advanced debugging options
- Optional immediate execution upon mission start, without need for manual call
Mission and campaign specific functions can be configured in Description.ext, while addon functions are defined in Config.cpp. Configuration structure is the same in both cases.
Tag
Functions are configured within CfgFunctions class. To prevent duplicities, every author must create a subclass with unique tag and place functions inside it. The tag name will be used when composing a function name.
class CfgFunctions { class myTag { }; class Anything { tag = "myTag"; // Custom tag name requiredAddons[] = {"A3_Data_F"}; // Optional requirements of CfgPatches classes. When some addons are missing, functions won't be compiled. }; };
Path
File Path
The easiest and the most transparent way is to set path for each function.
class CfgFunctions { class myTag { class myCategory { class myFunction {file = "myFile.sqf";}; }; }; };
Compile function myTag_fnc_myFunction from the following file:
%ROOT%\myFile.sqf
Where %ROOT% is either mission root (where mission.sqm file is), or the game root (path to an addon is not included and has to be part of the file path, e.g., myAddon\myFile.sqf).
Folder Path
You can set folder path and leave the function paths undefined. The functions will then be loaded from the folder.
class CfgFunctions { class myTag { class myCategory { file = "myPath"; class myFunction {}; }; }; };
Compile function myTag_fnc_myFunction from the following file:
%ROOT%\myPath\fn_myFunction.sqf
myPath can be a folder or multiple folders, e.g., myFolder\mySubfolder
Default Path (Mission Only)
In a mission, you can leave also the folder path undefine and let functions be loaded from the default directory.
class CfgFunctions { class myTag { class myCategory { class myFunction {}; }; }; };
This will try to compile function myTag_fnc_myFunction from the following file:
%ROOT%\functions\myCategory\fn_myFunction.sqf
Attributes
Apart from already mentioned file, function class can have additional attributes:
class CfgFunctions { class myTag { class myCategory { class myFunction { preInit = 1; // 1 to call the function upon mission start, before objects are initialized postInit = 1; // 1 to call the function upon mission start, after objects are initialized recompile = 1; // 1 to recompile the function upon mission start ext = ".fsm"; // Set file type, can be ".sqf" or ".fsm" (meaning scripted FSM). Default is ".sqf". }; }; }; };
preInit and postInit atrributes are truly powerful ones, as they let you execute your function at the beginning of every mission. Use them with caution!
- Any scripting error will prevent the mission from being loaded correctly
- Server admins might blacklist your addon if they find out you're using the function for hacking.
Writing a Function
The most important thing to remember when writing a function is that other people than you are going to use it. Most of them won't understand how does it work, expecting it to do its job without problems.
Your function must be robust. It shouldn't allow passing arguments of incorrect Data Types in. When some values are incorrect, it should throw an error explaining what went wrong and how to fix it. And above all, its header must provide complete explanation of usage.
Loading Parameters
Arguments are the only way how to interact with your function. Let's now see how to make sure they are loaded properly.
We have this very simple function which will let a unit watch a position:
_unit = _this select 0; _target = _this select 1; _unit doWatch _target;
Expected way how to call the function is by correctly defining all arguments:
[player,position myCar] call myTag_fnc_myFunction; |
However, the function will break down when you try to send only one argument in:
[player] call myTag_fnc_myFunction; |
Furthermore, using wrong data type will also lead to a problem:
[player,0] call myTag_fnc_myFunction; |
Variabe _target expects position array in format [x,y,z]. Scripting error will appear when different number of elements is used:
[player,[1,2,3,4]] call myTag_fnc_myFunction; |
As you can see there, the most common problems are:
- Param of wrong data type is sent
- Param is missing
- Param is an array expecting specific number of elements, but different number is sent
Rather than check for these exceptions yourself, you can use existing function BIS_fnc_param which will do it for you:
_unit = [_this, 0, objNull, [objNull]] call BIS_fnc_param; _target = [_this, 1, [0,0,0], [[],objNull], [2,3]] call BIS_fnc_param; _unit doWatch _target;
- First two arguments are the same as you used when selecting the param manually - source array and index number.
- Third argument is the default value. It will be used when the argument is incorrect.
- Next is optional array of compatible data types. They are defined by an example of the type, e.g. objNull will mean an object is allowed. When wrong data type is sent into your function, BIS_fnc_param will log an error message explaining what went wrong and use the default value.
- The last, also optional argument is an array of required array sizes. [2,3] means only array with 2 or 3 elements are allowed. When incorrectly large array is sent into your function, BIS_fnc_param will log an error message explaining what went wrong and use the default value.
Let's see what will happen when you try to use the wrong examples now:
[player] call myTag_fnc_myFunction; |
- _target is missing. Default [0,0,0] is used. No error message is logged.
[player,0] call myTag_fnc_myFunction; |
- _target has wrong type. Default [0,0,0] is used. Error message is logged.
[player,[1,2,3,4]] call myTag_fnc_myFunction; |
- _target has wrong size. Default [0,0,0] is used. Error message is logged.
Additionally, when only one argument is used, you can send it into the function directly without need to have it in an array.
player call myTag_fnc_myFunction; |
Returning Value
Users will often save result of your function to a variable. If no value is returned, the variable would be nil and could lead to script errors.
_myVar = [player,position myCar] call myTag_fnc_myFunction;
It's good practice to always return a value, even if it would be simple true denoting the function was completed. Let's use the example function from above:
_unit = [_this, 0, objNull, [objNull]] call BIS_fnc_param; _target = [_this, 1, [0,0,0], [[],objNull], [2,3]] call BIS_fnc_param; _unit doWatch _target; true
Showing Errors
While BIS_fnc_param can filter out the most common issues, sometimes your function will have special rules which will need to be handled. Let's return back to our example function, where we'd want to terminate the function with error when _unit is dead:
_unit = [_this, 0, objNull, [objNull]] call BIS_fnc_param; _target = [_this, 1, [0,0,0], [[],objNull], [2,3]] call BIS_fnc_param; if !(alive _unit) exitwith {["Unit %1 must be alive.",_unit] call BIS_fnc_error; false}; _unit doWatch _target; true
BIS_fnc_error accepts String and Array of formatted ext. The error is logged into RPT and if the mission is previewd from the editor, it will also appear on screen.
Example output:
RPT |
"User1/log: ERROR: [BIS_fnc_respawnTickets] #0: 0 is type SCALAR, must be NAMESPACE, SIDE, GROUP, OBJECT, BOOL. true used instead." |
In-game |
Logging
Apart from errors, you can print any debug message you need. Use one of the following functions:
- BIS_fnc_log - log a data of any type (e.g., String, Number, Object, ...)
- BIS_fnc_logFormat - log formatted text
Profile name and function name will automatically appear in the output text, helping you identify the source.
Usage examples:
Expression | RPT Output |
---|---|
"Hello World" call BIS_fnc_log;
|
"User1/BIS_fnc_log: [myTag_fnc_myFunction] Hello World"
|
42 call BIS_fnc_log;
|
"User1/BIS_fnc_log: [myTag_fnc_myFunction] 42"
|
["I'm playing %1",missionName] call BIS_fnc_logFormat; |
"User1/BIS_fnc_log: [myTag_fnc_myFunction] I'm playing FalconWing"
|
To prevent RPT spam, logging is by default enabled only when previewing a mission from the editor. To force it in the mission everywhere, use the following Description.ext attribute:
allowFunctionsLog = 1;
Recompiling
Once compiled, functions remain unchanged and editing their file won't have any effect in the game. To adjust functions on the fly, you can manually triger their recompilation.
1 call BIS_fnc_recompile;
- Recompiles all functions. Can be also achieved by clicking on RECOMPILE button in the Functions Viewer
"functionName" call BIS_fnc_recompile;
- Recompile the given function
As a security measure, functions are by default protected against rewriting during the mission. This restriction does not apply in missions previewed from the editor and in missions with the following attribute in Description.ext:
allowFunctionsRecompile = 1;
Meta Variables
System is adding header with basic meta data to all functions. Following local variables are declared there:
- _fnc_scriptName: String - Function name (e.g., myTag_fnc_myFunction)
- _fnc_scriptNameParent: String - Name of q function from which the current one was called (_fnc_scriptName used when not defined)