addEventHandler: Difference between revisions

From Bohemia Interactive Community
Jump to navigation Jump to search
m (Text replacement - "[[Category:Scripting Commands ArmA|" to "[[Category:Scripting Commands Armed Assault|")
m (Undo revision 363475 by R3vo (talk))
Tag: Undo
 
(56 intermediate revisions by 4 users not shown)
Line 1: Line 1:
{{Command|Comments=
{{RV|type=command
____________________________________________________________________________________________


| ofpr |=
|game1= ofp
|version1= 1.85


|1.85|Game version=
|game2= ofpe
|version2= 1.00


|arg= global |MParg=
|game3= arma1
|version3= 1.00


|eff= local |MPEFFECTS=
|game4= arma2
____________________________________________________________________________________________
|version4= 1.00


| 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 <tt>_this</tt> variable. Since Arma 3 v.1.63.137807 the EH handle is also stored in <tt>_thisEventHandler</tt> variable and is available during EH code execution.<br><br>For more information about event handlers and their types check the scripting topic [[:Category:Event Handlers|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.
|game5= arma2oa
|DESCRIPTION=
|version5= 1.50
____________________________________________________________________________________________


| object '''addEventHandler''' [type, command] |=
|game6= tkoh
|version6= 1.00


|p1= object: [[Object]] |PARAMETER1=
|game7= arma3
|version7= 0.50


|p2= [type, command]: [[Array]] |PARAMETER2=
|arg= global


|p3= type: [[String]] - [[:Category:Event Handlers|Event Handler]] type |PARAMETER3=
|eff= local


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


| [[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
|descr= 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]] to remove an Event Handler
|x1= <code>_EHkilledIdx <nowiki>=</nowiki> [[player]] [[addEventHandler]] ["killed", {_this [[exec]] "playerKilled.sqs"}]</code> |EXAMPLE1=
Read [[:Category:Event Handlers|Event Handlers]] for more information and a list of all available Event Handlers.


|x2= <code>this [[addEventHandler]] ["killed", "[[hint]] [[format]] ['Killed by %1',_this [[select]] 1]"]</code> |EXAMPLE2=
|mp= Some event handlers are persistent (i.e they stay attached to the unit, even after it dies and respawns).
____________________________________________________________________________________________


|mp= Some event handlers are persistent (i.e. they stay attached to a unit, even after it dies and respawns).
|s1= target [[addEventHandler]] [type, code]


{{EffArg|cmd|arg|loc}} "Killed" and "Hit" eventhandlers are executed where given unit is local. |Multiplayer=
|p1= target: [[Object]] or {{GVI|arma3|2.10|size= 0.75}} [[Group]]


| [[:Category:Event Handlers|EventHandlers list]], [[addMPEventHandler]], [[removeEventHandler]], [[removeAllEventHandlers]] |SEEALSO=
|p2= type: [[String]] - see [[:Category:Event Handlers|Event Handlers]] for the full list of available options


}}
|p3= code: [[Code]] or [[String]] - code that should be executed when the Event Handler fires; executed in [[missionNamespace]] by default. Several [[Magic Variables]] are available:
* Event Handler parameters are accessible via <sqf inline>_this</sqf>
* The Event Handler type is available as <sqf inline>_thisEvent</sqf>
* The Event Handler index is available as <sqf inline>_thisEventHandler</sqf>
 
|r1= [[Number]] - the index of the added Event Handler. Indices start at 0 for each unit and increment with each added Event Handler.


<h3 style="display:none">Notes</h3>
|x1= <sqf>
<dl class="command_description">
this addEventHandler ["Killed", {
<!-- Note Section BEGIN -->
params ["_unit", "_killer"];
<!-- Note Section END -->
systemChat format ["%1 has been killed by %2.", _unit, _killer];
</dl>
}];
</sqf>


<h3 style="display:none">Bottom Section</h3>
|x2= <sqs>_index = player addEventHandler ["Killed", { _this exec "playerKilled.sqs" }]</sqs>
[[Category:Scripting Commands|ADDEVENTHANDLER]]
[[Category:Scripting Commands OFP 1.99|ADDEVENTHANDLER]]
[[Category:Scripting Commands OFP 1.96|ADDEVENTHANDLER]]
[[Category:Scripting Commands Armed Assault|ADDEVENTHANDLER]]
[[Category:Command_Group:_Activators|{{uc:{{PAGENAME}}}}]]
[[Category:Scripting Commands Arma 2|{{uc:{{PAGENAME}}}}]]
[[Category:Scripting Commands Arma 3|{{uc:{{PAGENAME}}}}]]
[[Category:Scripting Commands Take On Helicopters|{{uc:{{PAGENAME}}}}]]


<!-- CONTINUE Notes -->
|seealso= [[removeEventHandler]] [[removeAllEventHandlers]] [[:Category:Event Handlers|Event Handlers]] [[addMPEventHandler]] [[addMissionEventHandler]] [[BIS_fnc_addScriptedEventHandler]] [[getEventHandlerInfo]]
<dl class="command_description">
}}
<dd class="notedate">Posted on July 7, 2015 - 21:06 (UTC)</dd>
 
<dt class="note">[[User:Killzone Kid|Killzone Kid]]</dt>
{{Note
<dd class="note">
|user= 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:
|timestamp= 20150607210600
<code>[[if]] (whatever) [[exitWith]] {[[true]]}; [[false]];</code>
|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:
<sqf>if (whatever) exitWith {true}; false;</sqf>
Forget about it, will not work. Instead use:
Forget about it, will not work. Instead use:
<code>[[if]] (whatever) [[then]] {[[true]]} [[else]] {[[false]]};</code>
<sqf>if (whatever) then {true} else {false};</sqf>
100% satisfaction guaranteed!
100% satisfaction guaranteed!
</dd>
}}
</dl>
<!-- DISCONTINUE Notes -->

Latest revision as of 16:58, 31 March 2024

Hover & click on the images for description

Description

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 to remove an Event Handler
Read Event Handlers for more information and a list of all available Event Handlers.
Multiplayer:
Some event handlers are persistent (i.e they stay attached to the unit, even after it dies and respawns).
Groups:
Event Handlers

Syntax

Syntax:
target addEventHandler [type, code]
Parameters:
target: Object or Arma 3 logo black.png2.10 Group
type: String - see Event Handlers for the full list of available options
code: Code or String - code that should be executed when the Event Handler fires; executed in missionNamespace by default. Several Magic Variables are available:
Return Value:
Number - the index of the added Event Handler. Indices start at 0 for each unit and increment with each added Event Handler.

Examples

Example 1:
this addEventHandler ["Killed", { params ["_unit", "_killer"]; systemChat format ["%1 has been killed by %2.", _unit, _killer]; }];
Example 2:
_index = player addEventHandler ["Killed", { _this exec "playerKilled.sqs" }]

Additional Information

See also:
removeEventHandler removeAllEventHandlers Event Handlers addMPEventHandler addMissionEventHandler BIS_fnc_addScriptedEventHandler getEventHandlerInfo

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
Killzone_Kid - c
Posted on Jun 07, 2015 - 21:06 (UTC)
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!