Event Handlers – Arma 2
Introduction
An event handler (abbreviated to EH) allows you to automatically monitor and then execute custom code upon particular events being triggered.
These event handlers relate to commands such as: addEventHandler, removeEventHandler, removeAllEventHandlers.
General MP Note
As opposed to OFP event handlers, in ArmA 2 a respawned unit retains the EHs it had before, so it is no longer required to re-add EHs after respawning.
The new MP event handler "MPHit" and "MPKilled" have to be assigned to the object by using addMPEventHandler.
Event Scripts
See also: Event Scripts for special event triggered scripts.
Event Handler List
AnimChanged
Trigerred everytime a new animation is started.
Passed array: [unit, anim]
- unit: Object - Object the event handler is assigned to
- anim: String - Name of the anim that is given by playMove/switchMove/playAction/switchAction
AnimDone
Trigerred everytime an animation is finished.
Unknown MP behaviour. Probably local.
Passed array: [unit, anim]
- unit: Object - Object the event handler is assigned to
- anim: String - Name of the anim that has been finished
AnimStateChanged
Available in Arma 2 only.
Trigerred everytime an animation state changes. Unlike AnimChanged and AnimDone, it is triggered for all animation states in a sequence.
Unknown MP behaviour. Probably local.
Passed array: [unit, anim]
- unit: Object - Object the event handler is assigned to
- anim: String - Name of the anim that has been started
Dammaged
Triggered when the unit is damaged. In ArmA works with all vehicles not only men like in OFP.
(If simultaneous damage occured (e.g. via grenade) EH might be triggered several times.)
Passed array: [unit, selectionName, damage]
- unit: Object - Object the event handler is assigned to
- selectionName: String - Name of the selection where the unit was damaged
- damage: Number - Resulting level of damage
Engine
Triggered when the engine of the unit is turned on/off.
Passed array: [vehicle, engineState]
- vehicle: Object - Vehicle the event handler is assigned to
- engineState: Boolean - True when the engine is turned on, false when turned off
Fired
Triggered when the unit fires a weapon.
This EH will not trigger if a unit fires out of a vehicle. For those cases an EH has to be attached to that particular vehicle.
Passed array: [unit, weapon, muzzle, mode, ammo]
b1.54.73642 : [unit, weapon, muzzle, mode, ammo, magazine, projectile]
- unit: Object - Object the event handler is assigned to
- weapon: String - Fired weapon
- muzzle: String - Muzzle that was used
- mode: String - Current mode of the fired weapon
- ammo: String - Ammo used
- magazine String - magazine name which was used
- projectile object - Object of the projectile that was shot
FiredNear
Available in Arma 2 only.
Triggered when a weapon is fired somewhere near the unit or vehicle.
It is also triggered if the unit itself is firing.
(Exception(s): the Throw weapon wont broadcast the FiredNear event)
Passed array: [unit, firer, distance, weapon, muzzle, mode, ammo]
- unit: Object - Object the event handler is assigned to
- firer: Object - Object which fires a weapon near the unit
- distance: Number - Distance in meters between the unit and firer (max. distance ~69m)
- weapon: String - Fired weapon
- muzzle: String - Muzzle that was used
- mode: String - Current mode of the fired weapon
- ammo: String - Ammo used
Fuel
Triggered when the unit's fuel status changes between completely empty / not empty (only useful if the event handler is assigned to a vehicle).
Passed array: [vehicle, fuelState]
- vehicle: Object - Vehicle the event handler is assigned to
- fuelState: Boolean - 0 when no fuel, 1 when the fuel tank is full
Gear
Triggered when the unit lowers/retracts the landing gear (only useful if the event handler is assigned to is a member of the class "Plane").
Global. Passed array: [vehicle, gearState]
- vehicle: Object - Vehicle the event handler is assigned to
- gearState: Boolean - True when the gear is lowered, false when retracted
GetIn
Triggered when a unit enters the object (only useful when the event handler is assigned to a vehicle). It does not trigger upon a change of positions within the same vehicle. It also is not triggered by the moveInX commands.
Passed array: [vehicle, position, unit]
- vehicle: Object - Vehicle the event handler is assigned to
- position: String - Can be either "driver", "gunner", "commander" or "cargo"
- unit: Object - Unit that entered the vehicle
GetOut
Triggered when a unit gets out from the object, works the same way as GetIn.
Passed array: [vehicle, position, unit]
- vehicle: Object - Vehicle the event handler is assigned to
- position: String - Can be either "driver", "gunner", "commander" or "cargo"
- unit: Object - Unit that leaved the vehicle
HandleDamage
Triggered when the unit is damaged. Works with all vehicles. C
ode provided must return a value what should be the damage of given part after processing.
Adding this eventhandler with no return value or with return value 0, replaces damage handling by the engine, making the object invulnerable if damage is not scripted in the eventhandler. If you want default processing to be done, be sure to return _this select 2.
You can setDamage and setHit in combination for additional damage handling.
Locality: HandleDamage is triggered where the unit is local. See also Locality in Multiplayer.
This EH is triggered separately for every selection of the damaged object.
Passed array: [unit, selectionName, damage, source, projectile]
- unit: Object - Object the event handler is assigned to.
- selectionName: String - Name of the selection where the unit was damaged. "" for over-all structural damage, "?" for unknown selections.
- damage: Number - Resulting level of damage for the selection.
- source: Object - The source unit that caused the damage.
- projectile: String - Classname of the projectile that caused inflicted the damage. ("" for unknown, such as falling damage.)
Example:
HANDLE = TargetSoldier addeventhandler ["HandleDamage",{diag_log text format ["T=%1 : %2", time, _this];_this select 2;} ];
When shooting the soldier once in the head, and once in the torso, ArmA2.RPT shows:
T=6.754 : [TargetSoldier,"",0.350463,B 1-1-A:1 (Matthijs),"B_556x45_Ball"] T=6.754 : [TargetSoldier,"head_hit",3.23586,B 1-1-A:1 (Matthijs),"B_556x45_Ball"] T=6.754 : [TargetSoldier,"body",0.0743558,B 1-1-A:1 (Matthijs),"B_556x45_Ball"] T=6.754 : [TargetSoldier,"hands",0.667459,B 1-1-A:1 (Matthijs),"B_556x45_Ball"] T=6.754 : [TargetSoldier,"legs",0.00307311,B 1-1-A:1 (Matthijs),"B_556x45_Ball"] T=10.078 : [TargetSoldier,"",0.350179,B 1-1-A:1 (Matthijs),"B_556x45_Ball"] T=10.078 : [TargetSoldier,"head_hit",0.230999,B 1-1-A:1 (Matthijs),"B_556x45_Ball"] T=10.078 : [TargetSoldier,"body",1.52881,B 1-1-A:1 (Matthijs),"B_556x45_Ball"] T=10.078 : [TargetSoldier,"hands",0.105206,B 1-1-A:1 (Matthijs),"B_556x45_Ball"] T=10.078 : [TargetSoldier,"legs",0.0211435,B 1-1-A:1 (Matthijs),"B_556x45_Ball"]
Link to explanation Celery's explanation
HandleHeal
Triggered when unit starts to heal (player using heal action or AI heals after being ordered). If code returns false, engine side healing follows. Return true if you handle healing in script, use AISFinishHeal to tell engine that script side healing is done. See also lifeState and setUnconscious commands.
Passed array: [unit, healer, healercanheal]
Hit
Triggered when the unit is hit/damaged.
Is not always triggered when unit is killed by a hit.
Most of the time only the killed event handler is triggered when a unit dies from a hit.
The hit EH will not necessarily fire if only minor damage occurred (e.g. firing a bullet at a tank), even though the damage increased.
Does not fire when a unit is set to allowDamage false.
Passed array: [unit, causedBy, damage]
- unit: Object - Object the event handler is assigned to
- causedBy: Object - Object that caused the damage.
Contains the unit itself in case of collisions. - damage: Number - Level of damage caused by the hit
Init
Triggered on mission start or when a vehicle is created on the fly using createVehicle.
Passed array: [unit]
- unit: Object - Object the event handler is assigned to
IncomingMissile
Triggered when a guided missile locked on the target or unguided missile or rocket aimed by AI at the target was fired.
Passed array: [unit, ammo, whoFired]
- unit: Object - Object the event handler is assigned to
- ammo: String - Ammo type that was fired on the unit
- whoFired: Object - Object that fired the weapon
Killed
Triggered when the unit is killed.
Passed array: [unit, killer]
- unit: Object - Object the event handler is assigned to
- killer: Object - Object that killed the unit
Contains the unit itself in case of collisions.
Be careful when the killer has been a vehicle. For most cases the reference of the vehicle is the same as the effectiveCommander, yet not always. For example:
_gunner setVariable ["Variable",true]; _killer getVariable "Variable" => <null> (gunner (vehicle _killer)) getVariable "Variable" => true
And sample debug output:
_killer => B 1-2-F:2 (gunner (vehicle _killer)) => B 1-2-F:2 ((gunner (vehicle _killer)) == _killer) => false
LandedTouchDown
Triggered when a plane (AI or player) touches the ground.
Passed array: [plane, airportID]
- plane: Object - Object the event handler is assigned to
- airportID: Number - ID of the airport (-1 for anything else).
LandedStopped
Triggered when an AI pilot would get out usually. Not executed for player.
Passed array: [plane, airportID]
- plane: Object - Object the event handler is assigned to
- airportID: Number - ID of the airport (-1 for anything else).
Respawn
Triggered when a unit respawns.
Passed array: [unit, corpse, ...]
- unit: Object - Object the event handler is assigned to
- corpse: Object - Object the event handler was assigned to, aka the corpse/unit player was previously controlling.
WeaponAssembled
Triggers when weapon gets assembled.
Passed array: [unit, weapon]
- unit: object - Object the event handler is assigned to
- weapon: object - Object of the assembled weapon.
WeaponDisassembled
Triggers when weapon gets disassembled.
Passed array: [unit, primaryBag, secondarybag]
- unit: object - Object the event handler is assigned to
- primaryBag: object - First backpack object which was weapon dissasembled into...
- secondarybag: object - Second backpack object which was weapon dissasembled into...
MPHit
Triggered when the unit is hit/damaged.
Is not always triggered when unit is killed by a hit.
Most of the time only the killed event handler is triggered when a unit dies from a hit.
The hit EH will not necessarily fire if only minor damage occurred (e.g. firing a bullet at a tank), even though the damage increased.
Does not fire when a unit is set to allowDamage false.
Passed array: [unit, causedBy, damage]
- unit: Object - Object the event handler is assigned to
- causedBy: Object - Object that caused the damage.
Contains the unit itself in case of collisions. - damage: Number - Level of damage caused by the hit
Notes:
This command must be used in conjunction with the new command AddMPEventHandler rather than the old command used for non MP commands.
http://community.bistudio.com/wiki/addMPEventHandler
MPKilled
Triggered when the unit is killed.
Passed array: [unit, killer]
- unit: Object - Object the event handler is assigned to
- killer: Object - Object that killed the unit
Contains the unit itself in case of collisions.
Notes:
This command must be used in conjunction with the new command AddMPEventHandler rather than the old command used for non MP commands. This event handler is triggered whenever any player disconnects.
http://community.bistudio.com/wiki/addMPEventHandler
Coding advice: Call a function from the MPKilled EH code space, rather to define the full code in there directly. The reason is the code space will be transferred over network on each event activation - so keep the data as small as possible!
//good _unit addMPEventHandler ["MPKilled",{_this spawn MyTag_Function;}]; //bad _unit addMPEventHandler ["MPKilled",{ /* complete script code directly in here */ }];
MPRespawn
Triggered when a unit respawns.
Passed array: [unit, corpse, ...]
- unit: Object - Object the event handler is assigned to
- corpse: Object - Object the event handler was assigned to, aka the corpse/unit player was previously controlling.
Notes:
This command must be used in conjunction with the new command AddMPEventHandler rather than the old command used for non MP commands.