Skip to content

Commit

Permalink
Merge pull request #1670 from LinkIsGrim/addPlayerEngineEvent
Browse files Browse the repository at this point in the history
Events - Add `CBA_fnc_addBISPlayerEventHandler` for adding EHs to just the player (from ACE3)
  • Loading branch information
PabstMirror authored Aug 10, 2024
2 parents bdcb661 + 02dea07 commit 6454723
Show file tree
Hide file tree
Showing 3 changed files with 127 additions and 0 deletions.
2 changes: 2 additions & 0 deletions addons/events/CfgFunctions.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ class CfgFunctions {
class CBA {
class Events {
PATHTO_FNC(addBISEventHandler);
PATHTO_FNC(addBISPlayerEventHandler);
PATHTO_FNC(addPlayerEventHandler);
PATHTO_FNC(removePlayerEventHandler);
PATHTO_FNC(addDisplayHandler);
Expand All @@ -13,6 +14,7 @@ class CfgFunctions {
PATHTO_FNC(removeKeyHandler);
PATHTO_FNC(addEventHandler);
PATHTO_FNC(addEventHandlerArgs);
PATHTO_FNC(removeBISPlayerEventHandler);
PATHTO_FNC(removeEventHandler);
PATHTO_FNC(localEvent);
PATHTO_FNC(globalEvent);
Expand Down
79 changes: 79 additions & 0 deletions addons/events/fnc_addBISPlayerEventHandler.sqf
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
#include "script_component.hpp"
/* ----------------------------------------------------------------------------
Function: CBA_fnc_addBISPlayerEventHandler
Description:
Adds an engine event handler just to the controlled entity.
Parameters:
_key - Unique identifier for the event. <STRING>
_eventType - Type of event to add. Can be any event supported by addEventHandler. <STRING>
_eventCode - Code to run when event is raised. <CODE>
_ignoreVirtual - Ignore virtual units (spectators, virtual zeus, UAV RC) [optional] (default: true) <BOOLEAN>
Returns:
Event was added <BOOLEAN>
Examples:
(begin example)
["TAG_MyFiredNearEvent", "FiredNear", {systemChat str _this}] call CBA_fnc_addBISPlayerEventHandler
(end)
Author:
PabstMirror
---------------------------------------------------------------------------- */
SCRIPT(addBISPlayerEventHandler);

if (canSuspend) exitWith {
[CBA_fnc_addBISPlayerEventHandler, _this] call CBA_fnc_directCall;
};

params [
["_key", "", [""]],
["_type", "", [""]],
["_code", {}, [{}]],
["_ignoreVirtual", true, [true]]
];

if (isNil QGVAR(playerEventsHash)) then { // first-run init
GVAR(playerEventsHash) = createHashMap;
["unit", {
params ["_newPlayer", "_oldPlayer"];

// uav check only applies to direct controlling UAVs from zeus, no effect on normal UAV operation
private _isVirtual = (unitIsUAV _newPlayer) || {getNumber (configOf _newPlayer >> "isPlayableLogic") == 1};

TRACE_4("",_newPlayer,_oldPlayer,_isVirtual,count GVAR(playerEventsHash));
{
_y params ["_type", "_code", "_ignoreVirtual"];

private _oldEH = _oldPlayer getVariable [_x, -1];
if (_oldEH != -1) then {
_oldPlayer removeEventHandler [_type, _oldEH];
_oldPlayer setVariable [_x, nil];
};

_oldEH = _newPlayer getVariable [_x, -1];
if (_oldEH != -1) then { continue }; // if respawned then var and EH already exists
if (_ignoreVirtual && _isVirtual) then { continue };

private _newEH = _newPlayer addEventHandler [_type, _code];
_newPlayer setVariable [_x, _newEH];
} forEach GVAR(playerEventsHash);
}, false] call CBA_fnc_addPlayerEventHandler;
};


_key = format [QGVAR(playerEvents_%1), toLowerANSI _key];
if (_key in GVAR(playerEventsHash)) exitWith { ERROR_1("bad key %1",_this); false};

GVAR(playerEventsHash) set [_key, [_type, _code, _ignoreVirtual]];

private _player = call CBA_fnc_currentUnit;
if (_ignoreVirtual && {(unitIsUAV _player) || {getNumber (configOf _player >> "isPlayableLogic") == 1}}) exitWith {};

// Add event now
private _newEH = _player addEventHandler [_type, _code];
_player setVariable [_key, _newEH];

true // return
46 changes: 46 additions & 0 deletions addons/events/fnc_removeBISPlayerEventHandler.sqf
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
#include "script_component.hpp"
/* ----------------------------------------------------------------------------
Function: CBA_fnc_removeBISPlayerEventHandler
Description:
Remove event handlers added via CBA_fnc_addBISPlayerEventHandler.
Parameters:
_key - Unique identifier for the event. <STRING>
Returns:
Event was removed <BOOLEAN>
Examples:
(begin example)
["TAG_MyFiredNearEvent"] call CBA_fnc_removeBISPlayerEventHandler
(end)
Author:
LinkIsGrim
---------------------------------------------------------------------------- */
SCRIPT(removeBISPlayerEventHandler);

if (canSuspend) exitWith {
[CBA_fnc_removeBISPlayerEventHandler, _this] call CBA_fnc_directCall;
};

params [
["_key", "", [""]]
];

if (isNil QGVAR(playerEventsHash)) exitWith {false};

_key = format [QGVAR(playerEvents_%1), toLowerANSI _key];
if !(_key in GVAR(playerEventsHash)) exitWith {false};

(GVAR(playerEventsHash) deleteAt _key) params ["_type", "", "_ignoreVirtual"];

private _player = call CBA_fnc_currentUnit;
private _ehIdx = _player getVariable [_key, -1]; // don't touch any events if it wasn't one we added
if (_ehIdx != -1) then {
_player removeEventHandler [_type, _ehIdx];
_player setVariable [_key, nil];
};

true // return

0 comments on commit 6454723

Please sign in to comment.