Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Events - Add CBA_fnc_addBISPlayerEventHandler for adding EHs to just the player (from ACE3) #1670

Merged
merged 8 commits into from
Aug 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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