Code: Difference between revisions

From Bohemia Interactive Community
Category: Data Types
m (Some wiki formatting)
m (Add precompiled info back)
 
(One intermediate revision by the same user not shown)
Line 1: Line 1:
'''Code''' represents data consisting of commands and their parameters. The contents of [[SQF Syntax|SQF]] and [[SQS Syntax|SQS]] files, for example are 'Code'.
'''Code''' represents data consisting of commands and their parameters.
In turn, it can also be the case that one of the scripting commands gets passed further scripting commands, e.g.
It is defined in script by being wrapped in <sqf inline>{</sqf>/<sqf inline>}</sqf> brackets, and is precompiled by the game engine.
<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>.
The contents of [[SQF Syntax|SQF]] and [[SQS Syntax|SQS]] files are Code.
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:
<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>


== Examples ==


<sqf>{ _x setDamage 1 } forEach ArrayToKill</sqf>
{{Feature|informative|See [[Code vs. Strings]] for more information on code/string data typing.}}
<sqf>
private _codeToExecute = { hint "it works!" };
private _codeStr = toString _codeToExecute; // ' hint "it works!" '
 
call _codeToExecute; // identical to
call compile _codeStr;
</sqf>




[[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.