canAdd: Difference between revisions

From Bohemia Interactive Community
Jump to navigation Jump to search
m (Text replacement - "<code>([^<]*)\[\[([a-zA-Z][a-zA-Z0-9_]+)\]\]([^<]*) *<\/code>" to "<code>$1$2$3</code>")
 
(31 intermediate revisions by 4 users not shown)
Line 6: Line 6:
|gr1= Unit Inventory
|gr1= Unit Inventory


|descr= Checks if given object can be stored in inventory of given object or any inventory container ([[uniform]], [[vest]], [[backpack]]) of given unit. Since Arma 3 v2.09 this command will count unit storage space as whole, instead of as biggest container, when multiple items are queried.  
|descr= Checks if given object can be stored in inventory of given object or any inventory container ([[uniform]], [[vest]], [[backpack]]) of given unit.<br>
Before {{arma3}} v2.10 this comment verified only the biggest container when multiple items were queried; now it checks all containers.
 
{{Feature|important|
Avoid passing an empty string <sqf inline>""</sqf> to this command as it will create a warning dialog:
[[File:a3 warning badvehicletype.jpg|none|alt= Warning Message: Bad vehicle type|Warning Message: Bad vehicle type]]
This message will show once per mission restart.
}}


|s1= object [[canAdd]] item
|s1= object [[canAdd]] item
Line 12: Line 19:
|p1= object: [[Object]] - unit or container
|p1= object: [[Object]] - unit or container


|p2= item: [[String]] - Config class
|p2= item: [[String]] - config class


|r1= [[Boolean]]
|r1= [[Boolean]]


|s2= object [[canAdd]] [item, count]
|s2= object [[canAdd]] [item, count, ignoreSoldierMaxLoad]


|s2since= arma3 1.56
|s2since= arma3 1.56
Line 22: Line 29:
|p21= object: [[Object]] - unit or container
|p21= object: [[Object]] - unit or container


|p22= item: [[String]] - Config class
|p22= item: [[String]] - config class


|p23= count: [[Number]] - desired quantity
|p23= count: [[Number]] - desired quantity
|p24= ignoreSoldierMaxLoad: [[Boolean]] - (Optional, default [[false]]) [[true]] to ignore 'soldierMaxLoad' param, container's [[maxLoad]] is still counted
|p24since= arma3 2.18


|r2= [[Boolean]]
|r2= [[Boolean]]
Line 32: Line 42:
|s3since= arma3 2.10
|s3since= arma3 2.10


|p41= weapon: [[String]] - Weapon class name
|p41= weapon: [[String]] - weapon class name


|p42= item: [[String]] - Config class of magazine or attachment
|p42= item: [[String]] - config class of magazine or attachment


|r3= [[Boolean]]
|r3= [[Boolean]]
Line 42: Line 52:
|s4since= arma3 2.10
|s4since= arma3 2.10


|p61= weapon: [[String]] - Weapon class name
|p61= weapon: [[String]] - weapon class name


|p62= attachment: [[String]] - Attachment class name
|p62= attachment: [[String]] - attachment class name


|p63= slot: [[String]] - Slot class name (defined under '''WeaponSlotsInfo''' class in weapon config)
|p63= slot: [[String]] - slot class name (defined under '''WeaponSlotsInfo''' class in weapon config)


|r4= [[Boolean]]
|r4= [[Boolean]]
Line 54: Line 64:
|s5since= arma3 2.10
|s5since= arma3 2.10


|p81= weapon: [[String]] - Weapon class name
|p81= weapon: [[String]] - weapon class name


|p82= magazine: [[String]] - Magazine class name
|p82= magazine: [[String]] - magazine class name


|p83= muzzle: [[String]] - Muzzle class name
|p83= muzzle: [[String]] - muzzle class name


|r5= [[Boolean]]
|r5= [[Boolean]]


|x1= <code>if (player canAdd "FirstAidKit") then
|x1= <sqf
>if (player canAdd "FirstAidKit") then
{
{
player addItem "FirstAidKit";
player addItem "FirstAidKit";
}
}
[[else]]
else
{
{
[[hint]] "Not enough space";
hint "Not enough space";
};</code>
};
</sqf>


|x2= <code>if (_box canAdd "FirstAidKit") then
|x2= <sqf>
if (_box canAdd "FirstAidKit") then
{
{
_box addWeaponCargo ["FirstAidKit", 1];
_box addWeaponCargo ["FirstAidKit", 1];
Line 78: Line 91:
{
{
hint "Not enough space";
hint "Not enough space";
};</code>
};
</sqf>


|x3= <code>car canAdd [<nowiki/>currentWeapon player, 50];</code>
|x3= <sqf>car canAdd [currentWeapon player, 50];</sqf>


|x4= <sqf>
|x4= <sqf>
Line 105: Line 119:
<br>
<br>
Where '''_box''' is '''B_supplyCrate_F''':
Where '''_box''' is '''B_supplyCrate_F''':
<code>// empty
<sqf>
// empty
_box canAdd "FirstAidKit"; // returns true
_box canAdd "FirstAidKit"; // returns true


// full
// full
_box canAdd "FirstAidKit"; // returns false</code>
_box canAdd "FirstAidKit"; // returns false
</sqf>
}}
}}


Line 116: Line 132:
|timestamp= 20170211184000
|timestamp= 20170211184000
|text= 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:<br>
|text= 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:<br>
<code>[[private]] _container = [[selectRandom]] [<nowiki/>[[uniformContainer]] [[player]], [[vestContainer]] [[player]], [[backpackContainer]] [[player]]];
<sqf>
[[if]] (_container [[canAdd]] "Rangefinder") [[then]]
private _container = selectRandom [uniformContainer player, vestContainer player, backpackContainer player];
if (_container canAdd "Rangefinder") then
{
{
_container [[addItemCargoGlobal]] ["Rangefinder", 1];
_container addItemCargoGlobal ["Rangefinder", 1];
};
};
</code>
</sqf>
}}
}}

Latest revision as of 11:33, 3 September 2024

Hover & click on the images for description

Description

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.
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.
Groups:
Unit Inventory

Syntax 1

Syntax:
object canAdd item
Parameters:
object: Object - unit or container
item: String - config class
Return Value:
Boolean

Syntax 2

Syntax:
object canAdd [item, count, ignoreSoldierMaxLoad]
Parameters:
object: Object - unit or container
item: String - config class
count: Number - desired quantity
since Arma 3 logo black.png2.18
ignoreSoldierMaxLoad: Boolean - (Optional, default false) true to ignore 'soldierMaxLoad' param, container's maxLoad is still counted
Return Value:
Boolean

Syntax 3

Syntax:
weapon canAdd item
Parameters:
weapon: String - weapon class name
item: String - config class of magazine or attachment
Return Value:
Boolean

Syntax 4

Syntax:
weapon canAdd [attachment, slot]
Parameters:
weapon: String - weapon class name
attachment: String - attachment class name
slot: String - slot class name (defined under WeaponSlotsInfo class in weapon config)
Return Value:
Boolean

Syntax 5

Syntax:
weapon canAdd [magazine, muzzle]
Parameters:
weapon: String - weapon class name
magazine: String - magazine class name
muzzle: String - muzzle class name
Return Value:
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"];

Additional Information

See also:
canAddItemToBackpack canAddItemToUniform canAddItemToVest compatibleMagazines compatibleItems

Notes

Report bugs on the Feedback Tracker and/or discuss them on the Arma Discord or on the Forums.
Only post proven facts here! Add Note


Jonpas - c
Posted on Nov 29, 2015 - 00:02 (UTC)
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 - c
Posted on Feb 11, 2017 - 18:40 (UTC)
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]; };