Code: Difference between revisions

From Bohemia Interactive Community
Category: Data Types
(fixed typo)
m (Add precompiled info back)
 
(6 intermediate revisions by 2 users not shown)
Line 1: Line 1:
Values of type '''Code''' represent some functionality or calculation. Often is it passed as an argument into some scripting commands which will execute it straight away, or at some later time, e.g. [[call]], [[addEventHandler]] or [[onMapSingleClick]].
'''Code''' represents data consisting of commands and their parameters.
It is defined in script by being wrapped in <sqf inline>{</sqf>/<sqf inline>}</sqf> brackets, and is precompiled by the game engine.


The text body that makes up '''Code''' are also scripting commands.  
The contents of [[SQF Syntax|SQF]] and [[SQS Syntax|SQS]] files are Code.


'''Code''' literals are represented by enclosing text into curly braces. '''{''' code.... '''}''' Any such code is precompiled by the script engine. Code values may also originate as a text, which can be converted to code using [[compile]] command.
Examples:
<sqf>
while { alive player } do { sleep 1; hintSilent format ["Health: %1/100", round ((1 - damage player) * 100)]; };
unit addEventHandler ["Killed", { systemChat format ["%1 is dead", name (_this select 0)]; }];
onMapSingleClick { systemChat format ["You clicked at %1", _pos]; };
</sqf>


[[ArmA|Armed Assault]] formalised this 'convention' by creating a Code '''Type'''. In previous versions it was a prefered style to write such strings in curled braces, but it was also possible to write them in quotation marks (exactly like [[String]]s). ArmA commands that are marked as requiring '''Code''' must be presented with curled braces. Resistance / Cold War crisis commands using a '''Code''' style paramater should ''also'' used curled brace as a good writing style that was adopted very early on by most mission makers.


==Examples==
{{Feature|informative|See [[Code vs. Strings]] for more information on code/string data typing.}}
 
'''{'''_x [[setDamage]] 1'''}''' [[forEach]] ArrayToKill




[[Category: Data Types]]
[[Category: Data Types]]

Latest revision as of 17:38, 2 March 2026

Code represents data consisting of commands and their parameters. It is defined in script by being wrapped in {/} brackets, and is precompiled by the game engine.

The contents of SQF and SQS files are Code.

Examples:

while { alive player } do { sleep 1; hintSilent format ["Health: %1/100", round ((1 - damage player) * 100)]; }; unit addEventHandler ["Killed", { systemChat format ["%1 is dead", name (_this select 0)]; }]; onMapSingleClick { systemChat format ["You clicked at %1", _pos]; };


See Code vs. Strings for more information on code/string data typing.