Advertisement
Guest User

Standalone file version of CBA events

a guest
Jan 18th, 2013
117
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.29 KB | None | 0 0
  1.  
  2. /*
  3.     Like CBA events but for missions.
  4.     Heavily "inspired" by CBA implementation.
  5. */
  6.  
  7. #define PV_VAR TAG_pv
  8. #define QUOTE(X_NAME) #X_NAME
  9.  
  10. //Prevent multiple loads
  11. if (!isNil "TAG_EventHandlers") exitWith {};
  12.  
  13. TAG_EventHandlers = "Logic" createVehicleLocal [0,0,0];
  14.  
  15. /*
  16.     Adds an event handler on this machine.
  17.     Returns handler-id to be used to remove
  18.  
  19.     [EVENT, CODE] call TAG_AddEventHandler;
  20. */
  21. TAG_AddEventHandler = {
  22.     private ["_event","_code","_eventHandlers","_index"];
  23.     _event = _this select 0;
  24.     _code = _this select 1;
  25.     _eventHandlers = TAG_EventHandlers getVariable _event;
  26.     if (isNil "_eventHandlers") then {
  27.         _eventHandlers = [];
  28.         TAG_EventHandlers setVariable [_event, _eventHandlers];
  29.     };
  30.     _index = count _eventHandlers;
  31.     _eventHandlers set [_index, _code];
  32.     _index
  33. };
  34.  
  35. /*
  36.     Removes the event handler that has the
  37.     id. Returns true if an event handler
  38.     with the ID was found.
  39.  
  40.     [EVENT, ID] call TAG_RemoveEventHandler;
  41. */
  42. TAG_RemoveEventHandler = {
  43.     private ["_event","_id","_eventHandlers","_wasRemoved"];
  44.     _event = _this select 0;
  45.     _id = _this select 1;
  46.     _eventHandlers = TAG_EventHandlers getVariable [_event, []];
  47.     _wasRemoved = _id >= 0 && _id < count _eventHandlers;
  48.     if (_wasRemoved) then {
  49.         if (!isNil {_eventHandlers select _id}) then {
  50.             _eventHandlers set [_id, nil];
  51.         } else {
  52.             _wasRemoved = false;
  53.         };
  54.     };
  55.     _wasRemoved
  56. };
  57.  
  58. /*
  59.     Triggers the event on on all remote
  60.     machines. (All except this machine)
  61.  
  62.     [EVENT, ARGS] call TAG_RemoteEvent;
  63. */
  64. TAG_RemoteEvent = {
  65.     PV_VAR = _this;
  66.     publicVariable QUOTE(PV_VAR);
  67. };
  68.  
  69. /*
  70.     Triggers the event only on this local
  71.     machine.
  72.  
  73.     [EVENT, ARGS] call TAG_LocalEvent;
  74. */
  75. TAG_LocalEvent = {
  76.     private ["_event","_args","_eventHandlers"];
  77.     _event = _this select 0;
  78.     _args = if (count _this > 1) then {_this select 1} else {[]};
  79.     _eventHandlers = TAG_EventHandlers getVariable [_event, []];
  80.     {
  81.         //Handler may have been removed.
  82.         if (!isNil "_x") then {
  83.             _args call _x;
  84.         };
  85.     } forEach _eventHandlers;
  86. };
  87.  
  88. /*
  89.     Triggers the event on all machines.
  90.  
  91.     [EVENT, ARGS] call TAG_GlobalEvent;
  92. */
  93. TAG_GlobalEvent = {
  94.     _this call TAG_RemoteEvent;
  95.     _this call TAG_LocalEvent;
  96. };
  97.  
  98. //Don't forget to receive
  99. QUOTE(PV_VAR) addPublicVariableEventHandler {(_this select 1) call TAG_LocalEvent};
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement