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

add '_thisArgs', '_thisId', '_thisType' and '_thisFnc' and to CBA events #375

Merged
merged 5 commits into from
Jun 13, 2016
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
4 changes: 4 additions & 0 deletions addons/events/CfgFunctions.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down
14 changes: 6 additions & 8 deletions addons/events/fnc_addEventHandler.sqf
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ Parameters:
_eventFunc - Function to call when event is raised. <CODE>

Returns:
_eventId - Unique ID of the event handler (can be used with <CBA_fnc_removeEventHandler>).
_eventId - Unique ID of the event handler (can be used with CBA_fnc_removeEventHandler).

Examples:
(begin example)
Expand All @@ -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;
Expand All @@ -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;
55 changes: 55 additions & 0 deletions addons/events/fnc_addEventHandlerArgs.sqf
Original file line number Diff line number Diff line change
@@ -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. <ANY>
_thisArgs - Arguments added to event by this function. <ANY>
_thisId - Same as the return value of this function. <NUMBER>
_thisType - Name of the event. (Same as _eventName passed to this function) <STRING>
_thisFnc - Piece of code added to the event by this function <CODE>

Parameters:
_eventName - Type of event to handle. <STRING>
_eventFunc - Function to call when event is raised. <CODE>
_arguments - Arguments to pass to event handler. (optional) <Any>

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
4 changes: 2 additions & 2 deletions addons/events/fnc_ownerEvent.sqf
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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);
Expand Down
6 changes: 3 additions & 3 deletions addons/events/script_component.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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})

Expand Down