Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- Like CBA events but for missions.
- Heavily "inspired" by CBA implementation.
- */
- #define PV_VAR TAG_pv
- #define QUOTE(X_NAME) #X_NAME
- //Prevent multiple loads
- if (!isNil "TAG_EventHandlers") exitWith {};
- TAG_EventHandlers = "Logic" createVehicleLocal [0,0,0];
- /*
- Adds an event handler on this machine.
- Returns handler-id to be used to remove
- [EVENT, CODE] call TAG_AddEventHandler;
- */
- TAG_AddEventHandler = {
- private ["_event","_code","_eventHandlers","_index"];
- _event = _this select 0;
- _code = _this select 1;
- _eventHandlers = TAG_EventHandlers getVariable _event;
- if (isNil "_eventHandlers") then {
- _eventHandlers = [];
- TAG_EventHandlers setVariable [_event, _eventHandlers];
- };
- _index = count _eventHandlers;
- _eventHandlers set [_index, _code];
- _index
- };
- /*
- Removes the event handler that has the
- id. Returns true if an event handler
- with the ID was found.
- [EVENT, ID] call TAG_RemoveEventHandler;
- */
- TAG_RemoveEventHandler = {
- private ["_event","_id","_eventHandlers","_wasRemoved"];
- _event = _this select 0;
- _id = _this select 1;
- _eventHandlers = TAG_EventHandlers getVariable [_event, []];
- _wasRemoved = _id >= 0 && _id < count _eventHandlers;
- if (_wasRemoved) then {
- if (!isNil {_eventHandlers select _id}) then {
- _eventHandlers set [_id, nil];
- } else {
- _wasRemoved = false;
- };
- };
- _wasRemoved
- };
- /*
- Triggers the event on on all remote
- machines. (All except this machine)
- [EVENT, ARGS] call TAG_RemoteEvent;
- */
- TAG_RemoteEvent = {
- PV_VAR = _this;
- publicVariable QUOTE(PV_VAR);
- };
- /*
- Triggers the event only on this local
- machine.
- [EVENT, ARGS] call TAG_LocalEvent;
- */
- TAG_LocalEvent = {
- private ["_event","_args","_eventHandlers"];
- _event = _this select 0;
- _args = if (count _this > 1) then {_this select 1} else {[]};
- _eventHandlers = TAG_EventHandlers getVariable [_event, []];
- {
- //Handler may have been removed.
- if (!isNil "_x") then {
- _args call _x;
- };
- } forEach _eventHandlers;
- };
- /*
- Triggers the event on all machines.
- [EVENT, ARGS] call TAG_GlobalEvent;
- */
- TAG_GlobalEvent = {
- _this call TAG_RemoteEvent;
- _this call TAG_LocalEvent;
- };
- //Don't forget to receive
- QUOTE(PV_VAR) addPublicVariableEventHandler {(_this select 1) call TAG_LocalEvent};
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement