{"name":"addEventHandler","operator":null,"title":"addEventHandler","type":"command","url":"https://community.bistudio.com/wiki/addEventHandler","revid":363476,"updated":"2024-03-31T14:58:59Z","groups":["Event Handlers"],"status":"ok","replaced_by":[],"games":[{"game":"ofp","name":"Operation Flashpoint","version":"1.85"},{"game":"ofpe","name":"Operation Flashpoint: Elite","version":"1.00"},{"game":"arma1","name":"Armed Assault","version":"1.00"},{"game":"arma2","name":"Arma 2","version":"1.00"},{"game":"arma2oa","name":"Arma 2: Operation Arrowhead","version":"1.50"},{"game":"tkoh","name":"Take On Helicopters","version":"1.00"},{"game":"arma3","name":"Arma 3","version":"0.50"}],"since":{"game":"ofp","name":"Operation Flashpoint","version":"1.85"},"arma3_since":"0.50","branch":null,"locality":{"argument":"global","effect":"local","server_exec":false},"exec":null,"description":"Adds an Event Handler to the given object.\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\nRead Event Handlers for more information and a list of all available Event Handlers.","syntaxes":[{"syntax":"target addEventHandler [type, code]","since":null,"effect":null,"exec":null,"params":[{"name":"target","type":"Object or Arma 3 v2.10 Group","optional":false,"default":null,"description":null,"types":["Object","Group"],"since":null,"text":"target: Object or Arma 3 v2.10 Group"},{"name":"type","type":"String","optional":false,"default":null,"description":"see Event Handlers for the full list of available options","types":["String"],"since":null,"text":"type: String - see Event Handlers for the full list of available options"},{"name":"code","type":"Code or String","optional":false,"default":null,"description":"code that should be executed when the Event Handler fires; executed in missionNamespace by default. Several Magic Variables are available:\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`","types":["Code","String"],"since":null,"text":"code: Code or String - code that should be executed when the Event Handler fires; executed in missionNamespace by default. Several Magic Variables are available:\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`"}],"returns":{"type":"Number","types":["Number"],"description":"the index of the added Event Handler. Indices start at 0 for each unit and increment with each added Event Handler.","text":"Number - the index of the added Event Handler. Indices start at 0 for each unit and increment with each added Event Handler."},"form":"binary","operator":null,"left":{"text":"target","types":["Object","Group"]},"right":{"text":"[type, code]","types":["Array"]}}],"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).","problems":null,"aliases":[],"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!"}],"categories":["ArmA: Armed Assault: Scripting Commands","Arma 2: Operation Arrowhead: Scripting Commands","Arma 2: Scripting Commands","Arma 3: Scripting Commands","Command Group: Event Handlers","Introduced with Operation Flashpoint version 1.85","Operation Flashpoint: Elite: Scripting Commands","Operation Flashpoint: New Scripting Commands","Operation Flashpoint: Scripting Commands","Scripting Commands","Scripting Commands: Local Effect","Take On Helicopters: Scripting Commands"]}