myVariable = 0;
myVariable = “Hello World!”;
_myVariable= 0;
private “_myvariable”; _myVariable = 0;
//Assume that the player is alive.
if (alive player) then {_living=true}; hint format["%1",_living];
_dead=true; if (alive player) then {_dead=false}; hint format["%1",_dead];
//Single Dimension _myVariable = ["Weapon1","Weapon2"]; _count = count _myVariable; // Output: 2 // Multi-Dimension _multiArray1 = [["Item1",1,2,3],["Item2",4,5,6]]; _count = count _multiArray1; // Output: 2 _count = count _multiArray1 select 0; // Output: 4
_string = "here is my string" _string2 = 'It may contain a lot of characters #@$'
a = b
-a; +-a; -+a;
+a;
(expression)
a + b
a % b a mod b
a ^ b
!a; not a;
a && b; a and b
a || b; a or b;
a != b;
a < b;
a > b;
a <= b;
a >= b;
if (condition) then {
STATEMENT;
…
};
_living = if (alive player) then {true} else {false};
if (alive player ) then {
if (someAmmo player) then {
hint "The player is alive and has ammo!";
} else {
hint "The player is out of ammo!";
};
} else {
hint "The player is dead!";
};
if (_color == "blue") then {
hint "What a nice color";
} else {
if (_color == "red") then {
hint "Don't you get aggressive?";
}
};
switch (VARIABLE) do {
case VALUE1: {
STATEMENT;
…
};
case VALUE2: {
STATEMENT;
…
};
...
};
switch (_color) do {
case "blue": {
hint "What a nice color";
};
case "red": {
hint "Don't you get aggressive?";
};
};
switch (VARIABLE) do {
case VALUE1: {
STATEMENT;
...
};
case VALUE2: {
STATEMENT;
…
};
…
default {
STATEMENT;
…
};
};
_color=switch (side player) do {
case west: {
"ColorGreen"
};
case east: {
"ColorRed"
};
};
while {CONDITION} do {
STATEMENT;
...
};
_counter = 0;
while {_counter < 10} do {
_counter = _counter + 1;
};
for [{BEGIN}, {CONDITION}, {STEP}] do {
STATEMENT;
...
};
// a loop repeating 10 times
for [{_i=0}, {_i<10}, {_i=_i+1}] do {
player globalChat format["%1",_i];
};
/*Will display
0
1
2
3
4
5
6
7
8
9
*/
//MyFile.sqf // Code here..
//InlineFunction as Variable
MyVariableName ={
// Code here..
};
// InlineFunction as command/function parameter
_myTemporaryVariable =player addEventHandler ["killed", { diag_log("I am providing an inline function (code) to this builtin command! Hurray for science!") }];
//MyCode.sqf private ["_parameterOne","_parameterTwo"]; _parameterOne = _this select 0; _parameterTwo = _this select 1;
// Inline Function
MyInlineFunction ={
private ["_parameterOne","_parameterTwo"];
_parameterOne = _this select 0;
_parameterTwo = _this select 1;
};
// Array variable as parameter _myTempParams = [_parameterOne,_parameterTwo]; _myTempVariableTwo = _myTempParams call MyInlineFunction;
// Array as parameter _myTempVariable = [_parameterOne,_parameterTwo] call MyInlineFunction;
my_fnc = {
if (_this > 0) exitWith {
_this + 1
};
_this - 1
};
hint str (5 call my_fnc); //6 hint str (-5 call my_fnc); //-6
my_fnc = {
a = 1;
b = 2;
c = a + b;
c //<- fine
};
my_fnc = {
a = 1;
b = 2;
c = a + b;
c; //<- also fine
};
//MyCode.sqf
private ["_myName","_returnMe"];
_myName = _this select 0;
_returnMe = "FAIL";
if(_myName == "Test") then
{
_returnMe = "PASS";
};
_returnMe
// myCodeInline
MyCodeReturnValue ={
private ["_myName","_returnMe"];
_myName = _this select 0;
_returnMe = "FAIL";
if(_myName == "Kaboom") then
{
_returnMe = "PASS";
};
_returnMe
};
_myCalledVariable = ["Kaboom"] call MyCodeReturnValue; // "PASS"
_myCalledVariableFail = ["Blah"] call MyCodeReturnValue; // "FAIL"
//return.sqf STATEMENT 1; STATEMENT 2; RETURN_VALUE
//test.sqf value = call compile preprocessFile "return.sqf"; // value is now RETURN_VALUE call compile preprocessFile "return.sqf"; // valid, but RETURN_VALUE is not saved anywhere
myFunction1 = compile loadFile "myFunction1.sqf"; myFunction2 = compile preprocessFile "myFunction2.sqf"; _result1 = call myFunction1; _result2 = [1, 2] call myFunction2;
myFunction1 = compile loadFile "myFunction1.sqf"; myFunction2 = compile preprocessFile "myFunction2.sqf"; _param spawn myFunction1; [1, 2] spawn myFunction2;
comment "Return maximum of first and second argument";
private ["_a","_b"];
_a = _this select 0;
_b = _this select 1;
if (_a>_b) then {_a} else {_b}
alternative max.sqf (big boys code :))
(_this select 0) max (_this select 1)
executing script:
fMax = compile preprocessFile "max.sqf";
maxValue = [3,5] call fMax;
// maxValue is now 5
comment "Switch all infantry units to safe mode";
{
if (vehicle _x == _x) then
{
_x setBehaviour "safe"
}
} forEach _this
FNC_sayhello = {hint format["hello %1",_this]};
name player call FNC_sayhello
call FNC_helloall
