Xpl dataref subscribe

From Sim Innovations Wiki
Jump to navigation Jump to search



xpl_dataref_subscribe is used to subscribe on one or more X-plane datarefs.

X-plane uses the following datarefs, these may be used subscribed to with this function. 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 [1])
2 .. n type String Data type of the DataRef, can be INT,FLOAT,DOUBLE,INT[n],FLOAT[n],DOUBLE[n], BYTE[n] or STRING (see X-plane DataRefs)
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)