Dataref


The dataref class allows you to interact with X-Plane via datarefs. The simple idea is that you create a Dataref object and then use it throughout your program to access or set (if writeable) that dataref.

Import

The Dataref class is in the XPyDeck library, so you import it in main.py with from XPyDeck import Dataref.

Constructor

ConstructorDescription
Dataref(net:Network,name:str,index:int,writeable:bool)Creates a Dataref object using the Network object you created earlier and the name of the Dataref, eg sim/cockpit2/annunciators/stall_warning. Indeces are optional, however, if the dataref expects an index, be sure to provide one! Writeable is self-explanatory and defaults to False.

Functions

All functions are non-static, so an example call would be: stallWarning.get()or flaps.set(0.5).

MethodDescriptionOutput/Return
get()Gets the current value of the datarefReturns the most recent float value of the dataref that could be accessed. If there in an exception, it will be printed.
set(float)Sets the dataref to the argument (currently has to be a float).Returns True if the command to set the dataref to the new value was successfully sent. Returns False if the dataref is not writeable or if there was an exception in sending the request. The exception can be accessed with .latestException

Instance Variables

VariableDescription
latestExceptionThe latest exception in sending the request to set the dataref. See above. None if there no exceptions have been encountered.
nameThe dataref name. (Use constructor to set instead)
indexIndex. (Use constructor to set instead)
valueMost recent returned value. Starts at 0.0. (Use method to access instead)
❗️

Note that even if there is no exception in sending the request to get or set the dataref, the request can still fail to fetch or set data from/in X-Plane; no exception only means that the request was successfully sent to the script running on your computer, not X-Plane.

Example Code Snippet

from XPyDeck import Network, Dataref

net = Network() # See Network docs

stallWarning = Dataref(net, "sim/cockpit2/annunciators/stall_warning")

stallWarning.get() # 0.0 if off, 1.0 if on

throttle = Dataref(net, "sim/cockpit2/engine/actuators/throttle_jet_rev_ratio", 0, True)
throttle.get()
throttle.set(0)

if throttle.latestException == None:
  print("Request sent to set Dataref")
else:
	print(f"Exception in sending request: {throttle.latestException}")
👍

You don't really need to ensure that the request to set the dataref was sent successfully as most of the time you set a dataref will be in a loop to keep it up to date with the hardware. However, if you need to make sure the new value was sent, this may be handy.