Xpl dataref subscribe

From Sim Innovations Wiki
Jump to navigation Jump to search



xpl_dataref_subscribe is used to subscribe to one or more X-Plane datarefs. You can find the available datarefs here.

Return value

This function won't return any value.


# Argument Type Description
1 .. n dataref String Reference to a dataref from X-Plane (see X-Plane DataRefs)
2 .. n type String Data type of the DataRef, can be INT,FLOAT,DOUBLE,INT[n],FLOAT[n],DOUBLE[n], BYTE[n] or STRING
last callback_function Function The function to call when new data is available

Example (single dataref)

-- This function will be called when new data is available from X-Plane
 function new_altitude_callback(altitude)
   print("New altitude: " .. altitude)

 -- subscribe X-Plane datarefs
 xpl_dataref_subscribe("sim/cockpit2/gauges/indicators/altitude_ft_pilot", "FLOAT", new_altitude_callback)

Example (single array dataref)

-- This function will be called when new data is available from X-Plane
 function new_PT_rpm_callback(engine_ff)
   -- #engine_ff would give the array length
   -- data[1] would give the first object in the array
   print("Array contains " .. #engine_ff .. " items. Data on first position is: " .. engine_ff[1])

 -- subscribe X-Plane datarefs
 xpl_dataref_subscribe("sim/flightmodel/engine/ENGN_FF", "FLOAT[8]", new_PT_rpm_callback)

Example (multi dataref)

-- It is also possible to subscribe to multiple datarefs
 -- Note that the order in which the datarefs are given to the xpl_dataref_subscribe function determines the order in which the datarefs will enter this function

 -- In our case 
 -- "sim/cockpit2/gauges/indicators/altitude_ft_pilot" maps to "altitude"
 -- "sim/cockpit2/gauges/indicators/airspeed_kts_pilot" maps to "speed"
 function new_altitude_and_speed_callback(altitude, speed)
   print("Altitude=" .. altitude .. " Speed=" .. speed)

 -- multi subscribe
 xpl_dataref_subscribe("sim/cockpit2/gauges/indicators/altitude_ft_pilot",  "FLOAT",
                       "sim/cockpit2/gauges/indicators/airspeed_kts_pilot", "FLOAT", new_altitude_and_speed_callback)