Difference between revisions of "PreProcessor Commands"

From Bohemia Interactive Community
Jump to navigation Jump to search
m (__EVAL)
m (Some wiki formatting)
 
(47 intermediate revisions by 8 users not shown)
Line 1: Line 1:
[[Category:Scripting Topics]]
+
{{TOC|side|0.9}}
The parser allows you to use macros in configs. Macros are a bit similar to functions in programming and allow you to use a single definition many times in the config, without having to duplicate the whole definition again and again. It also gives you a centralized place to correct errors in this definition. This page mainly refers to OFP, some examples won't work for ARMA and ARMA 2.<br>
+
The PreProcessor and the Config Parser allow to use macros in configs. The purpose of macros is to re-use the same definition in scripts and configs multiple times. It also gives a centralized place to correct errors in this definition.
{{Important|In ArmA 3, preprocessor commands are <b>case-sensitive!</b>}}
+
Know that edge cases may arise - see the collection of test-cases linked at the end of this page.
 +
{{Feature | arma3 | In {{arma3}}, preprocessor commands are '''case-sensitive!'''}}
  
  
 
== Parsing ==
 
== Parsing ==
 +
 +
See {{HashLink|#Config Parser}} below:
 
* '''[[Config.cpp]]''' - parsed when PBO is binarized.
 
* '''[[Config.cpp]]''' - parsed when PBO is binarized.
 
** [[localize]] cannot be used in macros, as it would hardcode string of current language instead of creating reference.
 
** [[localize]] cannot be used in macros, as it would hardcode string of current language instead of creating reference.
 
* '''[[Description.ext]]''' - parsed when mission is loaded or previewed in missions list.
 
* '''[[Description.ext]]''' - parsed when mission is loaded or previewed in missions list.
* '''[[SQF]] script''' - parsed when [[preprocessFile]], [[preprocessFileLineNumbers]] or [[execVM]] is used.
+
* '''[[SQF Syntax|SQF]] script''' - parsed when [[preprocessFile]], [[preprocessFileLineNumbers]], [[compileScript]] or [[execVM]] is used.
  
  
Line 14: Line 17:
  
 
=== Comments ===
 
=== Comments ===
 +
 
A comment is a line within code that is not actually processed by the game engine. They are used to make code more readable or to add notes for future reference. The preprocessor removes all comments from the file before it is processed. Therefore, comments are never actually "seen" by the game engine.
 
A comment is a line within code that is not actually processed by the game engine. They are used to make code more readable or to add notes for future reference. The preprocessor removes all comments from the file before it is processed. Therefore, comments are never actually "seen" by the game engine.
  
Comments may span multiple lines, or only part of a line if needed.
+
<syntaxhighlight lang="cpp">
 
+
// this is a single-line comment
{{codecomment|//this is a single-line comment}}
 
 
   
 
   
{{codecomment|/* this is a
+
/*
multi-line
+
this is a
comment */}}
+
multi-line
 +
comment
 +
*/
 
   
 
   
mycode = something; {{codecomment|//only this part of the line is commented out}}
+
myValue = something; //only this part of the line is commented out
 
myArray = ["apple"{{codecomment|/*,"bananna*/}},"pear"]; {{codecomment|//a portion in the middle of this line is commented out}}
 
  
 +
myArray = ["apple"/*, "banana"*/, "pear"]; // a portion in the middle of this line is commented out
 +
</syntaxhighlight>
  
 
=== #define ===
 
=== #define ===
Using the ''#define'' instruction, you can define a keyword and assign a definition to it. As an example:
 
  
  #define true 1
+
Using the ''#define'' instruction, it is possible to define a keyword and assign a definition to it. The keyword may contain any letter, digit or underscore in arbitrary order, as long as it does not start with a digit (RegEx: {{hl|[a-zA-Z_][0-9a-zA-Z_]*}}). As an example:
 +
<syntaxhighlight lang="cpp">
 +
#define true 1
 +
</syntaxhighlight>
  
 
The above means that whenever ''true'' is used in a config, the parser will replace this with the value ''1''.
 
The above means that whenever ''true'' is used in a config, the parser will replace this with the value ''1''.
 +
 +
The define-statement does swallow all spaces in between the macro-keyword and any non-space-character in the body (Note that tabs are not spaces! They don't get removed)
 +
<syntaxhighlight lang="cpp">
 +
#define MACRO                    test
 +
MACRO // preprocesses to test (without any spaces)
 +
 +
#define MACRO test // There's a tab between MACRO and test
 +
MACRO // preprocesses to " test" (without quotes - they are only used to show that the tab character didn't get removed)
 +
</syntaxhighlight>
 +
 +
The space between the macro-keyword and the body is also fully optional (though very useful to tell the preprocessor where the macro name ends and where the body begins):
 +
<syntaxhighlight lang="cpp">
 +
#define MACRO#test
 +
MACRO // preprocesses to "test"
 +
</syntaxhighlight>
  
 
==== Arguments ====
 
==== Arguments ====
You can add arguments to more complex macros, by including them between brackets after the keyword:
+
Arguments can be added to more complex macros, by including them between brackets after the keyword. For the name of the arguments the same rule as for the macro-keyword (see above) apply.
 +
<syntaxhighlight lang="cpp">
 +
#define CAR(NAME) displayName = NAME;
 +
</syntaxhighlight>
  
  #define CAR(NAME) displayName = NAME;
+
If ''CAR("Mini")'' is used, it will be replaced with ''displayName = "Mini";''. Multiple arguments can also be used:
 +
<syntaxhighlight lang="cpp">
 +
#define BLASTOFF(UNIT,RATE) UNIT setVelocity [0,0,RATE];
 +
</syntaxhighlight>
  
If you now use ''CAR("Mini")'', this will be replaced with ''displayName = "Mini";''. Multiple arguments can also be used:
+
Macro arguments may be composed of any characters, as long as they do not contain a comma (because commas are used as argument-delimiters). If quotes are being used, they have to be balanced. The same applies to single-quotes This is because String detection is working in macro arguments - Therefore commas can even get passed in as a macro argument as long as they are part of a String (This only works with Strings wrapped in double-quotes though). Note however that although the macro gets resolved properly, the comma gets removed from the String (probably a bug).
 +
<syntaxhighlight lang="cpp">
 +
#define MACRO(arg) arg
 +
MACRO("Some, content") // preprocesses to "Some content" (note the missing comma)
 +
</syntaxhighlight>
  
  #define BLASTOFF(UNIT,RATE) UNIT setVelocity [0,0,RATE];
+
Quote escaping is also not supported in this context (neither with double- nor with single-quotes)
 +
<syntaxhighlight lang="cpp">
 +
#define MACRO(arg) arg
 +
MACRO("Some ""content""") // preprocesses to "Some ""content"""
 +
</syntaxhighlight>
  
Passing arrays with more than one element <tt>[el1,el2,...]</tt> as arguments into macros as well as any argument containing comas <tt>"some, sentence"</tt>, will need a small workaround:
+
Passing arrays with more than one element {{hl|[el1,el2,...]}} as arguments into macros as well as any argument containing comas {{hl|"some, sentence"}}, will need a small workaround:
 
+
<syntaxhighlight lang="cpp">
  #define HINTARG(ARG) hint ("Passed argument: " + str ARG)
+
#define HINTARG(ARG) hint ("Passed argument: " + str ARG)
 +
</syntaxhighlight>
  
 
Incorrect usage:
 
Incorrect usage:
 
+
<syntaxhighlight lang="cpp">
  HINTARG([1,2,3,4,5,6,7,8,9,0]); //ERROR, won't even compile
+
HINTARG([1,2,3,4,5,6,7,8,9,0]); // ERROR, won't even compile
 +
</syntaxhighlight>
  
 
Correct usage:
 
Correct usage:
 +
<syntaxhighlight lang="cpp">
 +
#define array1 [1,2,3,4,5,6,7,8,9,0]
 +
HINTARG(array1); // SUCCESS
 +
</syntaxhighlight>
  
  #define array1 [1,2,3,4,5,6,7,8,9,0]
+
The argument replacement is performed before the expansion of the macro body. That means one doesn't have to worry about name-conflicts between argument-names of the current macro and already defined macros:
  HINTARG(array1); //SUCCESS
+
<syntaxhighlight lang="cpp">
 +
#define ONE foo
 +
#define TWO(ONE) ONE
 +
TWO(bar) // will preprocess to bar
 +
</syntaxhighlight>
  
 
==== Replacing parts of words ====
 
==== Replacing parts of words ====
By default you can only replace whole words by arguments. If you need to replace only part of a word, you can use the ''##'' instruction. This is necessary when either the start or the end of the argument connects to another character that is not a ''';''' (semi-colon) or &nbsp; (space).
+
By default, only whole words can be replaced by arguments. If only a part of the word should be replaced, use the ''##'' instruction. This is necessary when either the start or the end of the argument connects to another character that is not a ''';''' (semi-colon) or &nbsp; (space).
 
+
<syntaxhighlight lang="cpp">
  class NAME##_Button_Slider: RscText \
+
class NAME##_Button_Slider: RscText \
  { \
+
{ \
      model = \OFP2\Structures\Various\##FOLDER##\##FOLDER; \
+
model = \OFP2\Structures\Various\##FOLDER##\##FOLDER; \
 
+
</syntaxhighlight>
You can also use the single ''#'' to convert an argument to a string.
 
  
  statement = (this animate [#SEL, 0]); \
+
It is also possible to use the single ''#'' to convert an argument to a string.
 +
<syntaxhighlight lang="cpp">
 +
statement = (this animate [#SEL, 0]); \
 +
</syntaxhighlight>
  
 
==== Multi-line ====
 
==== Multi-line ====
For longer definitions, you can stretch the macro across multiple lines. To create a multi-line definition, each line except the last one should end with a ''\'' character:
+
For longer definitions, one can stretch the macro across multiple lines. To create a multi-line definition, each line except the last one should end with a ''\'' character:
 +
<syntaxhighlight lang="cpp">
 +
#define DRAWBUTTON(NAME)\
 +
__EXEC(idcNav = idcNav + 4) \
 +
...
 +
</syntaxhighlight>
  
  #define DRAWBUTTON(NAME)\
+
{{Feature | Informative | The backslash must be the last character in a line when defining a multi-line macro. Any character (including spaces) after the backslash will cause issues.}}
      __EXEC(idcNav = idcNav + 4) \
 
  ...
 
  
'''NOTE''': The backslash must be the last character in a line when defining a multi-line macro. Any character (including spaces) after the backslash will cause issues.
+
=== #undef ===
  
=== #undef ===
 
 
Undefine (delete) a macro previously set by the use of #define.
 
Undefine (delete) a macro previously set by the use of #define.
#undef NAME
+
<syntaxhighlight lang="cpp">
 +
#undef NAME
 +
</syntaxhighlight>
 +
 
 +
=== #if  ===
  
 +
'''Description:''' Checks condition and processes content if condition returns 1. Skips content if it returns 0
 +
<syntaxhighlight lang="cpp">
 +
#if __A3_DEBUG__ // Check if game is started in debug mode
 +
#include "debug.hpp" // Include some file if debug mode is enabled
 +
#endif
 +
</syntaxhighlight>
  
 
=== #ifdef ===
 
=== #ifdef ===
You can use a simple if-then construction to check whether a certain set of definitions has already been made:
+
 
#ifdef NAME
+
A simple if-then construction to check whether a certain set of definitions has already been made:
  ...text that will be used if NAME is defined...
+
<syntaxhighlight lang="cpp">
#endif
+
#ifdef NAME
 +
...text that will be used if NAME is defined...
 +
#endif
 +
</syntaxhighlight>
 +
 
 
IFDEFs ''cannot'' be nested. The preprocessor will generate errors for all inner definitions if the outer definition doesn't exist.
 
IFDEFs ''cannot'' be nested. The preprocessor will generate errors for all inner definitions if the outer definition doesn't exist.
 
  
 
=== #ifndef ===
 
=== #ifndef ===
Same as #ifdef, but checks for absence of definiton instead.
 
#ifndef NAME
 
  ...text that will be used if NAME ''isn't'' defined...
 
#endif
 
  
 +
Same as #ifdef, but checks for absence of definition instead.
 +
<syntaxhighlight lang="cpp">
 +
#ifndef NAME
 +
...text that will be used if NAME ''isn't'' defined...
 +
#endif
 +
</syntaxhighlight>
  
 
=== #else ===
 
=== #else ===
#ifndef NAME
+
 
  ...text that will be used if NAME ''isn't'' defined...
+
Provides alternative code to {{hl|#if}}, {{hl|#ifdef}}, {{hl|#ifndef}} checks.
#else
+
<syntaxhighlight lang="cpp">
  ...text that will be used if NAME ''is'' defined...
+
#ifndef NAME
#endif
+
...text that will be used if NAME is -not- defined...
 +
#else
 +
...text that will be used if NAME -is- defined...
 +
#endif
 +
</syntaxhighlight>
  
 
=== #endif ===
 
=== #endif ===
 +
 
This ends a conditional block as shown in the descriptions of #ifdef and #ifndef above.
 
This ends a conditional block as shown in the descriptions of #ifdef and #ifndef above.
  
 
=== #include ===
 
=== #include ===
 +
 
Copies the code from a target file and pastes it where #include directive is.
 
Copies the code from a target file and pastes it where #include directive is.
#include "file.hpp"
+
<syntaxhighlight lang="cpp">
#include <file.txt> {{codecomment |//Brackets are equivalent to quotation marks and may be used in their place.}}
+
#include "file.hpp"
 +
#include <file.txt> // Brackets are equivalent to quotation marks and may be used in their place.
 +
</syntaxhighlight>
  
 +
Source directory is:
 +
* For any file without starting the include path with \ - the file's current directory
 +
* When starting with \ - the internal filesystem root (see [[CMA:DevelopmentSetup#Addon_development|Addon Development]]) or the Game's working directory (only with [[Arma 3 Startup Parameters|-filePatching]] enabled)
  
Source directory is:  
+
A path beginning can be defined as follow:
* for any file without starting the include path with \ - the file's current directory
+
* drive (only with [[Arma 3 Startup Parameters|-filePatching]] enabled): <syntaxhighlight lang="cpp">#include "D:\file.txt"</syntaxhighlight>
* When starting with \ - the internal filesystem root (see [[CMA:DevelopmentSetup#Addon_development|Addon_development]]) or the Game's working directory (only with [[Arma_3_Startup_Parameters#Developer_Options -filePatching]] enabled)
+
* PBO with [[PBOPREFIX]]: <syntaxhighlight lang="cpp"> #include "\myMod\myAddon\file.txt"</syntaxhighlight>
 +
* PBO (keep in mind that in this case, if the PBO's file name will be changed, all '#include' referencing it will need to be updated):<!--
 +
--><syntaxhighlight lang="cpp">#include"\myMod\myAddon\file.txt" // Arma 3\@myMod\addons\myAddon.pbo\file.txt;</syntaxhighlight>
  
 +
To move to parent directory use '..' (two dots) (Supported since {{GVI|arma3|1.50}}):
 +
<syntaxhighlight lang="cpp">
 +
#include "..\file.sqf"
 +
</syntaxhighlight>
  
You can define a path beginning with:
+
Preprocessor does not support the use of macros for pre-defined file names.
 +
<syntaxhighlight lang="cpp">
 +
#define path "codestrip.txt"
 +
#include path // this will cause an error
 +
</syntaxhighlight>
  
* drive (only with [[Arma_3_Startup_Parameters#Developer_Options -filePatching]] enabled):
+
=== # ===
#include "D:\file.txt"
 
  
* PBO with [[PBOPREFIX]]:
+
'#' (single hash) operator wraps the text with quotation marks.
#include "\myMod\myAddon\file.txt"
+
<syntaxhighlight lang="cpp">
 +
#define STRINGIFY(s) #s
 +
#define FOO 123
 +
test1 = STRINGIFY(123); //test1 = "123";
 +
test2 = STRINGIFY(FOO); //test2 = "123";
 +
</syntaxhighlight>
  
* PBO (keep in mind that in this case, if the PBO's file name will be changed, all '#include' referencing it will need to be updated)
+
This operator does only work on keywords following the rules for macro-names (see {{hl|#define}}-section). If one wants to stringify anything else (like e.g. a number), one has to use a stringify-macro that takes an argument and stringifies that (as in the example above).
#include"\myMod\myAddon\file.txt" // Arma 3\@myMod\addons\myAddon.pbo\file.txt;
+
<syntaxhighlight lang="cpp">
 +
#define MACRO Test #123
 +
MACRO // preprocesses to Test 123 - note that there are not any quotes inserted
 +
</syntaxhighlight>
  
 +
=== ## ===
  
To move to parent directory use '..' (two dots) (Supported in Arma 3 since v1.49.131707):
+
'##' (double hash) operator concatenates what's before the ## with what's after it.
#include "..\file.sqf"
+
<syntaxhighlight lang="cpp">
 +
#define GLUE(g1,g2) g1##g2
 +
#define FOO 123
 +
#define BAR 456
 +
test1 = GLUE(123,456); //test1 = 123456;
 +
test2 = GLUE(FOO,BAR); //test2 = 123456;
 +
</syntaxhighlight>
  
 +
=== __LINE__ ===
 +
 +
This keyword gets replaced with the line number in the file where it is found. For example, if __LINE__ is found on the 10th line of a file, the word __LINE__ will be replaced with the number 10.
 +
 +
=== __FILE__ ===
 +
 +
This keyword gets replaced with the CURRENT file being processed.
 +
 +
{{ArgTitle|3|__has_include|{{GVI|arma3|2.02}}}}
 +
 +
'''Description:''' Checks if given file exists. Returns 1 if it does, 0 if it does not exist.
 +
#if __has_include("\z\ace\addons\main\script_component.hpp")
 +
{{cc|File exists! Do something with it}}
 +
#else
 +
{{cc|File does not exist}}
 +
#endif
 +
 +
{{ArgTitle|3|__DATE_ARR__|{{GVI|arma3|2.02}}}}
 +
 +
'''Description:''' Is replaced for current date in format [[Array|array]].
 +
__DATE_ARR__ {{cc|2020,10,28,15,17,42}}
 +
 +
{{ArgTitle|3|__DATE_STR__|{{GVI|arma3|2.02}}}}
 +
 +
'''Description:''' Is replaced for current date in format [[String|string]].
 +
__DATE_STR__  {{cc|"2020/10/28, 15:17:42"}}
 +
 +
{{ArgTitle|3|__DATE_STR_ISO8601__|{{GVI|arma3|2.02}}}}
 +
 +
'''Description:''' Is replaced for current date in format [[String|string]]. The date is presented in ISO8601 standard.
 +
__DATE_STR_ISO8601__ {{cc|"2020-10-28T14:17:42Z"}}
 +
 +
{{ArgTitle|3|__TIME__|{{GVI|arma3|2.02}}}}
 +
 +
'''Description:''' Is replaced for current time.
 +
__TIME__ {{cc|15:17:42}}
 +
 +
{{ArgTitle|3|__TIME_UTC__|{{GVI|arma3|2.02}}}}
 +
 +
'''Description:''' Is replaced for  UTC time.
 +
__TIME_UTC__ {{cc|14:17:42}}
 +
 +
{{ArgTitle|3|__COUNTER__|{{GVI|arma3|2.02}}}}
 +
 +
'''Description:''' Counts 1 up everytime it is defined.
 +
__COUNTER__ {{cc|0}}
 +
__COUNTER__ {{cc|1}}
 +
__COUNTER__ {{cc|2}}
 +
__COUNTER__ {{cc|3}}
 +
__COUNTER_RESET__
 +
__COUNTER__ {{cc|0}}
 +
__COUNTER__ {{cc|1}}
 +
__COUNTER__ {{cc|2}}
 +
 +
{{ArgTitle|3|__TIMESTAMP_UTC__|{{GVI|arma3|2.06}}}}
 +
 +
'''Description:''' Is replaced to the current timestamp in UNIX timestamp.
 +
__TIMESTAMP_UTC__ {{cc|1622639059}}
 +
 +
{{ArgTitle|3|__COUNTER_RESET__|{{GVI|arma3|2.02}}}}
 +
 +
'''Description:''' Resets counter. See example above.
 +
 +
{{ArgTitle|3|__RAND_INT{{hl|N}}__|{{GVI|arma3|2.02}}}}
 +
 +
'''Description:''' Gets replaced by a random {{hl|N}} bit integer. {{hl|N}} can be 8, 16, 32 or 64, e.g:
 +
<syntaxhighlight lang="cpp">
 +
__RAND_INT8__
 +
__RAND_INT16__
 +
__RAND_INT32__
 +
__RAND_INT64__
 +
</syntaxhighlight>
 +
 +
__RAND_INT8__ {{cc|e.g -102}}
 +
 +
{{ArgTitle|3|__RAND_UINT{{hl|N}}__|{{GVI|arma3|2.02}}}}
 +
 +
'''Description:''' Gets replaced by a random ''unsigned'' {{hl|N}} bit integer. {{hl|N}} can be 8, 16, 32 or 64, e.g:
 +
<syntaxhighlight lang="cpp">
 +
__RAND_UINT8__
 +
__RAND_UINT16__
 +
__RAND_UINT32__
 +
__RAND_UINT64__
 +
</syntaxhighlight>
 +
 +
__RAND_UINT8__ {{cc|108}}
 +
 +
{{ArgTitle|3|__GAME_VER__|{{GVI|arma3|2.02}}}}
 +
 +
'''Description:''' Gets replaced by the game version.
 +
__GAME_VER__ {{cc|02.00.146790}}
 +
 +
{{ArgTitle|3|__GAME_VER_MAJ__|{{GVI|arma3|2.02}}}}
 +
 +
'''Description:''' Gets replaced by the ''major'' game version.
 +
__GAME_VER_MAJ__ {{cc|02}}
 +
 +
{{ArgTitle|3|__GAME_VER_MIN__|{{GVI|arma3|2.02}}}}
 +
 +
'''Description:''' Gets replaced by the ''minor'' game version.
 +
__GAME_VER_MIN__ {{cc|00}}
 +
 +
{{ArgTitle|3|__GAME_BUILD__|{{GVI|arma3|2.02}}}}
 +
 +
'''Description:''' Gets replaced by the build number.
 +
__GAME_BUILD__ {{cc|146790}}
 +
 +
{{ArgTitle|3|__ARMA__|{{GVI|arma3|2.02}}}}
 +
 +
'''Description:''' {{Wiki|stub}}
 +
__ARMA__ {{cc|1}}
 +
 +
{{ArgTitle|3|__ARMA3__|{{GVI|arma3|2.02}}}}
 +
 +
'''Description:''' {{Wiki|stub}}
 +
__ARMA3__ {{cc|1}}
 +
 +
{{ArgTitle|3|__A3_DEBUG__|{{GVI|arma3|2.02}}}}
 +
 +
'''Description:''' This macro is only set when the [[Arma 3 Startup Parameters| -debug parameter]] was provided. It can be used to switch mods or scripts to debug mode dynamically.
 +
__A3_DEBUG__ {{cc|1 (If -debug was enabled)}}
  
Preprocessor does not support the use of macros for pre-defined file names.
 
#define path "codestrip.txt"
 
#include path {{codecomment|//this will cause an error}}
 
  
=== # ===
+
== Config Parser ==
'#' (single hash) operator wraps the text with quotation marks.
 
#define STRINGIFY(s) #s;
 
#define FOO 123
 
test1 = STRINGIFY(123); //test1 = "123";
 
test2 = STRINGIFY(FOO); //test2 = "123";
 
  
=== ## ===
+
{{Feature|important|'''Config Parser''' keywords '''cannot''' be used in preprocessor {{HashLink|#Macros}}, e.g '''{{HashLink|##if}}'''!}}
'##' (double hash) operator concatenates what's before the ## with what's after it.
 
#define GLUE(g1,g2) g1##g2
 
#define FOO 123
 
#define BAR 456
 
test1 = GLUE(123,456); //test1 = 123456;
 
test2 = GLUE(FOO,BAR); //test2 = 123456;
 
  
 
=== __EXEC ===
 
=== __EXEC ===
This '''config parser''' macro allows you to assign values to internal variables or just execute arbitrary code. The code inside <tt>__EXEC</tt> macros runs in [[parsingNamespace]] and variables defined in it will also be created in [[parsingNamespace]]. The variables can then be used to create more complex macros:
 
  
<code>__EXEC(cat = 5 + 1;)
+
This '''Config Parser''' macro allows to assign values to internal variables or just execute arbitrary code. The code inside {{hl|__EXEC}} macros runs in [[parsingNamespace]] and variables defined in it will also be created in [[parsingNamespace]]. The variables can then be used to create more complex macros:
__EXEC(lev = cat - 2;)</code>
 
<code>_cat = [[parsingNamespace]] [[getVariable]] "cat"; //6
 
_lev = [[parsingNamespace]] [[getVariable]] "lev"; //4</code>
 
  
{{Important | <tt>__EXEC</tt> macros are not suitable for <tt>SQF/SQS</tt> scripts but can be used in configs, including [[description.ext]]}}<br>
+
__EXEC(cat = 5 + 1;)
{{Warning | <tt>__EXEC</tt> doesn't like round brackets <tt>()</tt> inside expressions. If you need to have grouping, perhaps you could calculate values inside the brackets separately and assign to local variables:
+
__EXEC(lev = cat - 2;)
<code>__EXEC(a <nowiki>=</nowiki> (1+2);) // ERROR</code>
+
_cat = [[parsingNamespace]] [[getVariable]] "cat"; {{cc|6}}
<code>__EXEC(_expr <nowiki>=</nowiki> 1+2;)
+
_lev = [[parsingNamespace]] [[getVariable]] "lev"; {{cc|4}}
__EXEC(a <nowiki>=</nowiki> _expr;) // OK</code>
+
 
 +
{{Feature | Warning |
 +
{{hl|__EXEC}} doesn't like round brackets {{hl|()}} inside expressions. If grouping is needed, perhaps values could be calculated inside the brackets separately and assigned to local variables:
 +
<syntaxhighlight lang="cpp">__EXEC(a = (1+2);) // ERROR</syntaxhighlight>
 +
<syntaxhighlight lang="cpp">
 +
__EXEC(_expr = 1+2;)
 +
__EXEC(a = _expr;) // OK
 +
</syntaxhighlight>
 
}}
 
}}
  
 
=== __EVAL ===
 
=== __EVAL ===
With this '''config parser''' macro you can evaluate expressions, including previously assigned internal variables. Unlike with <tt>__EXEC</tt>, <tt>__EVAL</tt> supports multiple parentheses
 
  
<code>w = __EVAL([[safeZoneW]] - (5 * ((1 / ([[getResolution]] [[select]] 2)) * 1.25 * 4)));</code>
+
With this '''Config Parser''' macro expressions can be evaluated, including previously assigned internal variables. Unlike {{hl|__EXEC}}, {{hl|__EVAL}} supports multiple parentheses:
  
<tt>__EVAL</tt> macros MUST be assigned to a config property and the expression MUST be terminated with <tt>;</tt>. __EVAL can return only 2 types of data: [[Number]] and [[String]]. Any other type is represented as [[String]], even [[Boolean]] type, which will result in either <tt>"true"</tt> or <tt>"false"</tt>.<br><br>
+
w = __EVAL([[safeZoneW]] - (5 * ((1 / ([[getResolution]] [[select]] 2)) * 1.25 * 4)));
  
{{Important | <tt>__EVAL</tt> macros macros are not suitable for <tt>SQF/SQS</tt> scripts but can be used in configs, including [[description.ext]]. Both global and local variables set in <tt>__EXEC</tt> are available in <tt>__EVAL</tt>}}<br>
+
{{hl|__EVAL}} macros '''must''' be assigned to a config property and the expression '''must''' be terminated with {{hl|;}}. {{hl|__EVAL}} can only return [[Number]] or [[String]]: . Any other type is represented as [[String]], even [[Boolean]] type, which will result in either {{hl|"true"}} or {{hl|"false"}}.
{{Warning | <tt>__EVAL</tt> doesn't like curly brackets <tt>{}</tt>, if you need to have code in your expression use [[compile]] [[String]] instead:
 
<code>result <nowiki>=</nowiki> __EVAL([[call]] {123}); // ERROR</code>
 
<code>result <nowiki>=</nowiki> __EVAL([[call]] [[compile]] "123"); // OK</code> }}
 
  
=== __LINE__ ===
+
{{Feature|warning|
This keyword gets replaced with the line number in the file where it is found. For example, if __LINE__ is found on the 10th line of a file, the word __LINE__ will be replaced with the number 10.
+
{{hl|__EVAL}} does not like curly brackets {{hl|{}}}; if code is needed in the expression, use [[compile]] [[String]] instead:
 +
<syntaxhighlight lang="cpp">result = __EVAL(call { 123 }); // ERROR</syntaxhighlight>
 +
<syntaxhighlight lang="cpp">result = __EVAL(call compile "123"); // OK</syntaxhighlight>
 +
}}
 +
 
 +
{{Feature|important|{{hl|__EXEC}} and {{hl|__EVAL}} macros are not suitable for SQF/SQS scripts but can be used in configs, including [[Description.ext|description.ext]]. <br>Both global and local variables set in {{hl|__EXEC}} are available in {{hl|__EVAL}}}}
 +
 
 +
 
 +
== Errors ==
 +
 
 +
=== Error 2 ===
 +
 
 +
; Problem: Preprocessor failed error 2.
 +
 
 +
; How to fix: Add quotation marks in the title, or the file path. (''e.g.'' {{hl|#include "soGood.sqf"}}).
 +
 
 +
=== Error 6 ===
 +
 
 +
; Problem: Preprocessor failed on file X - error 6.
 +
 
 +
; Known reasons:
 +
: "The problem is using {{hl|#ifdef #ifdef #endif #endif}}, a.k.a nested {{hl|#ifdef}}. This doesn't work in Arma. It's only possible to use {{hl|#ifdef}} and {{hl|#endif}} once and not nested."
 +
: {{hl|#endif}} without preceding {{hl|#ifdef}} or {{hl|#ifndef}}
 +
 
 +
=== Error 7 ===
 +
 
 +
; Problem: Preprocessor failed on file X - error 7.
  
=== __FILE__ ===
+
; Known reasons: The preprocessor encountered an unknown directive. Read, as a typo most has likely found a way into the file (something like {{hl|#inlcude}} or {{hl|#defien}}). Double-check all preprocessor directives in that file.
This keyword gets replaced with the CURRENT file being processed.
 
  
  
 
== External links ==
 
== External links ==
* http://ofp-faguss.com/files/ofp_preprocessor_explained.pdf ([https://web.archive.org/web/20170319190732/http://ofp-faguss.com/files/ofp_preprocessor_explained.pdf Wayback Machine])
+
 
 +
* [https://web.archive.org/web/20170319190732/http://ofp-faguss.com/files/ofp_preprocessor_explained.pdf {{ofp}} - Preprocessor Guide (Wayback Machine)]
 +
* [https://github.com/Krzmbrzl/ArmaPreprocessorTestCases Collection of preprocessor test-cases]
 +
 
 +
 
 +
[[Category:Scripting_Topics]]

Latest revision as of 13:47, 15 July 2022

The PreProcessor and the Config Parser allow to use macros in configs. The purpose of macros is to re-use the same definition in scripts and configs multiple times. It also gives a centralized place to correct errors in this definition. Know that edge cases may arise - see the collection of test-cases linked at the end of this page.

Arma 3
In Arma 3, preprocessor commands are case-sensitive!


Parsing

See Config Parser below:


Macros

Comments

A comment is a line within code that is not actually processed by the game engine. They are used to make code more readable or to add notes for future reference. The preprocessor removes all comments from the file before it is processed. Therefore, comments are never actually "seen" by the game engine.

// this is a single-line comment
 
/*
	this is a
	multi-line
	comment
*/
 
myValue = something; //only this part of the line is commented out

myArray = ["apple"/*, "banana"*/, "pear"]; // a portion in the middle of this line is commented out

#define

Using the #define instruction, it is possible to define a keyword and assign a definition to it. The keyword may contain any letter, digit or underscore in arbitrary order, as long as it does not start with a digit (RegEx: [a-zA-Z_][0-9a-zA-Z_]*). As an example:

#define true 1

The above means that whenever true is used in a config, the parser will replace this with the value 1.

The define-statement does swallow all spaces in between the macro-keyword and any non-space-character in the body (Note that tabs are not spaces! They don't get removed)

#define MACRO                     test
MACRO // preprocesses to test (without any spaces)

#define MACRO	test // There's a tab between MACRO and test
MACRO // preprocesses to "	test" (without quotes - they are only used to show that the tab character didn't get removed)

The space between the macro-keyword and the body is also fully optional (though very useful to tell the preprocessor where the macro name ends and where the body begins):

#define MACRO#test
MACRO // preprocesses to "test"

Arguments

Arguments can be added to more complex macros, by including them between brackets after the keyword. For the name of the arguments the same rule as for the macro-keyword (see above) apply.

#define CAR(NAME) displayName = NAME;

If CAR("Mini") is used, it will be replaced with displayName = "Mini";. Multiple arguments can also be used:

#define BLASTOFF(UNIT,RATE) UNIT setVelocity [0,0,RATE];

Macro arguments may be composed of any characters, as long as they do not contain a comma (because commas are used as argument-delimiters). If quotes are being used, they have to be balanced. The same applies to single-quotes This is because String detection is working in macro arguments - Therefore commas can even get passed in as a macro argument as long as they are part of a String (This only works with Strings wrapped in double-quotes though). Note however that although the macro gets resolved properly, the comma gets removed from the String (probably a bug).

#define MACRO(arg) arg
MACRO("Some, content") // preprocesses to "Some content" (note the missing comma)

Quote escaping is also not supported in this context (neither with double- nor with single-quotes)

#define MACRO(arg) arg
MACRO("Some ""content""") // preprocesses to "Some ""content"""

Passing arrays with more than one element [el1,el2,...] as arguments into macros as well as any argument containing comas "some, sentence", will need a small workaround:

#define HINTARG(ARG) hint ("Passed argument: " + str ARG)

Incorrect usage:

HINTARG([1,2,3,4,5,6,7,8,9,0]); // ERROR, won't even compile

Correct usage:

#define array1 [1,2,3,4,5,6,7,8,9,0] 
HINTARG(array1); // SUCCESS

The argument replacement is performed before the expansion of the macro body. That means one doesn't have to worry about name-conflicts between argument-names of the current macro and already defined macros:

#define ONE foo
#define TWO(ONE) ONE
TWO(bar) // will preprocess to bar

Replacing parts of words

By default, only whole words can be replaced by arguments. If only a part of the word should be replaced, use the ## instruction. This is necessary when either the start or the end of the argument connects to another character that is not a ; (semi-colon) or   (space).

class NAME##_Button_Slider: RscText \
{ \
	model = \OFP2\Structures\Various\##FOLDER##\##FOLDER; \

It is also possible to use the single # to convert an argument to a string.

statement = (this animate [#SEL, 0]); \

Multi-line

For longer definitions, one can stretch the macro across multiple lines. To create a multi-line definition, each line except the last one should end with a \ character:

#define DRAWBUTTON(NAME)\
	__EXEC(idcNav = idcNav + 4) \
	...
The backslash must be the last character in a line when defining a multi-line macro. Any character (including spaces) after the backslash will cause issues.

#undef

Undefine (delete) a macro previously set by the use of #define.

#undef NAME

#if

Description: Checks condition and processes content if condition returns 1. Skips content if it returns 0

#if __A3_DEBUG__ // Check if game is started in debug mode
#include "debug.hpp" // Include some file if debug mode is enabled
#endif

#ifdef

A simple if-then construction to check whether a certain set of definitions has already been made:

#ifdef NAME
	...text that will be used if NAME is defined...
#endif

IFDEFs cannot be nested. The preprocessor will generate errors for all inner definitions if the outer definition doesn't exist.

#ifndef

Same as #ifdef, but checks for absence of definition instead.

#ifndef NAME
	...text that will be used if NAME ''isn't'' defined...
#endif

#else

Provides alternative code to #if, #ifdef, #ifndef checks.

#ifndef NAME
	...text that will be used if NAME is -not- defined...
#else
	...text that will be used if NAME -is- defined...
#endif

#endif

This ends a conditional block as shown in the descriptions of #ifdef and #ifndef above.

#include

Copies the code from a target file and pastes it where #include directive is.

#include "file.hpp"
#include <file.txt> // Brackets are equivalent to quotation marks and may be used in their place.

Source directory is:

  • For any file without starting the include path with \ - the file's current directory
  • When starting with \ - the internal filesystem root (see Addon Development) or the Game's working directory (only with -filePatching enabled)

A path beginning can be defined as follow:

  • drive (only with -filePatching enabled):
    #include "D:\file.txt"
    
  • PBO with PBOPREFIX:
     #include "\myMod\myAddon\file.txt"
    
  • PBO (keep in mind that in this case, if the PBO's file name will be changed, all '#include' referencing it will need to be updated):
    #include"\myMod\myAddon\file.txt" // Arma 3\@myMod\addons\myAddon.pbo\file.txt;
    

To move to parent directory use '..' (two dots) (Supported since Arma 3 logo black.png1.50):

#include "..\file.sqf"

Preprocessor does not support the use of macros for pre-defined file names.

#define path "codestrip.txt"
#include path // this will cause an error

#

'#' (single hash) operator wraps the text with quotation marks.

#define STRINGIFY(s) #s
#define FOO 123
test1 = STRINGIFY(123); //test1 = "123";
test2 = STRINGIFY(FOO); //test2 = "123";

This operator does only work on keywords following the rules for macro-names (see #define-section). If one wants to stringify anything else (like e.g. a number), one has to use a stringify-macro that takes an argument and stringifies that (as in the example above).

#define MACRO Test #123
MACRO // preprocesses to Test 123 - note that there are not any quotes inserted

##

'##' (double hash) operator concatenates what's before the ## with what's after it.

#define GLUE(g1,g2) g1##g2
#define FOO 123
#define BAR 456
test1 = GLUE(123,456); //test1 = 123456;
test2 = GLUE(FOO,BAR); //test2 = 123456;

__LINE__

This keyword gets replaced with the line number in the file where it is found. For example, if __LINE__ is found on the 10th line of a file, the word __LINE__ will be replaced with the number 10.

__FILE__

This keyword gets replaced with the CURRENT file being processed.

__has_include

Description: Checks if given file exists. Returns 1 if it does, 0 if it does not exist.

#if __has_include("\z\ace\addons\main\script_component.hpp")
// File exists! Do something with it
#else
// File does not exist
#endif

__DATE_ARR__

Description: Is replaced for current date in format array.

__DATE_ARR__ // 2020,10,28,15,17,42

__DATE_STR__

Description: Is replaced for current date in format string.

__DATE_STR__  // "2020/10/28, 15:17:42"

__DATE_STR_ISO8601__

Description: Is replaced for current date in format string. The date is presented in ISO8601 standard.

__DATE_STR_ISO8601__ // "2020-10-28T14:17:42Z"

__TIME__

Description: Is replaced for current time.

__TIME__ // 15:17:42

__TIME_UTC__

Description: Is replaced for UTC time.

__TIME_UTC__ // 14:17:42

__COUNTER__

Description: Counts 1 up everytime it is defined.

__COUNTER__ // 0
__COUNTER__ // 1
__COUNTER__ // 2
__COUNTER__ // 3
__COUNTER_RESET__
__COUNTER__ // 0
__COUNTER__ // 1
__COUNTER__ // 2

__TIMESTAMP_UTC__

Description: Is replaced to the current timestamp in UNIX timestamp.

__TIMESTAMP_UTC__ // 1622639059

__COUNTER_RESET__

Description: Resets counter. See example above.

__RAND_INTN__

Description: Gets replaced by a random N bit integer. N can be 8, 16, 32 or 64, e.g:

__RAND_INT8__
__RAND_INT16__
__RAND_INT32__
__RAND_INT64__
__RAND_INT8__ // e.g -102

__RAND_UINTN__

Description: Gets replaced by a random unsigned N bit integer. N can be 8, 16, 32 or 64, e.g:

__RAND_UINT8__
__RAND_UINT16__
__RAND_UINT32__
__RAND_UINT64__
__RAND_UINT8__ // 108

__GAME_VER__

Description: Gets replaced by the game version.

__GAME_VER__ // 02.00.146790

__GAME_VER_MAJ__

Description: Gets replaced by the major game version.

__GAME_VER_MAJ__ // 02

__GAME_VER_MIN__

Description: Gets replaced by the minor game version.

__GAME_VER_MIN__ // 00

__GAME_BUILD__

Description: Gets replaced by the build number.

__GAME_BUILD__ // 146790

__ARMA__

Description:

__ARMA__ // 1

__ARMA3__

Description:

__ARMA3__ // 1

__A3_DEBUG__

Description: This macro is only set when the -debug parameter was provided. It can be used to switch mods or scripts to debug mode dynamically.

__A3_DEBUG__ // 1 (If -debug was enabled)


Config Parser

Config Parser keywords cannot be used in preprocessor Macros, e.g #if!

__EXEC

This Config Parser macro allows to assign values to internal variables or just execute arbitrary code. The code inside __EXEC macros runs in parsingNamespace and variables defined in it will also be created in parsingNamespace. The variables can then be used to create more complex macros:

__EXEC(cat = 5 + 1;)
__EXEC(lev = cat - 2;)
_cat = parsingNamespace getVariable "cat"; // 6
_lev = parsingNamespace getVariable "lev"; // 4
__EXEC doesn't like round brackets () inside expressions. If grouping is needed, perhaps values could be calculated inside the brackets separately and assigned to local variables:
__EXEC(a = (1+2);) // ERROR
__EXEC(_expr = 1+2;)
__EXEC(a = _expr;) // OK

__EVAL

With this Config Parser macro expressions can be evaluated, including previously assigned internal variables. Unlike __EXEC, __EVAL supports multiple parentheses:

w = __EVAL(safeZoneW - (5 * ((1 / (getResolution select 2)) * 1.25 * 4)));

__EVAL macros must be assigned to a config property and the expression must be terminated with ;. __EVAL can only return Number or String: . Any other type is represented as String, even Boolean type, which will result in either "true" or "false".

__EVAL does not like curly brackets {}; if code is needed in the expression, use compile String instead:
result = __EVAL(call { 123 }); // ERROR
result = __EVAL(call compile "123"); // OK
__EXEC and __EVAL macros are not suitable for SQF/SQS scripts but can be used in configs, including description.ext.
Both global and local variables set in __EXEC are available in __EVAL


Errors

Error 2

Problem
Preprocessor failed error 2.
How to fix
Add quotation marks in the title, or the file path. (e.g. #include "soGood.sqf").

Error 6

Problem
Preprocessor failed on file X - error 6.
Known reasons
"The problem is using #ifdef #ifdef #endif #endif, a.k.a nested #ifdef. This doesn't work in Arma. It's only possible to use #ifdef and #endif once and not nested."
#endif without preceding #ifdef or #ifndef

Error 7

Problem
Preprocessor failed on file X - error 7.
Known reasons
The preprocessor encountered an unknown directive. Read, as a typo most has likely found a way into the file (something like #inlcude or #defien). Double-check all preprocessor directives in that file.


External links