ACE3/addons/cargo/functions/fnc_removeCargoItem.sqf
Ozan Eğitmen ca580daedd Cargo: Add removeCargoItem Function (#5432)
* Add removeCargoItem function to cargo module

* Fix documentation typos

* Fix header 'public' field

Of course it's a public function, that's the whole point!

* Fix spelling

* Add missing then command

* Work better with objects

* After-test fixes

* Also update wiki

* Overhaul

* Make the event more robust

* Fix logic & shorten docs
2017-09-16 11:21:23 -05:00

71 lines
1.8 KiB
Plaintext

/*
* Author: 654wak654
* Removes a cargo item from the vehicle.
*
* Arguments:
* 0: Item <STRING> or <OBJECT>
* 1: Vehicle <OBJECT>
* 2: Amount <NUMBER> (default: 1)
*
* Return Value:
* Number of items removed <NUMBER>
*
* Example:
* ["ACE_Wheel", vehicle, 2] call ace_cargo_fnc_removeCargoItem
* [crate_7, truck] call ace_cargo_fnc_removeCargoItem
*
* Public: Yes
*/
#include "script_component.hpp"
params ["_item", "_vehicle", ["_amount", 1]];
TRACE_3("params",_item,_vehicle,_amount);
private _loaded = _vehicle getVariable [QGVAR(loaded), []];
private _addedSpace = 0;
private _itemClass = _item;
private _itemsRemoved = 0;
private _continue = if (_item isEqualType objNull) then {
if !(_item in _loaded) exitWith {false};
_addedSpace = [_item] call FUNC(getSizeItem);
_loaded deleteAt (_loaded find _item);
_itemClass = typeOf _item;
deleteVehicle _item;
_itemsRemoved = 1;
true
} else {
{
if (_itemsRemoved == _amount) exitWith {};
if (
(_x isEqualType "" && {_x == _item}) || // Check for classname, case-insensitive
{_x isEqualType objNull && {typeOf _x isEqualTo _item}}
) then {
INC(_itemsRemoved);
ADD(_addedSpace,[_x] call FUNC(getSizeItem));
if (_x isEqualType objNull) then {
deleteVehicle _x;
};
_loaded set [_forEachIndex, nil];
};
} forEach _loaded;
FILTER(_loaded,_x != nil);
true
};
if (!_continue) exitWith {0};
_vehicle setVariable [QGVAR(loaded), _loaded, true];
private _space = [_vehicle] call FUNC(getCargoSpaceLeft);
_vehicle setVariable [QGVAR(space), _space + _addedSpace, true];
// Invoke listenable event
["ace_cargoRemoved", [_itemClass, _vehicle, _amount, _itemsRemoved]] call CBA_fnc_globalEvent;
_itemsRemoved