Skip to content

Commit

Permalink
Merge pull request #324 from CBATeam/retroactiveInit
Browse files Browse the repository at this point in the history
Add "initRetro" event to addClassEventHandler
  • Loading branch information
commy2 committed May 10, 2016
2 parents 85b9dd1 + c30ee09 commit 27b0692
Showing 1 changed file with 13 additions and 1 deletion.
14 changes: 13 additions & 1 deletion addons/xeh/fnc_addClassEventHandler.sqf
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ Parameters:
2: _eventFunc - Function to execute when event happens. <CODE>
3: _allowInheritance - Allow event for objects that only inherit from the given classname? [optional] <BOOLEAN> (default: true)
4: _excludedClasses - Exclude these classes from this event including their children [optional] <ARRAY> (default: [])
5: _applyInitRetroactively - Apply "init" event type on existing units that have already been initilized. [optional] <BOOLEAN> ((default: false)
Returns:
_success - Whether adding the event was successful or not. <BOOLEAN>
Expand All @@ -18,14 +19,15 @@ Examples:
(begin example)
["CAManBase", "fired", {systemChat str _this}] call CBA_fnc_addClassEventHandler;
["All", "init", {systemChat str _this}] call CBA_fnc_addClassEventHandler;
["Car", "init", {(_this select 0) engineOn true}, true, [], true] call CBA_fnc_addClassEventHandler; //Starts all current cars and those created later
(end)
Author:
commy2
---------------------------------------------------------------------------- */
#include "script_component.hpp"

params [["_className", "", [""]], ["_eventName", "", [""]], ["_eventFunc", {}, [{}]], ["_allowInheritance", true, [false]], ["_excludedClasses", [], [[]]]];
params [["_className", "", [""]], ["_eventName", "", [""]], ["_eventFunc", {}, [{}]], ["_allowInheritance", true, [false]], ["_excludedClasses", [], [[]]], ["_applyInitRetroactively", false, [false]]];

private _config = configFile >> "CfgVehicles" >> _className;

Expand All @@ -47,6 +49,11 @@ if (_eventName == "FiredBIS") exitWith {
};
if !(_eventName in GVAR(EventsLowercase)) exitWith {false};

// don't use "apply retroactively" for non init events
if (_applyInitRetroactively && {!(_eventName in ["init", "initpost"])}) then {
_applyInitRetroactively = false;
};

// add events to already existing objects
private _entities = entities "" + allUnits;
private _eventVarName = format [QGVAR(%1), _eventName];
Expand All @@ -61,6 +68,11 @@ private _eventVarName = format [QGVAR(%1), _eventName];
};

(_unit getVariable _eventVarName) pushBack _eventFunc;

//Run initReto now if the unit has already been initialized
if (_applyInitRetroactively && {ISINITIALIZED(_unit)}) then {
[_unit] call _eventFunc;
};
};
};
} forEach (_entities arrayIntersect _entities); // filter duplicates
Expand Down

0 comments on commit 27b0692

Please sign in to comment.