Code: Difference between revisions

From Bohemia Interactive Community
Jump to navigation Jump to search
mNo edit summary
m (Some wiki formatting)
 
(7 intermediate revisions by 3 users not shown)
Line 1: Line 1:
'''Code''' is a string like <u>paramater</u> passed or used by '''some''' of the scripting commands. [[addEventHandler|AddEventHandler]] eg [[onMapSingleClick|OnMapSingleClick]] eg.
'''Code''' represents data consisting of commands and their parameters. The contents of [[SQF Syntax|SQF]] and [[SQS Syntax|SQS]] files, for example are 'Code'.
In turn, it can also be the case that one of the scripting commands gets passed further scripting commands, e.g.
<sqf inline>while { code } do { code }; unit addEventHandler ["EventHandlerType", { code }]; onMapSingleClick "Code"</sqf>.


Code literals (or code '''blocks''') are usually represented by enclosing text into curly braces: <sqf inline>{</sqf> and <sqf inline>}</sqf>.
Any such code is precompiled by the script engine. Sometimes it may become necessary to first read in certain commands as [[String]].
In order to convert code from data type String into data type Code, the command [[compile]] can be used. See [[Code vs. Strings]] for more information on code data typing.


The text body that makes up '''Code''' are also scripting commands.  Eg commands that will be executed 'at some time'.


== Examples ==


'''Code''' should be represented by enclosing curly brace. '''{''' code.... '''}'''
<sqf>{ _x setDamage 1 } forEach ArrayToKill</sqf>
<sqf>
private _codeToExecute = { hint "it works!" };
private _codeStr = toString _codeToExecute; // ' hint "it works!" '


 
call _codeToExecute; // identical to
[[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.
call compile _codeStr;
 
</sqf>
==Examples==
 
'''{'''_x [[setDamage]] 1'''}''' [[forEach]] ArrayToKill




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

Latest revision as of 15:41, 14 November 2023

Code represents data consisting of commands and their parameters. The contents of SQF and SQS files, for example are 'Code'. In turn, it can also be the case that one of the scripting commands gets passed further scripting commands, e.g. while { code } do { code }; unit addEventHandler ["EventHandlerType", { code }]; onMapSingleClick "Code".

Code literals (or code blocks) are usually represented by enclosing text into curly braces: { and }. Any such code is precompiled by the script engine. Sometimes it may become necessary to first read in certain commands as String. In order to convert code from data type String into data type Code, the command compile can be used. See Code vs. Strings for more information on code data typing.


Examples

{ _x setDamage 1 } forEach ArrayToKill
private _codeToExecute = { hint "it works!" }; private _codeStr = toString _codeToExecute; // ' hint "it works!" ' call _codeToExecute; // identical to call compile _codeStr;