2023-09-12 18:58:10 +00:00
#include "..\script_component.hpp"
2015-01-11 16:42:31 +00:00
/*
* Author: commy2
* Add an addAction event to a unit. Used to handle multiple addAction events. Global arguments, local effects. Does only work for player controlled units.
*
2015-09-18 06:23:18 +00:00
* Arguments:
* 0: Unit the action should be assigned to <OBJECT>
* 1: Name of the action, e.g. "DefaultAction" <STRING>
* 2: Condition <CODE, STRING>
* 3: Code to execute <CODE, STRING>
2015-01-11 16:42:31 +00:00
*
2015-09-18 06:23:18 +00:00
* Return Value:
* ID of the action (used to remove it later) <NUMBER>
2015-09-18 23:50:17 +00:00
*
2017-06-08 13:31:51 +00:00
* Example:
* [bob, "DefaultAction", "condition", "execute"] call ace_common_fnc_addActionEventHandler
*
2015-09-18 23:50:17 +00:00
* Public: No
2015-01-11 16:42:31 +00:00
*/
2015-09-18 06:23:18 +00:00
params ["_unit", "_action", "_condition", "_statement"];
2015-01-11 16:42:31 +00:00
2015-11-20 17:40:31 +00:00
if (_condition isEqualType "") then {
2015-09-18 06:23:18 +00:00
_condition = compile _condition;
2015-01-11 16:42:31 +00:00
};
2015-11-20 17:40:31 +00:00
if (_statement isEqualType "") then {
2015-09-18 06:23:18 +00:00
_statement = compile _statement;
2015-01-11 16:42:31 +00:00
};
2015-12-14 12:08:19 +00:00
private _name = format ["ACE_Action_%1", _action];
private _actionsVar = _unit getVariable [_name, [-1, [-1, [], []], objNull]];
2015-04-16 00:00:37 +00:00
if (_unit != _actionsVar select 2) then { // check if the unit is still valid, fixes respawn issues
2015-09-18 06:23:18 +00:00
_actionsVar = [-1, [-1, [], []], objNull];
2015-04-16 00:00:37 +00:00
};
2015-01-11 16:42:31 +00:00
2015-09-18 06:23:18 +00:00
_actionsVar params ["_actionID", "_actionsArray"];
_actionsArray params ["_id", "_actionIDs", "_actions"];
2015-01-11 16:42:31 +00:00
2015-09-18 06:23:18 +00:00
_id = _id + 1;
2015-01-11 16:42:31 +00:00
_actionIDs pushBack _id;
_actions pushBack [_condition, _statement];
// first action to add, unit needs addAction command
if (_actionID == -1) then {
2015-12-14 12:08:19 +00:00
private _addAction = call compile format [
2015-09-18 06:23:18 +00:00
"[
'',
2016-03-08 00:19:32 +00:00
{[{if (inputAction '%1' == 0) exitWith {}; {if (_this call (_x select 0)) then {_this call (_x select 1)}} forEach (((_this select 0) getVariable '%2') select 1 select 2)}, _this] call CBA_fnc_directCall},
2015-09-18 06:23:18 +00:00
nil,
-1,
false,
true,
'%1',
""if (_this != ACE_player || {vehicle _this != _target}) exitWith {false}; _actions = (_this getVariable '%2') select 1 select 2; _count = count _actions; _index = 0; _return = false; while {_index < _count && {!_return}} do {_return = [_target, _this] call ((_actions select _index) select 0); _index = _index + 1}; _return""
]",
_action,
_name
];
2015-01-11 16:42:31 +00:00
2015-09-18 06:23:18 +00:00
_actionID = _unit addAction _addAction;
2015-01-11 16:42:31 +00:00
};
2015-04-16 00:00:37 +00:00
_unit setVariable [_name, [_actionID, [_id, _actionIDs, _actions], _unit], false];
2015-01-11 16:42:31 +00:00
_id