Functions Library – Arma 3
Lou Montana (talk | contribs) m (Fix typo) |
Lou Montana (talk | contribs) m (Text replacement - "[[OFPEC tags" to "[[Scripting Tags") |
||
(10 intermediate revisions by the same user not shown) | |||
Line 4: | Line 4: | ||
The advantages of using the Functions Library over e.g [[execVM]] an [[SQF Syntax|SQF]] file are as follow: | 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. | # '''Automatic compilation upon mission start''' into a global variable - no need to remember direct paths to files. | ||
# '''Anti-hack protection''' using [[compileFinal]] (see {{ | # '''Anti-hack protection''' using [[compileFinal]] (see {{Link|Arma 3: Writing a Function#Recompiling}} and [[BIS_fnc_recompile]]) | ||
# '''Listing in the [[Arma 3: Functions Viewer|Functions Viewer]]''' | # '''Listing in the [[Arma 3: Functions Viewer|Functions Viewer]]''' | ||
# '''Advanced debugging options''' | # '''Advanced debugging options''' | ||
Line 19: | Line 19: | ||
== Function Declaration == | == Function Declaration == | ||
Functions are configured within the | Functions are configured within the {{hl|CfgFunctions}} class. | ||
Mission and campaign specific functions can be configured in [[Description.ext]]/[[Campaign Description.ext]], while addon functions are defined in [[Config.cpp]]. | Mission and campaign specific functions can be configured in [[Description.ext]]/[[Campaign Description.ext]], while addon functions are defined in [[Config.cpp]]. | ||
Line 25: | Line 25: | ||
{{Feature|informative|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 | {{Feature|informative|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 {{hl|CfgFunctions}}' location: | ||
* if [[Description.ext]], '''<ROOT>''' means the [[Mission Editor: External#Mission Folder|mission's root]] directory | * if [[Description.ext]], '''<ROOT>''' means the [[Mission Editor: External#Mission Folder|mission's root]] directory | ||
* if [[Campaign Description.ext]], '''<ROOT>''' means the campaign's root directory | * if [[Campaign Description.ext]], '''<ROOT>''' means the campaign's root directory | ||
Line 45: | Line 45: | ||
* The function's name will be '''{{Color|purple|TAG}}'''_fnc_'''{{Color|teal|functionName}}''' | * The function's name will be '''{{Color|purple|TAG}}'''_fnc_'''{{Color|teal|functionName}}''' | ||
* The function will be loaded: | * The function will be loaded: | ||
** from config: | ** from config: {{hl|'''<ROOT>'''\{{Color|darkorange|Category}}\'''fn_'''{{Color|teal|functionName}}'''.sqf'''}} | ||
** from description.ext: | ** from description.ext: {{hl|'''<ROOT>'''\'''Functions'''\{{Color|darkorange|Category}}\'''fn_'''{{Color|teal|functionName}}'''.sqf'''}} | ||
{{Feature| | {{Feature|important| | ||
< | '''config.cpp only''': | ||
* {{hl|<ROOT>}} in mods is defined by the {{hl|file}} mandatory attribute - see the {{Link|#file}} section below. | |||
* Anywhere outside of running mission, refer to the functions stored in [[uiNamespace]]. e.g: <sqf>arguments call (uiNamespace getVariable "TAG_fnc_functionName");</sqf> | |||
}} | |||
=== Config Levels === | === Config Levels === | ||
Line 56: | Line 59: | ||
==== Tag ==== | ==== Tag ==== | ||
To prevent duplicates, every author must create a subclass with a unique {{Color|purple|[[ | To prevent duplicates, every author must create a subclass with a unique {{Color|purple|[[Scripting Tags|tag]]}} and create functions under it. | ||
The tag name will be used when composing a function name (e.g [[BIS_fnc_spawnGroup|'''BIS'''_fnc_spawnGroup]]). | The tag name will be used when composing a function name (e.g [[BIS_fnc_spawnGroup|'''BIS'''_fnc_spawnGroup]]). | ||
class CfgFunctions | class CfgFunctions | ||
Line 80: | Line 83: | ||
===== Attributes ===== | ===== Attributes ===== | ||
====== tag ====== | ====== tag ====== | ||
The | The {{hl|tag}} attribute can override the config {{Color|green|tag}} set differently. | ||
==== Category ==== | ==== Category ==== | ||
Line 109: | Line 112: | ||
===== Attributes ===== | ===== Attributes ===== | ||
====== file ====== | ====== file ====== | ||
The | The {{hl|file}} attribute can override the category's loading path segment - it is required for mods. | ||
====== requiredAddons ====== | ====== requiredAddons ====== | ||
The category can skip loading if a required addon is missing by setting its dependency with the | The category can skip loading if a required addon is missing by setting its dependency with the {{hl|requiredAddons}} attribute. | ||
==== Function ==== | ==== Function ==== | ||
Line 159: | Line 162: | ||
|- | |- | ||
| file | | file | ||
| the | | the {{hl|file}} attribute can be used to manually set the file path. | ||
{{Feature|important|The '''file''' entry '''overrides''' Category's set path and ignores it entirely (they are not merged).}} | {{Feature|important|The '''file''' entry '''overrides''' Category's set path and ignores it entirely (they are not merged).}} | ||
| style="text-align: center" | {{Icon|checked}} | | style="text-align: center" | {{Icon|checked}} | ||
Line 165: | Line 168: | ||
|- | |- | ||
| preInit | | preInit | ||
| the | | the {{hl|preInit}} 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 <sqf inline>["preInit"]</sqf>. | ||
The function is run in an '''[[Scheduler#Unscheduled Environment|unscheduled environment]]'''. | |||
| style="text-align: center" | {{Icon|checked}} | | style="text-align: center" | {{Icon|checked}} | ||
| style="text-align: center" | {{Icon|checked}} | | style="text-align: center" | {{Icon|checked}} | ||
|- | |- | ||
| postInit | | postInit | ||
| the | | the {{hl|postInit}} attribute can be set to 1 to call the function upon mission start, after objects are initialized.<br>Passed arguments are <sqf inline>["postInit", didJIP]</sqf>. | ||
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'''. | |||
| style="text-align: center" | {{Icon|checked}} | | style="text-align: center" | {{Icon|checked}} | ||
| style="text-align: center" | {{Icon|checked}} | | style="text-align: center" | {{Icon|checked}} | ||
|- | |- | ||
| ext | | ext | ||
| the | | the {{hl|ext}} attribute can be used to set function file's type; it can be one of: | ||
* ".sqf" (default) | * ".sqf" (default) | ||
* ".fsm" (see [[FSM]]) | * ".fsm" (see [[FSM]]) | ||
Line 182: | Line 187: | ||
|- | |- | ||
| preStart | | preStart | ||
| the | | the {{hl|preStart}} attribute can be set to 1 to call the function upon game start, before title screen, but after all addons are loaded. | ||
| style="text-align: center" | {{Icon|unchecked}} | | style="text-align: center" | {{Icon|unchecked}} | ||
| style="text-align: center" | {{Icon|checked}} | | style="text-align: center" | {{Icon|checked}} | ||
|- | |- | ||
| recompile | | recompile | ||
| the | | the {{hl|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) | ||
| style="text-align: center" | {{Icon|unchecked}} | | style="text-align: center" | {{Icon|unchecked}} | ||
| style="text-align: center" | {{Icon|checked}} | | style="text-align: center" | {{Icon|checked}} | ||
|- | |- | ||
| headerType | | headerType | ||
| the | | the {{hl|headerType}} adds code to the beginning of functions that can: | ||
* Set up the [[#Meta Variables|Meta Variables]] _fnc_scriptParent and _fnc_scriptName | * Set up the [[#Meta Variables|Meta Variables]] _fnc_scriptParent and _fnc_scriptName | ||
* Name the current scope via the command [[scriptName]] | * Name the current scope via the command [[scriptName]] | ||
* Adds debug information by saving the current execution order in the [[Array]] _fnc_scriptMap.<br/> | * Adds debug information by saving the current execution order in the [[Array]] _fnc_scriptMap.<br/> | ||
Possible values are: | 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 {{ | * -1: No header - Adds nothing, literally adds a blank [[String]] to the beginning of functions. The functions will have no debug or {{Link|#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]] | * 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. | * 1: System header - Incorporates the [[#Meta Variables|Meta Variables]] _fnc_scriptParent. Also names the current scope.<sqf> | ||
private _fnc_scriptNameParent = if (isNil '_fnc_scriptName') then { '%1' } else { _fnc_scriptName }; | |||
scriptName '%1'; | |||
</sqf> | |||
See the following section for more information. | See the following section for more information. | ||
| style="text-align: center" | {{Icon|checked}} | | style="text-align: center" | {{Icon|checked}} | ||
Line 207: | Line 213: | ||
|} | |} | ||
{{Feature|important|Notes about | {{Feature|important|Notes about {{hl|preInit}} and {{hl|postInit}} defined in '''config.cpp''': | ||
* these attributes will make the function run '''on each, every and any mission start''' | * 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 | * Any scripting error will prevent the mission from being loaded correctly | ||
Line 228: | Line 234: | ||
==== Debug Mode 0 ==== | ==== Debug Mode 0 ==== | ||
default - incorporates the [[#Meta Variables|Meta Variables]] _fnc_scriptParent and _fnc_scriptName and names the current scope. | default - incorporates the [[#Meta Variables|Meta Variables]] _fnc_scriptParent and _fnc_scriptName and names the current scope. | ||
<sqf> | |||
private _fnc_scriptNameParent = if (isNil "_fnc_scriptName") then { "%1" } else { _fnc_scriptName }; | |||
private _fnc_scriptName = "%1"; | |||
scriptName _fnc_scriptName; | |||
</sqf> | |||
==== Debug Mode 1 ==== | ==== Debug Mode 1 ==== | ||
As per ''Debug Mode 0'' and also saves the current execution order in the [[Array]] _fnc_scriptMap. | As per ''Debug Mode 0'' and also saves the current execution order in the [[Array]] _fnc_scriptMap. | ||
<sqf> | |||
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] }; | |||
</sqf> | |||
==== Debug Mode 2 ==== | ==== 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? | 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? | ||
<sqf> | |||
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]; | |||
</sqf> | |||
== See Also == | == See Also == | ||
* [[ | * [[Initialisation Order]] | ||
* [[Arma 3: Functions Viewer]] | * [[Arma 3: Functions Viewer]] | ||
* [[:Category:Arma 3: Functions|Arma 3 Functions]] | * [[:Category:Arma 3: Functions|Arma 3 Functions]] | ||
* [[Arma 3: Writing a Function|Writing a Function Tutorial]] | * [[Arma 3: Writing a Function|Writing a Function Tutorial]] | ||
{{GameCategory|arma3|Editing}} | {{GameCategory|arma3|Editing}} | ||
[[Category: Functions Library]] | [[Category: Functions Library]] |
Latest revision as of 12:01, 2 October 2024
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:
- Automatic compilation upon mission start into a global variable - no need to remember direct paths to files.
- Anti-hack protection using compileFinal (see Arma 3: Writing a Function - Recompiling and BIS_fnc_recompile)
- Listing in the Functions Viewer
- Advanced debugging options
- Optional immediate execution upon mission start, without need for manual call
- Potential performance improvements
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.
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 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 - it is required for mods.
requiredAddons
The category can skip loading if a required addon is missing by setting its dependency with the requiredAddons attribute.
Function
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. | ||
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. |
||
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. |
||
ext | the ext attribute can be used to set function file's type; it can be one of:
|
||
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. | ||
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) | ||
headerType | the headerType adds code to the beginning of functions that can:
Possible values are:
See the following section for more information. |
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.
Debug Mode 1
As per Debug Mode 0 and also saves the current execution order in the Array _fnc_scriptMap.
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?