Arma 3 Scripting

canAdd

  • Arma 3 v0.70

Command · Markdown · JSON · original wiki page

Description

Checks if given object can be stored in inventory of given object or any inventory container (uniform, vest, backpack) of given unit.
Before Arma 3 v2.10 this comment verified only the biggest container when multiple items were queried; now it checks all containers.

Important

Avoid passing an empty string "" to this command as it will create a warning dialog:

Warning Message: Bad vehicle type

This message will show once per mission restart.

Syntax

Syntax 1

object canAdd item

Parameters:

Returns: Boolean

Syntax 2

since arma3 1.56

object canAdd [item, count, ignoreSoldierMaxLoad]

Parameters:

Returns: Boolean

Syntax 3

since arma3 2.10

weapon canAdd item

Parameters:

Returns: Boolean

Syntax 4

since arma3 2.10

weapon canAdd [attachment, slot]

Parameters:

Returns: Boolean

Syntax 5

since arma3 2.10

weapon canAdd [magazine, muzzle]

Parameters:

Returns: Boolean

Examples

Example 1

if (player canAdd "FirstAidKit") then
{
    player addItem "FirstAidKit";
}
else
{
    hint "Not enough space";
};

Example 2

if (_box canAdd "FirstAidKit") then
{
    _box addWeaponCargo ["FirstAidKit", 1];
}
else
{
    hint "Not enough space";
};

Example 3

car canAdd [currentWeapon player, 50];

Example 4

// check if attachment can be loaded
"arifle_Katiba_GL_F" canAdd "acc_pointer_IR";
"arifle_Katiba_GL_F" canAdd ["acc_pointer_IR", "PointerSlot"];

Example 5

// check if magazine can be loaded
"arifle_Katiba_GL_F" canAdd "30Rnd_65x39_caseless_green";
"arifle_Katiba_GL_F" canAdd ["30Rnd_65x39_caseless_green", "this"];
"arifle_Katiba_GL_F" canAdd ["1Rnd_SmokeRed_Grenade_shell", "EGLM"];

See also

canAddItemToBackpack · canAddItemToUniform · canAddItemToVest · compatibleMagazines · compatibleItems · compatibleWeapons

Community notes

Jonpas

Can also be used on any object that has inventory, not only player inventory containers.

Where _box is B_supplyCrate_F:

// empty
_box canAdd "FirstAidKit"; // returns true

// full
_box canAdd "FirstAidKit"; // returns false

Demellion

This can also be used as alternative to canAddItemToBackpack, canAddItemToUniform, canAddItemToVest where container might not be strictly defined. In some cases it might be a good optimisation for your code, where you also don't need to perform checks on which type of player container this is. Example:

private _container = selectRandom [uniformContainer player, vestContainer player, backpackContainer player];
if (_container canAdd "Rangefinder") then
{
    _container addItemCargoGlobal ["Rangefinder", 1];
};