Arma 3 Scripting

Code Best Practices

Topic · Markdown · JSON · original wiki page

Getting Started

In the domain of development, any rule is a rule of thumb. If a rule states for example that it is better that a line of code doesn't go over 80 characters, it doesn't mean that any line must not go over 80 characters; sometimes, the situation needs it. If you have a good structure, do not change your code to enforce a single arbitrary rule. If you break many of them, you may have to change something; again, this is according to your judgement.

With that being said, let's go!

Prerequisites

Best Practices

Code Format

Variable Format

Code Structuration

Code/Files Organisation

Examples

Bad Example Good Example
Good Practice examples
_unit = player; private _unit = player;
private _uB = allUnits select { side _x == blufor }; private _bluforUnits = allUnits select { side _x == blufor };
private _plead = leader player; private _playersLeader = leader player;
finalAssault = true; publicVariable "finalAssault"; PREFIX_finalAssault = true; publicVariable "PREFIX_finalAssault";
player setVariable ["MoneyInPocket", 250, true]; player setVariable ["PREFIX_MoneyInPocket", 250, true];
#define KILL(UNIT) UNIT setDamage 1
// ...
{
    KILL(_x);
} forEach (units group player - [player]);
private _killFnc = { _this setDamage 1; };
// ...
{
    _x call _killFnc;
} forEach (units group player - [player]);
// if player has less than 3/4 health
if (damage player > 0.25) then
{
    // if the player has no first aid kit
    if (not ("FirstAidKit" in items player))
    {
        // if player has room for first aid kit
        if (player canAdd "FirstAidKit") then
        {
             // add first aid kit to the player
            player addItem "FirstAidKit";
        }
        else
        {
            // set player's damage to 1/4
            player setDamage 0.25;
        };
    };
};
// player will need health at this stage of the mission
if (
    damage player > 0.25 &&
    not ("FirstAidKit" in items player)) then
{
    if (player canAdd "FirstAidKit") then
    {
        player addItem "FirstAidKit";
    }
    else // let's help him anyway
    {
        player setDamage 0.25;
    };
};
Flow Logic examples
if (alive player && damage player >= 0.9) then {
    hint "very damaged";
};
if (alive player && damage player >= 0.5 && damage player < 0.9) then {
    hint "quite damaged";
};
if (alive player && damage player > 0 && damage player < 0.5) then {
    hint "slightly damaged";
};
if (alive player && damage player == 0) then {
    hint "pristine state";
};
if (not alive player) then {
    hint "dead";
};
if (not alive player) exitWith { hint "dead"; };
private _playerDamage = damage player;
switch (true) do {
    case (_playerDamage >= 0.9): { hint "very damaged"; };
    case (_playerDamage >= 0.5): { hint "quite damaged"; };
    case (_playerDamage > 0)   : { hint "slightly damaged"; };
    default { hint "pristine"; };
};
if (cond1) then
{
    if (cond2) then
    {
        if (cond3) then
        {
            // hadouken code
        }
        else { /* failure 3 */ };
    }
    else { /* failure 2 */ };
}
else { /* failure 1 */ };
if (!cond1) exitWith { /* failure 1 */ };
if (!cond2) exitWith { /* failure 2 */ };
if (!cond3) exitWith { /* failure 3 */ };
// code
Format examples
if (not alive player) exitWith
{ hint "dead"; };
private _playerDamage = damage player;
switch (true) do {
case (_playerDamage >= 0.9): {hint "very damaged";};
case (_playerDamage >= 0.5): {
    hint  "quite damaged";};
case (_playerDamage > 0): { hint "slightly damaged"; };
  default
{
   hint "pristine";
  };
};
if (not alive player) exitWith { hint "dead"; };
private _playerDamage = damage player;
switch (true) do {
    case (_playerDamage >= 0.9): { hint "very damaged"; };
    case (_playerDamage >= 0.5): { hint "quite damaged"; };
    case (_playerDamage > 0)   : { hint "slightly damaged"; };
    default { hint "pristine"; };
};

Final Words

See also