---
name: "addEventHandler"
type: command
status: ok
groups: ["Event Handlers"]
arma3_since: "0.50"
locality: {"argument": "global", "effect": "local", "server_exec": false}
source: https://community.bistudio.com/wiki/addEventHandler
revid: 363476
updated: 2024-03-31T14:58:59Z
---

# addEventHandler

*Command · Event Handlers · Arma 3 v0.50 · introduced in Operation Flashpoint v1.85*

Locality: arguments **global** · effect **local**

## Description

Adds an Event Handler to the given object.
- As many Event Handlers of any type can be added - existing Event Handlers do not get overwritten
- Use [removeEventHandler](../commands/removeEventHandler.md) to remove an Event Handler
Read Event Handlers for more information and a list of all available Event Handlers.

## Syntax

```sqf
target addEventHandler [type, code]
```

**Parameters:**

- `target`: [Object](../topics/Object.md) or Arma 3 v2.10 [Group](../topics/Group.md)
- `type`: [String](../topics/String.md) - see Event Handlers for the full list of available options
- `code`: [Code](../topics/Code.md) or [String](../topics/String.md) - code that should be executed when the Event Handler fires; executed in [missionNamespace](../commands/missionNamespace.md) by default. Several [Magic Variables](../topics/Magic_Variables.md) are available:
  - Event Handler parameters are accessible via `_this`
  - The Event Handler type is available as `_thisEvent`
  - The Event Handler index is available as `_thisEventHandler`

**Returns:** [Number](../topics/Number.md) - the index of the added Event Handler. Indices start at 0 for each unit and increment with each added Event Handler.

## Examples

### Example 1

```sqf
this addEventHandler ["Killed", {
    params ["_unit", "_killer"];
    systemChat format ["%1 has been killed by %2.", _unit, _killer];
}];
```

### Example 2

```sqs
_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](../commands/removeEventHandler.md) · [removeAllEventHandlers](../commands/removeAllEventHandlers.md) · [addMPEventHandler](../commands/addMPEventHandler.md) · [addMissionEventHandler](../commands/addMissionEventHandler.md) · [BIS_fnc_addScriptedEventHandler](../functions/BIS_fnc_addScriptedEventHandler.md) · [getEventHandlerInfo](../commands/getEventHandlerInfo.md)

## Community notes

**Killzone_Kid** — 2015-06-07

When using overridable EH, such as "InventoryOpened" and similar, where returning [true](../commands/true.md) allows to override default action, [exitWith](../commands/exitWith.md) cannot be used to return value. So:

```sqf
if (whatever) exitWith {true}; false;
```

Forget about it, will not work. Instead use:

```sqf
if (whatever) then {true} else {false};
```

100% satisfaction guaranteed!

<sub>Source: [BI Community Wiki — addEventHandler](https://community.bistudio.com/wiki/addEventHandler) (revision 363476).</sub>
