Difference between revisions of "Arma 3: Functions Library"

From Bohemia Interactive Community
Jump to navigation Jump to search
m (Comments belongs to discuss page)
m (Fix typo)
(78 intermediate revisions by 11 users not shown)
Line 1: Line 1:
[[Category:Arma 3: Editing]]
+
{{TOC|side||3}}
 +
{{arma3}}'s '''Functions Library''' is the way to declare mission, campaign or addon's [[Function]]s. The main difference from older [[:Category:Functions Library|Function Libraries]] is that it runs automatically and does not require a Functions module.
  
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.
+
The advantages of using the Functions Library over e.g [[execVM]] an [[SQF Syntax|SQF]] file are as follow:
 
+
# '''Automatic compilation upon mission start''' into a global variable - no need to remember direct paths to files.
== Finding a Function ==
+
# '''Anti-hack protection''' using [[compileFinal]] (see {{HashLink|#Recompiling}})
[[File:A3_functionwViewer.png|300px|thumb|right|Functions Viewer]]
+
# '''Listing in the [[Arma 3: Functions Viewer|Functions Viewer]]'''
Before you can use a function, you first need to find it. The easiest way is to access the '''Functions Viewer''':
+
# '''Advanced debugging options'''
 
+
# '''Optional immediate execution upon mission start''', without need for manual call
* In editor, click on [[File:icon editor functions.png|20px]] icon or press Ctrl + F
+
# '''Potential performance improvements'''
* In mission, access the [[Mission_Editor:_Debug_Console|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.
 
 
 
{{note|Some functions, especially the older ones, may have missing or incomplete headers.<br />We do our best to update them, but if you find some issues, feel free to report them in the [http://feedback.arma3.com/ Feedback Tracker].}}
 
  
  
Selected functions have also detailed description here on Community Wiki, see [[:Category:Arma_3:_Functions|Arma 3 Functions]] category.
+
{{Feature|important|This page is about {{arma3}} Functions Library.
 +
* For {{arma2}}/{{Name|arma2oa|short}}, see [[Arma 2: Functions Library]].
 +
* For {{tkoh}}, see [[Take On Helicopters: Functions Library]].
 +
}}
  
== 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 ===
+
== Function Declaration ==
Arguments are data sent into the function, affecting its behavior.
 
  
They can be '''mandatory''' or '''optional'''.
+
Functions are configured within the <tt>CfgFunctions</tt> class.
* 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.
 
  
{{note|Within the function, '''arguments''' are called '''parameters''', or simply params.}}
+
Mission and campaign specific functions can be configured in [[Description.ext]]/[[Campaign Description.ext]], while addon functions are defined in [[Config.cpp]].
 +
Configuration structure is the same in both cases.
  
  
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:
+
{{Feature|informative|The root directory (noted as '''&lt;ROOT&gt;''' on this page) is the origin from which the game will try to load function files. It depends on <tt>CfgFunctions</tt>' location:
/*
+
* if [[Description.ext]], '''&lt;ROOT&gt;''' means the [[Mission Editor: External#Mission Folder|mission's root]] directory
Author: Karel Moricky
+
* if [[Campaign Description.ext]], '''&lt;ROOT&gt;''' means the campaign's root directory
+
* if in config, '''&lt;ROOT&gt;''' means the '''game'''<nowiki/>'s root directory. The addon path needs to be set manually.
Description:
+
}}
Ends mission with specific ending.
 
 
<span style="color:DarkCyan;">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</span>
 
 
Returns:
 
BOOL
 
*/
 
  
As you can see, all arguments are marked optional and you can call the function without them.
 
  
[] call BIS_fnc_endMission;
+
See a basic example config:
:This will result in successfull ending of type "end1", preceeded with the signature [[Debriefing|closing shot]].
+
  class '''CfgFunctions'''
 
 
["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.
 
 
 
<nowiki>[</nowiki>[[nil]],[[nil]],false] call BIS_fnc_endMission;
 
:Disable the closing effects, but keep the other aguments intact (successful "end1").
 
 
 
{{note|Some older functions are incompatible with this syntax and using nil would break them. See their header for more information.}}
 
 
 
=== 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
 
 
<span style="color:DarkCyan;">Returns:
 
STRING</span>
 
*/
 
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 <span style="color:green;">myTag</span>
+
  class {{Color|purple|TAG}}
 
  {
 
  {
};
+
class {{Color|darkorange|Category}}
class Anything
+
  {
{
+
class {{Color|teal|functionName}} {};
tag = "<span style="color:green;">myTag</span>"; {{codecomment|// Custom tag name}}
+
};
  requiredAddons[] = {"A3_Data_F"}; {{codecomment|// Optional requirements of CfgPatches classes. When some addons are missing, functions won't be compiled.}}
 
 
  };
 
  };
 
  };
 
  };
 +
* The function's name will be '''{{Color|purple|TAG}}'''_fnc_'''{{Color|teal|functionName}}'''
 +
* The function will be loaded:
 +
** from config: <tt>'''&lt;ROOT&gt;'''\{{Color|darkorange|Category}}\'''fn_'''{{Color|teal|functionName}}'''.sqf'''</tt>
 +
** from description.ext: <tt>'''&lt;ROOT&gt;'''\'''Functions'''\{{Color|darkorange|Category}}\'''fn_'''{{Color|teal|functionName}}'''.sqf'''</tt>
 +
 +
{{Feature|informative|'''config.cpp only''': Anywhere outside of running mission, refer to the functions stored in [[uiNamespace]]. e.g:
 +
<code>''arguments'' [[call]] ([[uiNamespace]] [[getVariable]] ''"{{Color|purple|TAG}}_fnc_{{Color|teal|functionName}}"'');</code>}}
 +
 +
=== Config Levels ===
 +
 +
A '''CfgFunctions''' config is made of three levels: Tag, Category, and Function.
  
=== Path ===
+
==== Tag ====
==== File Path ====
+
To prevent duplicates, every author must create a subclass with a unique {{Color|purple|[[OFPEC tags|tag]]}} and create functions under it.
The easiest and the most transparent way is to set path for each function.
+
The tag name will be used when composing a function name (e.g [[BIS_fnc_spawnGroup|'''BIS'''_fnc_spawnGroup]]).
 
  class CfgFunctions
 
  class CfgFunctions
 
  {
 
  {
  class <span style="color:green;">myTag</span>
+
  class {{Color|purple|TAG}}
 
  {
 
  {
  class myCategory
+
  class Category
 
  {
 
  {
  class <span style="color:teal;">myFunction</span> {file = "<span style="color:DarkOrange;">myFile.sqf</span>";};
+
  class myFunction {};
 
  };
 
  };
 
  };
 
  };
  };
+
   
This will try to compile function '''<big><span style="color:green;">myTag</span>_fnc_<span style="color:teal;">myFunction</span></big>''' from the following file:
+
  class TAG_WeaponManagement
''%ROOT%''\<span style="color:DarkOrange;">myFile.sqf</span>
 
Where ''%ROOT%'' is either '''[[Mission_Editor:_External#Mission_Folder|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., <span style="color:DarkOrange;">myAddon\myFile.sqf</span>).
 
 
 
==== 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 <span style="color:green;">myTag</span>
 
 
  {
 
  {
  class myCategory
+
'''tag''' = {{Color|purple|"TAG"}}; {{cc|the function will be named '''{{Color|purple|TAG}}_fnc_myOtherFunction'''}}
 +
  class Category
 
  {
 
  {
file = "<span style="color:DarkOrange;">myPath</span>";
+
  class myOtherFunction {};
  class <span style="color:teal;">myFunction</span> {};
 
 
  };
 
  };
 
  };
 
  };
 
  };
 
  };
Compile function '''<big><span style="color:green;">myTag</span>_fnc_<span style="color:teal;">myFunction</span></big>''' from the following file:
 
''%ROOT%''\<span style="color:DarkOrange;">myPath</span>\fn_<span style="color:teal;">myFunction</span>.sqf
 
<span style="color:DarkOrange;">myPath</span> can be a folder or multiple folders, e.g., <span style="color:DarkOrange;">myFolder\mySubfolder</span>
 
  
==== Default Path (Mission Only) ====
+
===== Attributes =====
In a mission, you can leave also the folder path undefined and let functions be loaded from the default directory.
+
====== tag ======
 +
The <tt>tag</tt> attribute can override the config {{Color|green|tag}} set differently.
 +
 
 +
==== Category ====
 +
The category name changes the function's category in the [[Arma 3: Functions Viewer|Functions Viewer]]. It does not change the function's name, only the loading path.
 
  class CfgFunctions
 
  class CfgFunctions
 
  {
 
  {
  class <span style="color:green;">myTag</span>
+
  class TAG
 
  {
 
  {
  class <span style="color:crimson;">myCategory</span>
+
  class {{Color|darkorange|Category}}
 
  {
 
  {
  class <span style="color:teal;">myFunction</span> {};
+
  class myFunction {};
 +
};
 +
 +
class OtherCategory
 +
{
 +
'''file''' = "{{Color|darkorange|My}}\{{Color|darkorange|Category}}\{{Color|darkorange|Path}}";
 +
class myFunction {}; {{cc|file path will be '''&lt;ROOT&gt;'''\{{Color|darkorange|My}}\{{Color|darkorange|Category}}\{{Color|darkorange|Path}}\fn_myFunction.sqf";}}
 +
};
 +
 +
class DataCategory
 +
{
 +
'''requiredAddons[]''' = {{Color|darkorange|{ "A3_Data_F" }<nowiki/>}}; {{cc|Optional requirements of [[CfgPatches]] classes. If some addons are missing, category functions will not be compiled.}}
 +
class myDataFunction {};
 
  };
 
  };
 
  };
 
  };
 
  };
 
  };
Compile function '''<big><span style="color:green;">myTag</span>_fnc_<span style="color:teal;">myFunction</span></big>''' from the following file:
 
''%ROOT%''\functions\<span style="color:crimson;">myCategory</span>\fn_<span style="color:teal;">myFunction</span>.sqf
 
  
=== Attributes ===
+
===== Attributes =====
Apart from already mentioned ''file'', function class can have additional attributes:
+
====== file ======
 +
The <tt>file</tt> attribute can override the category's loading path segment.
 +
 
 +
====== requiredAddons ======
 +
The category can skip loading if a required addon is missing by setting its dependency with the <tt>requiredAddons</tt> attribute.
 +
 
 +
==== Function ====
 +
{{Feature|informative|By convention, the function class should be named in '''camelCase''' and not have its first letter capitalised (e.g PascalCase):
 +
* {{Icon|checked}} TAG_fnc_{{Color|teal|myFunction}}
 +
* {{Icon|unchecked}} TAG_fnc_{{Color|teal|MyFunction}}
 +
* {{Icon|checked}} TAG_fnc_{{Color|teal|POWRescue}}
 +
}}
 
  class CfgFunctions
 
  class CfgFunctions
 
  {
 
  {
  class myTag
+
  class TAG
 
  {
 
  {
  class myCategory
+
  class Category1
 +
{
 +
class {{Color|teal|myFunction}} {};
 +
};
 +
 +
class Category2
 
  {
 
  {
  class <span style="color:teal;">myFunction</span>
+
  file = "Path\To\Category";
 +
class myFunction
 
  {
 
  {
  preInit = 1; {{codecomment|// 1 to call the function upon mission start, <u>before</u> objects are initialized. Passed arguments are ["preInit"]}}
+
  '''file''' = "{{Color|teal|My}}\{{Color|teal|Function}}\{{Color|teal|Filepath.sqf}}"; {{cc|file path will be '''&lt;ROOT&gt;'''\{{Color|teal|My}}\{{Color|teal|Function}}\{{Color|teal|Filepath.sqf}}", ignoring "Path\To\Category"}}
postInit = 1; {{codecomment|// 1 to call the function upon mission start, <u>after</u> objects are initialized. Passed arguments are ["postInit"]}}
+
};
  recompile = 1; {{codecomment|// 1 to recompile the function upon mission start}}
+
  ext = ".fsm"; {{codecomment|// Set file type, can be ".sqf" or ".fsm" (meaning scripted FSM). Default is ".sqf".}}
+
class {{Color|teal|myFSMFunction}}
 +
{
 +
  '''preInit''' = 1;
 +
'''postInit''' = 1;
 +
  '''ext''' = ".fsm";
 +
'''preStart''' = 1;
 +
'''recompile''' = 1;
 
  };
 
  };
 
  };
 
  };
 
  };
 
  };
 
  };
 
  };
''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.
 
  
=== Initialization Order ===
+
===== Attributes =====
When mission is starting, its components are initialized in the following order:
+
{| class="wikitable sortable"
# Functions with ''recompile'' param set to 1 are recompiled
+
! rowspan="2" | Attribute
# Functions with ''preInit'' param set to 1 are called
+
! rowspan="2" | Description
# Object [[Arma_3:_Event_Handlers#Init|''Init'' Event Handlers]] are called
+
! colspan="2" | Available in
# Object initialization fields are called
+
|- style="font-size: 0.9em"
# [[Event_Scripts|''init.sqs'']] is executed in singleplayer
+
! [[Description.ext]]
# [[Event_Scripts|''init.sqf'']] is executed in singleplayer
+
! [[config.cpp]]
# Persistent [[#Multiplayer|multiplayer functions]] are called (client only)
+
|-
# [[Arma_3_Modules|Modules]] are initialized
+
| file
# [[Event_Scripts|''initServer.sqf'']] is executed (server only)
+
| the <tt>file</tt> attribute can be used to manually set the file path.
# [[Event_Scripts|''initPlayerLocal.sqf'']] is executed
+
{{Feature|important|The '''file''' entry '''overrides''' Category's set path and ignores it entirely (they are not merged).}}
# [[Event_Scripts|''initPlayerServer.sqf'']] is executed (server only)
+
| style="text-align: center" | {{Icon|checked}}
# Functions with ''postInit'' param set to 1 are called
+
| style="text-align: center" | {{Icon|checked}}
# "''BIS_fnc_init''" variable is set to true
+
|-
# [[Event_Scripts|''init.sqs'']] is executed in multiplayer
+
| preInit
# [[Event_Scripts|''init.sqf'']] is executed in multiplayer
+
| the <tt>preInit</tt> attribute (formerly known as "forced") can be set to 1 to call the function upon mission start, before objects are initialized.<br>Passed arguments are {{ic|["preInit"]}}. The function is run in an '''[[Scheduler#Unscheduled Environment|unscheduled environment]]'''.
 
+
| style="text-align: center" | {{Icon|checked}}
== Writing a Function ==
+
| style="text-align: center" | {{Icon|checked}}
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.
+
|-
 
+
| postInit
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.
+
| the <tt>postInit</tt> attribute can be set to 1 to call the function upon mission start, after objects are initialized.<br>Passed arguments are {{ic|["postInit", [[didJIP]]]}}. The function is run in a '''[[Scheduler#Scheduled Environment|scheduled environment]]''' so suspension is allowed, but '''any long term suspension will halt the mission loading until suspension is done'''.
{{Important|What is not documented, does not exist!}}
+
| style="text-align: center" | {{Icon|checked}}
 
+
| style="text-align: center" | {{Icon|checked}}
=== 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.
+
| ext
 
+
| the <tt>ext</tt> attribute can be used to set function file's type; it can be one of:
We have this very simple function which will let a unit watch a position:
+
* ".sqf" (default)
_unit = <span style="color:green;">_this</span> [[select]] <span style="color:darkorange;">0</span>;
+
* ".fsm" (see [[FSM]])
_target = <span style="color:green;">_this</span> select <span style="color:darkorange;">1</span>;
+
| style="text-align: center" | {{Icon|checked}}
_unit [[doWatch]] _target;
+
| style="text-align: center" | {{Icon|checked}}
 
+
|-
Expected way how to call the function is by correctly defining all arguments:
+
| preStart
{|
+
| the <tt>preStart</tt> attribute can be set to 1 to call the function upon game start, before title screen, but after all addons are loaded.
| [[File:Ico_ok.png]]
+
| style="text-align: center" | {{Icon|unchecked}}
|
+
| style="text-align: center" | {{Icon|checked}}
  [player,position myCar] call myTag_fnc_myFunction;
+
|-
 +
| recompile
 +
| the <tt>recompile</tt> attribute can be set to 1 to recompile the function upon mission start (functions in [[Description.ext]] are always compiled upon mission (re)start)
 +
| style="text-align: center" | {{Icon|unchecked}}
 +
| style="text-align: center" | {{Icon|checked}}
 +
|-
 +
| headerType
 +
| the <tt>headerType</tt> adds code to the beginning of functions that can:
 +
* Set up the [[#Meta Variables|Meta Variables]] _fnc_scriptParent and _fnc_scriptName
 +
* Name the current scope via the command [[scriptName]]
 +
* Adds debug information by saving the current execution order in the [[Array]] _fnc_scriptMap.<br/>
 +
Possible values are:
 +
* -1: No header - Adds nothing, literally adds a blank [[String]] to the beginning of functions. The functions will have no debug or {{HashLink|#Meta Variables}} assigned for it and the scope will not be named. Useful to reduce the performance overhead or if custom debug system is used.
 +
* 0: Default header - The default header changes based on the current [[#Debug Modes|Debug Mode]]
 +
* 1: System header - Incorporates the [[#Meta Variables|Meta Variables]] _fnc_scriptParent. Also names the current scope.
 +
[[private]] _fnc_scriptNameParent = [[if]] ([[isNil]] '_fnc_scriptName') [[then]] { '%1' } [[else]] { _fnc_scriptName };
 +
  [[scriptName]] '%1';
 +
See the following section for more information.
 +
| style="text-align: center" | {{Icon|checked}}
 +
| style="text-align: center" | {{Icon|checked}}
 
|}
 
|}
  
However, the function will break down when you try to send only one argument in:
+
{{Feature|important|Notes about <tt>preInit</tt> and <tt>postInit</tt> defined in '''config.cpp''':
{|
+
* these attributes will make the function run '''on each, every and any mission start'''
| [[File:Ico_none.png]]
+
* Any scripting error will prevent the mission from being loaded correctly
|
+
* Server admins might blacklist the whole addon if they find out the function is used for hacking
[player] call myTag_fnc_myFunction;
+
}}
|}
 
 
 
Furthermore, using wrong data type will also lead to a problem:
 
{|
 
| [[File:Ico_none.png]]
 
|
 
[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:
 
{|
 
| [[File:Ico_none.png]]
 
|
 
[player,[1,2,3,4]] call myTag_fnc_myFunction;
 
|}
 
  
 +
== Advanced Knowledge ==
  
As you can see there, the most common problems are:
+
=== Meta Variables ===
# '''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 = [<span style="color:green;">_this</span>, <span style="color:darkorange;">0</span>, <span style="color:teal;">objNull</span>, <span style="color:crimson;">[objNull]</span>] call BIS_fnc_param;
 
_target = [<span style="color:green;">_this</span>, <span style="color:darkorange;">1</span>, <span style="color:teal;">[0,0,0]</span>, <span style="color:crimson;">[[],objNull]</span>, <span style="color:indigo;">[2,3]</span>] call BIS_fnc_param;
 
_unit doWatch _target;
 
* First two arguments are the same as you used when selecting the param manually - <span style="color:green;">'''source array'''</span> and <span style="color:darkorange;">'''index'''</span> number.
 
* Third argument is the <span style="color:teal;">'''default value'''</span>. It will be used when the argument is missing, is [[nil]] or when wrong data type is used.
 
* Next is optional <span style="color:crimson;">'''array of compatible data types'''</span>. 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 <span style="color:indigo;">'''array of required array sizes'''</span>. [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:
 
{|
 
| [[File:Ico_ok.png]]
 
|
 
[player] call myTag_fnc_myFunction;
 
|}
 
: ''_target'' is undefined. Default <span style="color:teal;">[0,0,0]</span> is used instead. No error message is logged.
 
 
 
{|
 
| [[File:Ico_ok.png]]
 
|
 
[nil,position myCar] call myTag_fnc_myFunction;
 
|}
 
: ''_unit'' is undefined (nil is passed instead). Default <span style="color:teal;">objNull</span> is used instead. No error message is logged.
 
 
 
{|
 
| [[File:Ico_warning.png]]
 
|
 
[player,0] call myTag_fnc_myFunction;
 
|}
 
: ''_target'' has wrong <span style="color:crimson;">type</span>. Default <span style="color:teal;">[0,0,0]</span> is used instead. Error message is logged.
 
 
 
{|
 
| [[File:Ico_warning.png]]
 
|
 
[player,[1,2,3,4]] call myTag_fnc_myFunction;
 
|}
 
: ''_target'' has wrong <span style="color:indigo;">size</span>. Default <span style="color:teal;">[0,0,0]</span> is used instead. 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.
 
{|
 
| [[File:Ico_ok.png]]
 
|
 
player call myTag_fnc_myFunction;
 
|}
 
  
=== Returning Value ===
+
* '''_fnc_scriptName''': [[String]] - Name of the script
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.
+
* '''_fnc_scriptNameParent''': [[String]] - Name of the parent script
_myVar = [player,position myCar] call myTag_fnc_myFunction;
+
* '''_fnc_scriptMap''': [[Array]] - List of all parent scripts
  
It's good practice to '''always''' return a value, even if it would be simple [[true]] marking the function as completed. Let's use the example function from above:
+
=== Debug Modes ===
_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 ===
+
Developers can access several debug modes using ''[[BIS_fnc_functionsDebug]]'' function. '''For this to work [[Description.ext#allowFunctionsRecompile|function recompiling]] has to be allowed!'''
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
 
Notice that we're returning [[false]] at the end of [[exitWith]] code.
 
{{Important|Error states must always return value of the same type as when everything is fine ([[Boolean]] in this case).}}
 
  
 +
==== Debug Mode 0 ====
 +
default - incorporates the [[#Meta Variables|Meta Variables]] _fnc_scriptParent and _fnc_scriptName and names the current scope.
 +
[[private]] _fnc_scriptNameParent = [[if]] ([[isNil]] '_fnc_scriptName') [[then]] {'%1'} [[else]] {_fnc_scriptName};
 +
[[private]] _fnc_scriptName = '%1';
 +
[[scriptName]] _fnc_scriptName;
  
BIS_fnc_error accepts [[String]] and [[Array]] of [[format]]ted ext. The error is logged into [[RPT]] and if the mission is previewd from the editor, it will also appear on screen.
+
==== Debug Mode 1 ====
{|
+
As per ''Debug Mode 0'' and also saves the current execution order in the [[Array]] _fnc_scriptMap.
| [[RPT]]
+
[[private]] _fnc_scriptNameParent = [[if]] ([[isNil]] '_fnc_scriptName') [[then]] { '%1' } [[else]] { _fnc_scriptName };
|
+
[[private]] _fnc_scriptName = '%1';
  "User1/log: ERROR: [BIS_fnc_respawnTickets] #0: 0 is type SCALAR, must be NAMESPACE, SIDE, GROUP, OBJECT, BOOL. true used instead."
+
[[scriptName]] _fnc_scriptName;
|-
+
  [[private]] _fnc_scriptMap = [[if]] ([[isNil]] '_fnc_scriptMap') [[then]] {[_fnc_scriptName]} [[else]] {_fnc_scriptMap + [_fnc_scriptName]};
| In-game
 
| [[File:A3 functions error.jpg|800px|On-screen error]]
 
|}
 
  
=== Logging ===
+
==== Debug Mode 2 ====
Apart from errors, you can print any debug message you need. Use one of the following functions:
+
As per ''Debug Mode 1'' and also outputs execution order using [[textLogFormat]] ( textLogFormat is not available in the retail version of Arma3 ) is it available in Diagnostics exe?
* [[BIS_fnc_log]] - log a data of any type (e.g., [[String]], [[Number]], [[Object]], ...)
+
[[private]] _fnc_scriptNameParent = [[if]] ([[isNil]] '_fnc_scriptName') [[then]] { '%1' } [[else]] { _fnc_scriptName };
* [[BIS_fnc_logFormat]] - log [[format]]ted text
+
[[private]] _fnc_scriptName = '%1';
Profile name and function name will automatically appear in the output text, helping you identify the source.
+
[[scriptName]] _fnc_scriptName;
 +
[[private]] _fnc_scriptMap = [[if]] ([[isNil]] '_fnc_scriptMap') [[then]] { [_fnc_scriptName] } [[else]] { _fnc_scriptMap + [_fnc_scriptName] };
 +
[[textLogFormat]] ['%1 : %2', _fnc_scriptMap [[joinString]] ' >> ', _this];
  
  
Usage examples:
+
== See Also ==
{| class="wikitable"
 
! Expression
 
! [[RPT]] Output
 
|-
 
|
 
<span style="color:green;">"Hello World"</span> call BIS_fnc_log;
 
|
 
"User1/BIS_fnc_log: [myTag_fnc_myFunction] <span style="color:green;">Hello World</span>"
 
|-
 
|
 
<span style="color:green;">42</span> call BIS_fnc_log;
 
|
 
"User1/BIS_fnc_log: [myTag_fnc_myFunction] <span style="color:green;">42</span>"
 
|-
 
|
 
[<span style="color:green;">"I'm playing %1"</span>,[[missionName]]] call BIS_fnc_logFormat;
 
|
 
"User1/BIS_fnc_log: [myTag_fnc_myFunction] <span style="color:green;">I'm playing FalconWing</span>"
 
|}
 
  
 +
* [[Initialization Order]]
 +
* [[Arma 3: Functions Viewer]]
 +
* [[:Category:Arma 3: Functions|Arma 3 Functions]]
 +
* [[Arma 3: Writing a Function|Writing a Function Tutorial]]
  
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:
+
{{GameCategory|arma3|Editing}}
allowFunctionsLog = 1;
+
[[Category: Functions Library]]
 
 
 
 
=== 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 trigger 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;
 
 
 
"Recompile" button in the functions viewer will enabled only when recompiling is allowed.
 
 
 
=== 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)
 
<!--* '''_fnc_scriptMap''': ARRAY - List of all parent scripts (available only in debug mode 1 and higher, see [[#Debug_Mode|above]]).-->
 
{{Important|Do not modify these values!}}
 
 
 
<!--
 
=== Debug Mode ===
 
Developers can access several debug modes using ''[[BIS_fnc_functionsDebug]]'' function.
 
# '''No debug'''
 
#* Default
 
# '''Save script map'''
 
#* Variable ''_fnc_scriptMap'' tracking script execution progress is stored in function header
 
# '''Save and log script map'''
 
#* Variable ''_fnc_scriptMap'' tracking script execution progress is stored in functions header and it's printed to debug log.
 
{{Important|Function recompiling has to be allowed!}}
 
-->
 

Revision as of 22:41, 1 September 2021

Arma 3's Functions Library is the way to declare mission, campaign or addon's Functions. The main difference from older Function Libraries is that it runs automatically and does not require a Functions module.

The advantages of using the Functions Library over e.g execVM an SQF file are as follow:

  1. Automatic compilation upon mission start into a global variable - no need to remember direct paths to files.
  2. Anti-hack protection using compileFinal (see Recompiling)
  3. Listing in the Functions Viewer
  4. Advanced debugging options
  5. Optional immediate execution upon mission start, without need for manual call
  6. Potential performance improvements


This page is about Arma 3 Functions Library.


Function Declaration

Functions are configured within the CfgFunctions class.

Mission and campaign specific functions can be configured in Description.ext/Campaign Description.ext, while addon functions are defined in Config.cpp. Configuration structure is the same in both cases.


The root directory (noted as <ROOT> on this page) is the origin from which the game will try to load function files. It depends on CfgFunctions' location:


See a basic example config:

class CfgFunctions
{
	class TAG
	{
		class Category
		{
			class functionName {};
		};
	};
};
  • The function's name will be TAG_fnc_functionName
  • The function will be loaded:
    • from config: <ROOT>\Category\fn_functionName.sqf
    • from description.ext: <ROOT>\Functions\Category\fn_functionName.sqf
config.cpp only: Anywhere outside of running mission, refer to the functions stored in uiNamespace. e.g: arguments call (uiNamespace getVariable "TAG_fnc_functionName");

Config Levels

A CfgFunctions config is made of three levels: Tag, Category, and Function.

Tag

To prevent duplicates, every author must create a subclass with a unique tag and create functions under it. The tag name will be used when composing a function name (e.g BIS_fnc_spawnGroup).

class CfgFunctions
{
	class TAG
	{
		class Category
		{
			class myFunction {};
		};
	};

	class TAG_WeaponManagement
	{
		tag = "TAG"; // the function will be named TAG_fnc_myOtherFunction
		class Category
		{
			class myOtherFunction {};
		};
	};
};
Attributes
tag

The tag attribute can override the config tag set differently.

Category

The category name changes the function's category in the Functions Viewer. It does not change the function's name, only the loading path.

class CfgFunctions
{
	class TAG
	{
		class Category
		{
			class myFunction {};
		};

		class OtherCategory
		{
			file = "My\Category\Path";
			class myFunction {}; // file path will be <ROOT>\My\Category\Path\fn_myFunction.sqf";
		};

		class DataCategory
		{
			requiredAddons[] = { "A3_Data_F" }; // Optional requirements of CfgPatches classes. If some addons are missing, category functions will not be compiled.
			class myDataFunction {};
		};
	};
};
Attributes
file

The file attribute can override the category's loading path segment.

requiredAddons

The category can skip loading if a required addon is missing by setting its dependency with the requiredAddons attribute.

Function

By convention, the function class should be named in camelCase and not have its first letter capitalised (e.g PascalCase):
  • Ico ok.png TAG_fnc_myFunction
  • Ico none.png TAG_fnc_MyFunction
  • Ico ok.png TAG_fnc_POWRescue
class CfgFunctions
{
	class TAG
	{
		class Category1
		{
			class myFunction {};
		};

		class Category2
		{
			file = "Path\To\Category";
			class myFunction
			{
				file = "My\Function\Filepath.sqf"; // file path will be <ROOT>\My\Function\Filepath.sqf", ignoring "Path\To\Category"
			};

			class myFSMFunction
			{
				preInit		= 1;
				postInit	= 1;
				ext			= ".fsm";
				preStart	= 1;
				recompile	= 1;
			};
		};
	};
};
Attributes
Attribute Description Available in
Description.ext config.cpp
file the file attribute can be used to manually set the file path.
The file entry overrides Category's set path and ignores it entirely (they are not merged).
Ico ok.png Ico ok.png
preInit the preInit attribute (formerly known as "forced") can be set to 1 to call the function upon mission start, before objects are initialized.
Passed arguments are ["preInit"]. The function is run in an unscheduled environment.
Ico ok.png Ico ok.png
postInit the postInit attribute can be set to 1 to call the function upon mission start, after objects are initialized.
Passed arguments are ["postInit", didJIP]. The function is run in a scheduled environment so suspension is allowed, but any long term suspension will halt the mission loading until suspension is done.
Ico ok.png Ico ok.png
ext the ext attribute can be used to set function file's type; it can be one of:
  • ".sqf" (default)
  • ".fsm" (see FSM)
Ico ok.png Ico ok.png
preStart the preStart attribute can be set to 1 to call the function upon game start, before title screen, but after all addons are loaded. Ico none.png Ico ok.png
recompile the recompile attribute can be set to 1 to recompile the function upon mission start (functions in Description.ext are always compiled upon mission (re)start) Ico none.png Ico ok.png
headerType the headerType adds code to the beginning of functions that can:
  • Set up the Meta Variables _fnc_scriptParent and _fnc_scriptName
  • Name the current scope via the command scriptName
  • Adds debug information by saving the current execution order in the Array _fnc_scriptMap.

Possible values are:

  • -1: No header - Adds nothing, literally adds a blank String to the beginning of functions. The functions will have no debug or Meta Variables assigned for it and the scope will not be named. Useful to reduce the performance overhead or if custom debug system is used.
  • 0: Default header - The default header changes based on the current Debug Mode
  • 1: System header - Incorporates the Meta Variables _fnc_scriptParent. Also names the current scope.
private _fnc_scriptNameParent = if (isNil '_fnc_scriptName') then { '%1' } else { _fnc_scriptName };
scriptName '%1';

See the following section for more information.

Ico ok.png Ico ok.png
Notes about preInit and postInit defined in config.cpp:
  • these attributes will make the function run on each, every and any mission start
  • Any scripting error will prevent the mission from being loaded correctly
  • Server admins might blacklist the whole addon if they find out the function is used for hacking


Advanced Knowledge

Meta Variables

  • _fnc_scriptName: String - Name of the script
  • _fnc_scriptNameParent: String - Name of the parent script
  • _fnc_scriptMap: Array - List of all parent scripts

Debug Modes

Developers can access several debug modes using BIS_fnc_functionsDebug function. For this to work function recompiling has to be allowed!

Debug Mode 0

default - incorporates the Meta Variables _fnc_scriptParent and _fnc_scriptName and names the current scope.

private _fnc_scriptNameParent = if (isNil '_fnc_scriptName') then {'%1'} else {_fnc_scriptName};
private _fnc_scriptName = '%1';
scriptName _fnc_scriptName;

Debug Mode 1

As per Debug Mode 0 and also saves the current execution order in the Array _fnc_scriptMap.

private _fnc_scriptNameParent = if (isNil '_fnc_scriptName') then { '%1' } else { _fnc_scriptName };
private _fnc_scriptName = '%1';
scriptName _fnc_scriptName;
private _fnc_scriptMap = if (isNil '_fnc_scriptMap') then {[_fnc_scriptName]} else {_fnc_scriptMap + [_fnc_scriptName]};

Debug Mode 2

As per Debug Mode 1 and also outputs execution order using textLogFormat ( textLogFormat is not available in the retail version of Arma3 ) is it available in Diagnostics exe?

private _fnc_scriptNameParent = if (isNil '_fnc_scriptName') then { '%1' } else { _fnc_scriptName };
private _fnc_scriptName = '%1';
scriptName _fnc_scriptName;
private _fnc_scriptMap = if (isNil '_fnc_scriptMap') then { [_fnc_scriptName] } else { _fnc_scriptMap + [_fnc_scriptName] };
textLogFormat ['%1 : %2', _fnc_scriptMap joinString ' >> ', _this];


See Also