Difference between revisions of "DCS func addEventHandler"

From DCS World Wiki - Hoggitworld.com
Line 45: Line 45:
 
|optType2=
 
|optType2=
  
|examples=  
+
|examples= The following displays a message detailing the event that occurred:
 +
 
 +
  local e = {}
 +
  function e:onEvent(event)
 +
      local m = {}
 +
      m[#m+1] = "Event ID: "
 +
      m[#m+1] = event.id
 +
      if event.initiator then
 +
          m[#m+1] = "\nInitiator : "
 +
          m[#m+1] = event.initiator:getName()
 +
      end
 +
      if event.weapon then
 +
          m[#m+1] = "\nWeapon : "
 +
          m[#m+1] = event.weapon :getTypeName()
 +
      end
 +
      if event.target then
 +
          m[#m+1] = "\nTarget : "
 +
          m[#m+1] = event.target :getName()
 +
      end
 +
     
 +
 
 +
      trigger.action.outText(table.concat(m), 60)
 +
  end
 +
   
 +
    world.addEventHandler(e)
  
  

Revision as of 00:58, 18 March 2022

Scripting Root

Envrioment: Mission Scripting
Function: addEventHandler Added with: 1.2.0
Member Of: world
Syntax: function world.addEventHandler(EventHandler handler )
Description: Adds a function as an event handler that executes whenever a simulator event occurs. The most common uses of event handlers are to track statistics of units within a given scenario and to execute code based on certain events occurring. Handlers are passed event tables which contains numerous data regarding the event.

For examples of the event tables returned, reference the event page on the wiki to get more information regarding the event.

Event Types: shot, hit, takeoff, land, crash, ejection, refueling, dead, pilot_dead, base_captured, mission_start, mission_end, took_control, refueling_stop, birth, human_failure, detailed_failure, engine_startup, engine_shutdown, player_enter_unit, player_leave_unit, player_comment, shooting_start, shooting_end, mark_added, mark_change, mark_remove, kill, score, unit_lost, landing_after_ejection, discard_chair_after_ejection, weapon_add, landing_quality_mark, ai_abort_mission, weapon_drop, runway_takeoff, runway_touch,


Return Value: function
Return Example:
Examples: The following displays a message detailing the event that occurred:
  local e = {}
  function e:onEvent(event)
      local m = {}
      m[#m+1] = "Event ID: "
      m[#m+1] = event.id
      if event.initiator then 
         m[#m+1] = "\nInitiator : "
         m[#m+1] = event.initiator:getName()
      end
      if event.weapon then 
         m[#m+1] = "\nWeapon : "
         m[#m+1] = event.weapon :getTypeName()
      end 
      if event.target then 
         m[#m+1] = "\nTarget : "
         m[#m+1] = event.target :getName()
      end 
     
      trigger.action.outText(table.concat(m), 60)
  end
   
   world.addEventHandler(e)
Related Functions: World Functions: addEventHandler, removeEventHandler, getPlayer, getAirbases, searchObjects, getMarkPanels, removeJunk
Notes: