Fsx variable subscribe

From Sim Innovations Wiki
Jump to navigation Jump to search

Description

fsx_variable_subscribe(variable,unit,callback_function)

fsx_variable_subscribe is used to subscribe on one or more FSX or Prepar3D variables.

You can find the available variables for FSX here and the Prepar3D variables here (official).

Return value

This function won't return any value.

Arguments

# Argument Type Description
1 .. n variable String Reference to a variable from Flight Simulator X or Prepar3D
2 .. n unit String Unit of the variable, can be found with the variable. Different units can be used for different variables. For example Hours, Knots and RPM (look here for FSX units and here for Prepar3D)
last callback_function Function The function to call when new data is available

Examples

Example (single variable)

-- This function will be called when new data is available from FSX or Prepar3D
 function new_altitude_callback(altitude)
   -- Prints the altitude in the debug window
   print("New altitude: " .. altitude)
 end

 -- subscribe to FSX / Prepar3D variables on the databus
 fsx_variable_subscribe("INDICATED ALTITUDE", "Feet", new_altitude_callback)

Example (single index variable)

-- This function will be called when new data is available from FSX or Prepar3D
 function new_rpm_callback(engine_rpm)
   -- Prints the RPM for engine 2 in the debug window
   print("The RPM for engine 2 is: " .. engine_rpm)
 end

 -- subscribe FSX / Prepar3D variables on the databus
 fsx_variable_subscribe("GENERAL ENG RPM:2", "Rpm", new_rpm_callback)

Example (multi variable)

-- It is also possible to subscribe to multiple variables
 -- Note that the order in which the variables are given to the fsx_variable_subscribe function determines the order in which the variables will enter the callback function

 -- In our case 
 -- "INDICATED ALTITUDE" maps to "altitude"
 -- "AIRSPEED INDICATED" maps to "speed"
 function new_altitude_and_speed_callback(altitude, speed)
   print("Altitude=" .. altitude .. " Speed=" .. speed)
 end

 -- multi subscribe
 fsx_variable_subscribe("INDICATED ALTITUDE", "Feet",
                       "AIRSPEED INDICATED", "Knots", new_altitude_and_speed_callback)

LVAR example (single variable)

-- This function will be called when new data is available from FSX or Prepar3D
 function new_rpm_callback(engine_rpm)
   -- Prints the A2A Cessna 172 engine RPM in the debug window
   print("The RPM is: " .. engine_rpm)
 end

 -- Subscribe FSX / Prepar3D A2A Cessna 172 LVAR on the databus
 -- Use L: in front of the LVAR name to indicate that it is an LVAR you are using
 fsx_variable_subscribe("L:Eng1_RPM", "RPM", new_rpm_callback)

LVAR example (multi variable)

-- This function will be called when new data is available from FSX or Prepar3D
 function new_data(engine_rpm, hobbs)
   -- Prints the A2A Cessna 172 engine RPM and engine hours in the debug window. Use \n to create a new line when printing in the debug window.
   print("The RPM is: " .. engine_rpm .. "\nThe number of hobbs hours is: " .. hobbs)
 end

 -- Subscribe FSX / Prepar3D A2A Cessna 172 LVAR on the databus
 -- Use L: in front of the LVAR name to indicate that it is an LVAR you are using
 fsx_variable_subscribe("L:Eng1_RPM", "RPM",
                        "L:Counter1Hours", "hours", new_data)

PMDG example (multi variable)

-- Subscribing to a PMDG variable works the same as FSX variables.
 -- Be sure to use the exact same key and type name from the PMDG variable list.

 function new_PMDG_data_callback(fqtyc, startvalve1, startvalve2, domeswitch)
   print("Fuel quantity center = " .. fqtyc .. "\nStarter valve 1 open = " .. tostring(startvalve1) .. "\nStarter valve 2 open = " .. tostring(startvalve2) .. "\nDome light switch setting = " .. domeswitch)
 end
 
 -- multi subscribe
 fsx_variable_subscribe("PMDG 737NGX:FUEL_QtyCenter", "NUMBER",
                        "PMDG 737NGX:ENG_StartValve:1", "BOOL",
                        "PMDG 737NGX:ENG_StartValve:2", "BOOL",
                        "PMDG 737NGX:LTS_DomeWhiteSw", "ENUM", new_PMDG_data_callback)

Add-on aircraft and sources

Add-on Description
PMDG_737NGX variables Variable list for the PMDG 737 NGX 800 and 900