{"name":"addEventHandler","title":"addEventHandler","type":"command","url":"https://community.bistudio.com/wiki/addEventHandler","revid":363476,"updated":"2024-03-31T14:58:59Z","groups":["Event Handlers"],"status":"ok","games":{"ofp":"1.85","ofpe":"1.00","arma1":"1.00","arma2":"1.00","arma2oa":"1.50","tkoh":"1.00","arma3":"0.50"},"since":"ofp 1.85","arma3_since":"0.50","locality":{"argument":"global","effect":"local"},"description":"Adds an Event Handler to the given object.\n\n- As many Event Handlers of any type can be added - existing Event Handlers do not get overwritten\n- Use removeEventHandler to remove an Event Handler\n\nRead Event Handlers for more information and a list of all available Event Handlers.","syntaxes":[{"syntax":"target addEventHandler [type, code]","params":[{"name":"target","type":"Object or Arma 3 v2.10 Group","type_expr":"Object | Group"},{"name":"type","type":"String","description":"see Event Handlers for the full list of available options","type_expr":"String"},{"name":"code","type":"Code or String","description":"code that should be executed when the Event Handler fires; executed in missionNamespace by default. Several Magic Variables are available:\n\n- Event Handler parameters are accessible via `_this`\n- The Event Handler type is available as `_thisEvent`\n- The Event Handler index is available as `_thisEventHandler`","type_expr":"Code | String"}],"returns":{"type":"Number","type_expr":"Number","description":"the index of the added Event Handler. Indices start at 0 for each unit and increment with each added Event Handler."},"form":"binary","left":{"text":"target"},"right":{"text":"[type, code]"}}],"examples":[{"text":"```sqf\nthis addEventHandler [\"Killed\", {\n    params [\"_unit\", \"_killer\"];\n    systemChat format [\"%1 has been killed by %2.\", _unit, _killer];\n}];\n```","code":["this addEventHandler [\"Killed\", {\n\tparams [\"_unit\", \"_killer\"];\n\tsystemChat format [\"%1 has been killed by %2.\", _unit, _killer];\n}];"]},{"text":"```sqs\n_index = player addEventHandler [\"Killed\", { _this exec \"playerKilled.sqs\" }]\n```","code":["_index = player addEventHandler [\"Killed\", { _this exec \"playerKilled.sqs\" }]"]}],"multiplayer":"Some event handlers are persistent (i.e they stay attached to the unit, even after it dies and respawns).","see_also":["removeEventHandler","removeAllEventHandlers","addMPEventHandler","addMissionEventHandler","BIS_fnc_addScriptedEventHandler","getEventHandlerInfo"],"notes":[{"user":"Killzone_Kid","date":"2015-06-07T21:06:00Z","text":"When using overridable EH, such as \"InventoryOpened\" and similar, where returning true allows to override default action, exitWith cannot be used to return value. So:\n\n```sqf\nif (whatever) exitWith {true}; false;\n```\n\nForget about it, will not work. Instead use:\n\n```sqf\nif (whatever) then {true} else {false};\n```\n\n100% satisfaction guaranteed!"}]}