Skip to content

Commit

Permalink
Common - Allow custom event name for status effects (#10473)
Browse files Browse the repository at this point in the history
* Common - Allow custom event name for status effects

* Apply suggestions from code review

Co-authored-by: Dart <[email protected]>

---------

Co-authored-by: Dart <[email protected]>
  • Loading branch information
PabstMirror and DartRuffian authored Nov 16, 2024
1 parent 0d1089d commit 9bca2a7
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 35 deletions.
4 changes: 1 addition & 3 deletions addons/common/XEH_preInit.sqf
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,7 @@ ACE_COUNTERS = [];
// Load ace_settings into CBA Settings
[] call FUNC(cbaSettings);

GVAR(statusEffect_Names) = [];
GVAR(statusEffect_isGlobal) = [];
GVAR(statusEffect_sendJIP) = [];
GVAR(statusEffects) = createHashMap;

GVAR(setHearingCapabilityMap) = [];

Expand Down
13 changes: 7 additions & 6 deletions addons/common/functions/fnc_statusEffect_addType.sqf
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
* 1: Send event globally <BOOL>
* 2: Common Effect Reaons to pre-seed durring init <ARRAY>
* 3: Send event to JIP (requires sending event globally) <BOOL>
* 4: Event name <STRING> (default: "ace_common_<effect>")
*
* Return Value:
* None
Expand All @@ -18,16 +19,16 @@
* Public: No
*/

params [["_name", "", [""]], ["_isGlobal", false, [false]], ["_commonReasonsArray", [], [[]]], ["_sendJIP", false, [false]]];
TRACE_3("params",_name,_isGlobal,_commonReasonsArray);
params [["_name", "", [""]], ["_isGlobal", false, [false]], ["_commonReasonsArray", [], [[]]], ["_sendJIP", false, [false]], ["_eventName", "", [""]]];
TRACE_4("params",_name,_isGlobal,_commonReasonsArray,_eventName);

_name = toLowerANSI _name;
if (_name == "") exitWith {ERROR_1("addStatusEffect - Bad Name %1",_this)};
if (_name in GVAR(statusEffect_Names)) exitWith {WARNING_1("addStatusEffect - Effect Already Added (note, will not update global bit) %1",_this)};
if (_name in GVAR(statusEffects)) exitWith {WARNING_1("addStatusEffect - Effect Already Added (note, will not update global bit) %1",_this)};
if (_sendJIP && !_isGlobal) exitWith {WARNING_1("addStatusEffect - Trying to add non-global JIP effect %1",_this)};
if (_eventName == "") then { _eventName = format [QGVAR(%1), _name]; };

GVAR(statusEffect_Names) pushBack _name;
GVAR(statusEffect_isGlobal) pushBack _isGlobal;
GVAR(statusEffect_sendJIP) pushBack _sendJIP;
GVAR(statusEffects) set [_name, [_isGlobal, _sendJIP, _eventName]];

//We add reasons at any time, but more efficenet to add all common ones at one time during init
if (isServer && {_commonReasonsArray isNotEqualTo []}) then {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,6 @@ if (_object == _objectRef) exitWith {};
TRACE_2("forced reset defined array on object mismatch",_x,_effectNumber);
_object setVariable [_effectVarName, 0, true]; //This always resets to 0 (not -1/nil)!
};
} forEach GVAR(statusEffect_Names);
} forEach GVAR(statusEffects);

_object setVariable [QGVAR(statusEffect_object), _object, true];
53 changes: 28 additions & 25 deletions addons/common/functions/fnc_statusEffect_sendEffects.sqf
Original file line number Diff line number Diff line change
Expand Up @@ -21,31 +21,34 @@ TRACE_2("params",_object,_effectName);

if (isNull _object) exitWith {};

{
if ((_effectName == "") || {_effectName == _x}) then {
private _effectVarName = format [QGVAR(effect_%1), _x];
private _effectNumber = _object getVariable [_effectVarName, -1];
if (_effectName == "") exitWith { // Recurse through all possible effects
{
[_object, _x] call FUNC(statusEffect_sendEffects)
} forEach (keys GVAR(statusEffects));
};

//We only do anything if the effect has been defined at some point in the game for this unit
TRACE_2("checking if event is nil",_x,_effectNumber);
if (_effectNumber != -1) then {
private _eventName = format [QGVAR(%1), _x];
switch (true) do {
case (GVAR(statusEffect_sendJIP) select _forEachIndex): {
TRACE_2("Sending Global JIP Event",_object,_effectNumber);
private _jipID = format [QGVAR(effect_%1_%2), _eventName, hashValue _object];
[_eventName, [_object, _effectNumber], _jipID] call CBA_fnc_globalEventJIP;
[_jipID, _object] call CBA_fnc_removeGlobalEventJIP;
};
case (GVAR(statusEffect_isGlobal) select _forEachIndex): {
TRACE_2("Sending Global Event",_object,_effectNumber);
[_eventName, [_object, _effectNumber]] call CBA_fnc_globalEvent;
};
default {
TRACE_2("Sending Target Event",_object,_effectNumber);
[_eventName, [_object, _effectNumber], _object] call CBA_fnc_targetEvent;
};
};

private _effectVarName = format [QGVAR(effect_%1), _effectName];
private _effectNumber = _object getVariable [_effectVarName, -1];

// We only do anything if the effect has been defined at some point in the game for this unit
TRACE_2("checking if event is nil",_effectName,_effectNumber);
if (_effectNumber != -1) then {
(GVAR(statusEffects) get toLowerANSI _effectName) params ["_isGlobal", "_sendJIP", "_eventName"];
switch (true) do {
case (_sendJIP): {
TRACE_2("Sending Global JIP Event",_object,_effectNumber);
private _jipID = format [QGVAR(effect_%1_%2), _eventName, hashValue _object];
[_eventName, [_object, _effectNumber], _jipID] call CBA_fnc_globalEventJIP;
[_jipID, _object] call CBA_fnc_removeGlobalEventJIP;
};
case (_isGlobal): {
TRACE_2("Sending Global Event",_object,_effectNumber);
[_eventName, [_object, _effectNumber]] call CBA_fnc_globalEvent;
};
default {
TRACE_2("Sending Target Event",_object,_effectNumber);
[_eventName, [_object, _effectNumber], _object] call CBA_fnc_targetEvent;
};
};
} forEach GVAR(statusEffect_Names);
};

0 comments on commit 9bca2a7

Please sign in to comment.