addEventHandler: Difference between revisions

From Bohemia Interactive Community
Jump to navigation Jump to search
m (template:command argument fix)
m (template:command argument fix)
Line 17: Line 17:
| object '''addEventHandler''' [type, command] |=
| object '''addEventHandler''' [type, command] |=


|p1= object: [[Object]] |= Parameter 1
|p1= object: [[Object]] |PARAMETER1=


|p2= [type, command]: [[Array]] |= Parameter 2
|p2= [type, command]: [[Array]] |PARAMETER2=


|p3= type: [[String]] - [[:Category:Event Handlers|Event Handler]] type |= Parameter 3
|p3= type: [[String]] - [[:Category:Event Handlers|Event Handler]] type |PARAMETER3=


|p4= command: [[Code]] or [[String]] - code that should be executed once the event occurs, by default executed in [[missionNamespace]] |= Parameter 4
|p4= command: [[Code]] or [[String]] - code that should be executed once the event occurs, by default executed in [[missionNamespace]] |PARAMETER4=


| [[Number]] - The index of the currently added event handler is returned. Indices start at 0 for each unit and increment with each added event handler.|= RETRUNVALUE
| [[Number]] - The index of the currently added event handler is returned. Indices start at 0 for each unit and increment with each added event handler.|= RETRUNVALUE
____________________________________________________________________________________________
____________________________________________________________________________________________
   
   
|x1= <code>_EHkilledIdx <nowiki>=</nowiki> [[player]] [[addEventHandler]] ["killed", {_this [[exec]] "playerKilled.sqs"}]</code> |= Example 1
|x1= <code>_EHkilledIdx <nowiki>=</nowiki> [[player]] [[addEventHandler]] ["killed", {_this [[exec]] "playerKilled.sqs"}]</code> |EXAMPLE1=


|x2= <code>this [[addEventHandler]] ["killed", "[[hint]] [[format]] ['Killed by %1',_this [[select]] 1]"]</code> |= Example 2
|x2= <code>this [[addEventHandler]] ["killed", "[[hint]] [[format]] ['Killed by %1',_this [[select]] 1]"]</code> |EXAMPLE2=
____________________________________________________________________________________________
____________________________________________________________________________________________



Revision as of 15:27, 7 April 2019

Hover & click on the images for description

Description

Description:
Adds event handler (EH) to the given object and returns EH handle. If EH has some data to return upon event (e.g. the "killed" EH will return an array with 2 elements: the killed unit, and the killer), it is passed in _this variable. Since Arma 3 v.1.63.137807 the EH handle is also stored in _thisEventHandler variable and is available during EH code execution.

For more information about event handlers and their types check the scripting topic Event handlers in this reference. You may add as many event handlers of any type as you like to every unit. For instance, if you add an event handler of type "killed" and one already exists, the old one doesn't get overwritten. Use removeEventHandler to delete event handlers.
Multiplayer:
Some event handlers are persistent (i.e. they stay attached to a unit, even after it dies and respawns). Template:EffArg "Killed" and "Hit" eventhandlers are executed where given unit is local.
Groups:
Uncategorised

Syntax

Syntax:
object addEventHandler [type, command]
Parameters:
object: Object
[type, command]: Array
type: String - Event Handler type
command: Code or String - code that should be executed once the event occurs, by default executed in missionNamespace
Return Value:
Number - The index of the currently added event handler is returned. Indices start at 0 for each unit and increment with each added event handler.

Examples

Example 1:
_EHkilledIdx = player addEventHandler ["killed", {_this exec "playerKilled.sqs"}]
Example 2:
this addEventHandler ["killed", "hint format ['Killed by %1',_this select 1]"]

Additional Information

See also:
EventHandlers listaddMPEventHandlerremoveEventHandlerremoveAllEventHandlers

Notes

Report bugs on the Feedback Tracker and/or discuss them on the Arma Discord or on the Forums.
Only post proven facts here! Add Note

Notes

Bottom Section

Posted on July 7, 2015 - 21:06 (UTC)
Killzone Kid
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: if (whatever) exitWith {true}; false; Forget about it, will not work. Instead use: if (whatever) then {true} else {false}; 100% satisfaction guaranteed!
Posted on August 28, 2018 - 12:34 (UTC)
bloodwyn1756
Avoid using exitWith{} in the command statement when passing a return value (f.e. dammage value for HandleDamage).