diff --git a/addons/events/CfgFunctions.hpp b/addons/events/CfgFunctions.hpp index ef2daaa7a..8e473bec6 100644 --- a/addons/events/CfgFunctions.hpp +++ b/addons/events/CfgFunctions.hpp @@ -46,6 +46,10 @@ class CfgFunctions { description = "Registers an event handler for a specific CBA event."; file = "\x\cba\addons\events\fnc_addEventHandler.sqf"; }; + class addEventHandlerArgs { + description = "Registers an event handler for a specific CBA event with arguments."; + file = "\x\cba\addons\events\fnc_addEventHandlerArgs.sqf"; + }; class removeEventHandler { description = "Removes an event handler previously registered with CBA_fnc_addEventHandler."; file = "\x\cba\addons\events\fnc_removeEventHandler.sqf"; diff --git a/addons/events/fnc_addEventHandler.sqf b/addons/events/fnc_addEventHandler.sqf index d1f13393d..3035a62d8 100644 --- a/addons/events/fnc_addEventHandler.sqf +++ b/addons/events/fnc_addEventHandler.sqf @@ -9,7 +9,7 @@ Parameters: _eventFunc - Function to call when event is raised. Returns: - _eventId - Unique ID of the event handler (can be used with ). + _eventId - Unique ID of the event handler (can be used with CBA_fnc_removeEventHandler). Examples: (begin example) @@ -22,9 +22,9 @@ Author: #include "script_component.hpp" SCRIPT(addEventHandler); -params [["_eventName", "", [""]], ["_eventFunc", nil, [{}]]]; +[{ + params [["_eventName", "", [""]], ["_eventFunc", nil, [{}]]]; -{ if (_eventName isEqualTo "" || isNil "_eventFunc") exitWith {-1}; private _events = GVAR(eventNamespace) getVariable _eventName; @@ -41,14 +41,12 @@ params [["_eventName", "", [""]], ["_eventFunc", nil, [{}]]]; private _internalId = _events pushBack _eventFunc; - // get last id + // get new id private _eventId = [_eventHash, "#lastId"] call CBA_fnc_hashGet; - - // inc id - _eventId = _eventId + 1; + INC(_eventId); [_eventHash, "#lastId", _eventId] call CBA_fnc_hashSet; [_eventHash, _eventId, _internalId] call CBA_fnc_hashSet; _eventId -} call CBA_fnc_directCall; +}, _this] call CBA_fnc_directCall; diff --git a/addons/events/fnc_addEventHandlerArgs.sqf b/addons/events/fnc_addEventHandlerArgs.sqf new file mode 100644 index 000000000..21d4c5832 --- /dev/null +++ b/addons/events/fnc_addEventHandlerArgs.sqf @@ -0,0 +1,55 @@ +/* ---------------------------------------------------------------------------- +Function: CBA_fnc_addEventHandlerArgs + +Description: + Registers an event handler for a specific CBA event with arguments. + + A event added with this function will have the following variables defined: + _this - Arguments passed by function calling the events. + _thisArgs - Arguments added to event by this function. + _thisId - Same as the return value of this function. + _thisType - Name of the event. (Same as _eventName passed to this function) + _thisFnc - Piece of code added to the event by this function + +Parameters: + _eventName - Type of event to handle. + _eventFunc - Function to call when event is raised. + _arguments - Arguments to pass to event handler. (optional) + +Returns: + _eventId - Unique ID of the event handler (can be used with CBA_fnc_removeEventHandler). + +Examples: + (begin example) + ["test1", { + systemChat str _thisArgs; + [_thisType, _thisId] call CBA_fnc_removeEventHandler + }, "hello world"] call CBA_fnc_addEventHandlerArgs; + + "test1" call CBA_fnc_localEvent; + (end) + +Author: + commy2 +---------------------------------------------------------------------------- */ +#include "script_component.hpp" +SCRIPT(addEventHandlerArgs); + +params [["_eventName", "", [""]], ["_eventFunc", nil, [{}]], ["_arguments", []]]; + +if (isNil QGVAR(eventsArgs)) then { + GVAR(eventsArgs) = []; +}; + +private _eventData = [_arguments, _eventFunc, _eventName]; +private _id = GVAR(eventsArgs) pushBack _eventData; + +_eventFunc = compile format [' + (GVAR(eventsArgs) select %1) params ["_thisArgs", "_thisFnc", "_thisType", "_thisId"]; + _this call _thisFnc; +', _id]; + +private _eventId = [_eventName, _eventFunc] call CBA_fnc_addEventHandler; + +_eventData pushBack _eventId; +_eventId diff --git a/addons/events/fnc_ownerEvent.sqf b/addons/events/fnc_ownerEvent.sqf index 4701c25ca..3b0417a89 100644 --- a/addons/events/fnc_ownerEvent.sqf +++ b/addons/events/fnc_ownerEvent.sqf @@ -29,7 +29,7 @@ TRACE_3("params",_eventName,_params,_targetOwner); if (_targetOwner == 2) then { //Going to server: if (isServer) then { //We are the server: Do local event (no network traffic) - [_eventName, _params] call CBA_fnc_localEvent; + CALL_EVENT(_params,_eventName); } else { //Remote Server, send event (using publicVariableServer) //Note: publicVariableClient does not seem to work on dedicated @@ -39,7 +39,7 @@ if (_targetOwner == 2) then { //Going to server: if (CBA_clientID == _targetOwner) then { //We are the target client: Do local event (no network traffic) //Note: publicVariableClient to yourself DOES trigger addPublicVariableEventHandler, but it also causes network traffic - [_eventName, _params] call CBA_fnc_localEvent; + CALL_EVENT(_params,_eventName); } else { //Remote Client, send event (using publicVariableClient) SEND_EVENT_TO_CLIENT(_params,_eventName,_targetOwner); diff --git a/addons/events/script_component.hpp b/addons/events/script_component.hpp index 75750b189..abfa370a9 100644 --- a/addons/events/script_component.hpp +++ b/addons/events/script_component.hpp @@ -31,11 +31,11 @@ #define SEND_TEVENT_TO_SERVER(params,name,targets) TEVENT_PVAR = [name, params, targets]; publicVariableServer TEVENT_PVAR_STR -#define CALL_EVENT(params,event) {\ +#define CALL_EVENT(args,event) {\ if !(isNil "_x") then {\ - params call _x;\ + args call _x;\ };\ -} forEach (GVAR(eventNamespace) getVariable event) +} forEach +(GVAR(eventNamespace) getVariable event) // copy array so events can be removed while iterating safely #define GETOBJ(obj) (if (obj isEqualType grpNull) then {leader obj} else {obj})