Fs2020 variable subscribe

From Sim Innovations Wiki
Jump to navigation Jump to search

Description

fs2020_variable_subscribe(variable,unit,callback_function)

fs2020_variable_subscribe is used to subscribe on one or more FS2020 variables.

Return value

This function won't return any value.

Arguments

# Argument Type Description
1 .. n variable String Reference to a variable from Flight Simulator 2020
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.
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 FS2020
 function new_altitude_callback(altitude)
   -- Prints the altitude in the debug window
   print("New altitude: " .. altitude)
 end

 -- subscribe to FS2020 variable
 fs2020_variable_subscribe("INDICATED ALTITUDE", "Feet", new_altitude_callback)

Example (single index variable)

-- This function will be called when new data is available from FS2020
 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 to FS2020 variable
 fs2020_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 fs2020_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
 fs2020_variable_subscribe("INDICATED ALTITUDE", "Feet",
                       "AIRSPEED INDICATED", "Knots", new_altitude_and_speed_callback)