XLua2 API Ref XPLMProcessing Latest - 4.4.0-d4

Flight Loop Callbacks


XPLMFlightLoopPhaseType

enum XPLM210

You can register a flight loop callback to run either before or after the flight model is integrated by X-Plane.

Name Value Description
xplm_FlightLoop_Phase_BeforeFlightModel 0 Your callback runs before X-Plane integrates the flight model.
xplm_FlightLoop_Phase_AfterFlightModel 1 Your callback runs after X-Plane integrates the flight model.

XPLMFlightLoopID

typedef XPLM210

This is an opaque identifier for a flight loop callback. You can use this identifier to easily track and remove your callbacks, or to use the new flight loop APIs.

local my_flightLoopID = nil  -- XPLMFlightLoopID

Used by:


XPLMFlightLoop_f

callback

This is your flight loop callback. Each time the flight loop is iterated through, you receive this call at the end.

Flight loop callbacks receive a number of input timing parameters. These input timing parameters are not particularly useful; you may need to track your own timing data (e.g. by reading datarefs). The input parameters are:

  • inElapsedSinceLastCall: the wall time since your last callback.
  • inElapsedTimeSinceLastFlightLoop: the wall time since any flight loop was dispatched.
  • inCounter: a monotonically increasing counter, bumped once per flight loop dispatch from the sim.
  • inRefcon: your own pointer constant provided when you registered yor callback.

Your return value controls when you will next be called.

  • Return 0 to stop receiving callbacks.
  • Return a positive number to specify how many seconds until the next callback. (You will be called at or after this time, not before.)
  • Return a negative number to specify how many loops must go by until you are called. For example, -1.0 means call me the very next loop.

Try to run your flight loop as infrequently as is practical, and suspend it (using return value 0) when you do not need it; lots of flight loop callbacks that do nothing lowers X-Plane's frame rate.

Your callback will NOT be unregistered if you return 0; it will merely be inactive.

function my_FlightLoop_callback(
    inElapsedSinceLastCall,              -- float
    inElapsedTimeSinceLastFlightLoop,    -- float
    inCounter,                           -- int
    inRefcon                             -- any Lua var/table
)
    -- your code here
    return nil  -- float
end

XPLMCreateFlightLoop_t

struct XPLM210

XPLMCreateFlightLoop_t contains the parameters to create a new flight loop callback. The structure may be expanded in future SDKs - always set structSize to the size of your structure in bytes.

local My_CreateFlightLoop_t = {
    structSize    = 0,       -- int
    phase         = nil,     -- XPLMFlightLoopPhaseType
    callbackFunc  = nil,     -- see XPLMFlightLoop_f
    refcon        = nil,     -- any Lua var/table
}

See available callback(s):


XPLMGetElapsedTime

function

This routine returns the elapsed time since the sim started up in decimal seconds. This is a wall timer; it keeps counting upward even if the sim is pasued.

WARNING: XPLMGetElapsedTime is not a very good timer! It lacks precision in both its data type and its source. Do not attempt to use it for timing critical applications like network multiplayer.

-- returns float -> assign to local/var
local my_result = XPLMGetElapsedTime(
)

XPLMGetCycleNumber

function

This routine returns a counter starting at zero for each sim cycle computed/video frame rendered.

-- returns int -> assign to local/var
local my_result = XPLMGetCycleNumber(
)

XPLMCreateFlightLoop

function XPLM210

This routine creates a flight loop callback and returns its ID. The flight loop callback is created using the input param struct, and is inited to be unscheduled. Use XPLMScheduleFlightLoop to schedule it.

-- returns XPLMFlightLoopID -> assign to local/var
local my_flightLoopID = XPLMCreateFlightLoop(
    inParams     -- see XPLMCreateFlightLoop_t
)

See associated types:


XPLMDestroyFlightLoop

function XPLM210

This routine destroys a flight loop callback by ID. Only call it on flight loops created with the newer XPLMCreateFlightLoop API.

XPLMDestroyFlightLoop(
    inFlightLoopID     -- XPLMFlightLoopID
)

See associated types:


XPLMScheduleFlightLoop

function XPLM210

This routine schedules a flight loop callback for future execution. If inInterval is negative, it is run in a certain number of frames based on the absolute value of the input. If the interval is positive, it is a duration in seconds.

If inRelativeToNow is true, times are interpreted relative to the time this routine is called; otherwise they are relative to the last call time or the time the flight loop was registered (if never called).

XPLMScheduleFlightLoop(
    inFlightLoopID,     -- XPLMFlightLoopID
    inInterval,         -- float
    inRelativeToNow     -- boolean
)

See associated types: