Skip to content

Commit

Permalink
Pass functions by "reference" to bis event handlers (#4898)
Browse files Browse the repository at this point in the history
* Pass functions by "reference" to bis event handlers

* Add Doc

* Fix

Conflicts:
	addons/laserpointer/XEH_postInit.sqf
  • Loading branch information
PabstMirror authored and thojkooi committed Mar 13, 2017
1 parent 5105394 commit a3750b3
Show file tree
Hide file tree
Showing 6 changed files with 16 additions and 9 deletions.
2 changes: 1 addition & 1 deletion addons/concertina_wire/XEH_init.sqf
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
#include "script_component.hpp"
params ["_wire"];
_wire addEventHandler ["HandleDamage", FUNC(handleDamage)];
_wire addEventHandler ["HandleDamage", {call FUNC(handleDamage)}];
6 changes: 3 additions & 3 deletions addons/hearing/XEH_postInit.sqf
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ GVAR(lastPlayerVehicle) = objNull;
TRACE_2("removed veh eh",_firedEH,GVAR(lastPlayerVehicle));
};
if ((!isNull _vehicle) && {_player != _vehicle}) then {
private _firedEH = _vehicle addEventHandler ["FiredNear", LINKFUNC(firedNear)];
private _firedEH = _vehicle addEventHandler ["FiredNear", {call FUNC(firedNear)}];
_vehicle setVariable [QGVAR(firedEH), _firedEH];
GVAR(lastPlayerVehicle) = _vehicle;
TRACE_2("added veh eh",_firedEH,GVAR(lastPlayerVehicle));
Expand All @@ -60,9 +60,9 @@ GVAR(lastPlayerVehicle) = objNull;
};
// Don't add a new EH if the unit respawned
if ((_player getVariable [QGVAR(firedEH), -1]) == -1) then {
private _firedEH = _player addEventHandler ["FiredNear", LINKFUNC(firedNear)];
private _firedEH = _player addEventHandler ["FiredNear", {call FUNC(firedNear)}];
_player setVariable [QGVAR(firedEH), _firedEH];
private _explosionEH = _player addEventHandler ["Explosion", LINKFUNC(explosionNear)];
private _explosionEH = _player addEventHandler ["Explosion", {call FUNC(explosionNear)}];
_player setVariable [QGVAR(explosionEH), _explosionEH];
TRACE_3("added unit eh",_player,_firedEH,_explosionEH);
};
Expand Down
2 changes: 1 addition & 1 deletion addons/interact_menu/XEH_clientInit.sqf
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ GVAR(ParsedTextCached) = [];
//Setup text/shadow/size/color settings matrix
[] call FUNC(setupTextColors);
// Install the render EH on the main display
addMissionEventHandler ["Draw3D", DFUNC(render)];
addMissionEventHandler ["Draw3D", {call FUNC(render)}];
}] call CBA_fnc_addEventHandler;

//Add Actions to Houses:
Expand Down
4 changes: 1 addition & 3 deletions addons/laserpointer/XEH_postInit.sqf
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,5 @@ GVAR(nearUnits) = [];

} , 5, []] call CBA_fnc_addPerFrameHandler;

addMissionEventHandler ["Draw3D", {
call FUNC(onDraw);
}];
addMissionEventHandler ["Draw3D", {call FUNC(onDraw)}];
}] call CBA_fnc_addEventHandler;
2 changes: 1 addition & 1 deletion addons/spectator/functions/fnc_handleInterface.sqf
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ switch (toLower _mode) do {
[FUNC(handleUnits), 9, _display] call CBA_fnc_addPerFrameHandler;

// Handle 3D unit icons
GVAR(iconHandler) = addMissionEventHandler ["Draw3D",FUNC(handleIcons)];
GVAR(iconHandler) = addMissionEventHandler ["Draw3D", {call FUNC(handleIcons)}];

// Populate the help window
private _help = (_display displayCtrl IDC_HELP) controlsGroupCtrl IDC_HELP_LIST;
Expand Down
9 changes: 9 additions & 0 deletions docs/wiki/development/coding-guidelines.md
Original file line number Diff line number Diff line change
Expand Up @@ -590,6 +590,15 @@ Event handlers in ACE3 are implemented through the CBA event system (ACE3's own

More information on the [CBA Events System](https://github.com/CBATeam/CBA_A3/wiki/Custom-Events-System){:target="_blank"} and [CBA Player Events](https://github.com/CBATeam/CBA_A3/wiki/Player-Events){:target="_blank"} pages.

<div class="panel info">
<h5>Warning about BIS event handlers:</h5>
<p>BIS's event handlers (`addEventHandler`, `addMissionEventHandler`) are slow when passing a large code variable. Use a short code block that calls the function you want.</p>
```js
player addEventHandler ["Fired", FUNC(handleFired)]; // bad
player addEventHandler ["Fired", {call FUNC(handleFired)}]; // good
```
</div>

### 7.4 Hashes

When a key value pair is required, make use of the hash implementation from ACE3.
Expand Down

0 comments on commit a3750b3

Please sign in to comment.