canAdd: Difference between revisions

From Bohemia Interactive Community
Jump to navigation Jump to search
m (Text replacement - " \|([ps][0-9]+) *= " to " |$1= ")
m (Some wiki formatting)
Line 22: Line 22:
|p21= object: [[Object]] - unit or container
|p21= object: [[Object]] - unit or container


|p22= [item, count]: [[Array]]
|p22= item: [[String]] - Config class


|p23= item: [[String]] - Config class
|p23= count: [[Number]] - desired quantity
 
|p24= count: [[Number]] - desired quantity


|r2= [[Boolean]]
|r2= [[Boolean]]


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


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




<dl class="command_description">
{{Note
 
|user= Jonpas
<dt></dt>
|timestamp= 20151129000200
<dd class="notedate">Posted on November 29, 2015 - 00:02 (UTC)</dd>
|text= Can also be used on any object that has inventory, not only player inventory containers.<br>
<dt class="note">[[User:Jonpas|Jonpas]]</dt>
<dd class="note">
Can also be used on any object that has inventory, not only player inventory containers.<br>
<br>
<br>
Where <b>_box</b> is <b>B_supplyCrate_F</b>:
Where '''_box''' is '''B_supplyCrate_F''':
<code>// Empty
<code>{{cc|empty}}
_box canAdd "FirstAidKit";
_box [[canAdd]] "FirstAidKit"; {{cc|returns [[true]]}}
-> true


// Full
{{cc|full}}
_box canAdd "FirstAidKit";
_box [[canAdd]] "FirstAidKit"; {{cc|returns [[false]]}}
-> false
</code>
</code>
</dd>
}}


<dt></dt>
{{Note
<dd class="notedate">Posted on February 11, 2017 - 18:40 (UTC)</dd>
|user= Demellion
<dt class="note">[[User:Demellion|Demellion]]</dt>
|timestamp= 20170211184000
<dd class="note">
|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>
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]]];
<code>_i = [(uniformContainer player),(vestContainer player),(backpackContainer player)] call BIS_fnc_selectRandom;
[[if]] (_container [[canAdd]] "Rangefinder") [[then]]
if (_i canAdd "Rangefinder") then {
{
    _i addItemCargoGlobal ["Rangefinder",1];
_container [[addItemCargoGlobal]] ["Rangefinder", 1];
};
};
</code>
</code>
 
}}
</dd>
 
</dl>

Revision as of 03:38, 30 October 2021

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

Syntax

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

Alternative Syntax

Syntax:
object canAdd [item, count]
Parameters:
object: Object - unit or container
item: String - Config class
count: Number - desired quantity
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];

Additional Information

See also:
canAddItemToBackpackcanAddItemToUniformcanAddItemToVest

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]; };