FlyWithLua scripts

FlywithLua is the Lua script engine for X-Plane, which makes creating and running scripts easy.

I have created some Lua scripts that may be helpful for you too. Sometimes the same variables are used. If you want to use more than one script just delete duplicate variables (or comment out by using –).

 Wind Info Script

Wind_info LUA

This script show the wind heading relative to your plane (where wind is coming from):

Show lua code
-- This script shows the direction relative to your plane. Parts used from Glider wind script (Org Forum)
-- 
-- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
 
-- USER editable values -------------------------------------------------------------------------------
--                                                                                                   --
local window_x = 40             -- Display position from right edge of window
local window_y = 40             -- Display position from top edge of window
--                                                                                                   --
-- STARTUP --------------------------------------------------------------------------
 
require("graphics")
 
DataRef("Wind_WDir", "sim/cockpit2/gauges/indicators/wind_heading_deg_mag", "readonly")
DataRef("Wind_WSpd", "sim/cockpit2/gauges/indicators/wind_speed_kts", "readonly")
DataRef("current_heading", "sim/flightmodel/position/psi", "readonly")
 
local show_wind = false
 
function wind_main() -------------------------------------------------------- MAIN
	if show_wind then 
		XPLMSetGraphicsState(0,0,0,1,1,0,0)
 
		graphics.set_color(0, 0, 0, 0.5)
		graphics.draw_filled_circle(SCREEN_WIDTH - window_x, SCREEN_HIGHT - window_y, 30)
		graphics.set_color(0.0, 0.6, 0.0, 0.7)
		graphics.draw_circle(SCREEN_WIDTH - window_x, SCREEN_HIGHT - window_y, 30, 2)
 
		graphics.set_color(0.0, 0.6, 0.0, 0.7)
		graphics.draw_angle_line(SCREEN_WIDTH - window_x, SCREEN_HIGHT - window_y, Wind_WDir - current_heading + 180, 30)
		graphics.draw_angle_arrow(SCREEN_WIDTH - window_x, SCREEN_HIGHT - window_y, Wind_WDir - current_heading, 30, 15, 1)
		local string = ""
		graphics.set_color(1, 1, 1, 0.8)
		if Wind_WSpd < 10 then
			string = " " .. string.format("% 2.1f", Wind_WSpd)
		else
			string = string.format("% 2.1f", Wind_WSpd)
		end
		draw_string_Helvetica_12(SCREEN_WIDTH - window_x -15, SCREEN_HIGHT - window_y - 5, string)
		draw_string_Helvetica_12(SCREEN_WIDTH - window_x - 14, SCREEN_HIGHT - window_y - 45, "From")
  end
end 
 
function toggle_wind()
	if show_wind then
		show_wind = false
	else
		show_wind = true
	end
end
 
create_command("lua/wind_toggle", "Wind direction toggle", "toggle_wind()", "", "")
do_every_draw("wind_main()")

 Copilot: keeps plane in straight line when taxiing

This helps me to stay on the taxiway when working on checklists etc.

Show lua code
-- Copilot: keeps plane in straight line when taxiing
-----------------------------------------------------
 
dataref("current_heading", "sim/flightmodel/position/psi")
dataref("datRAlt", "sim/cockpit2/gauges/indicators/radio_altimeter_height_ft_pilot")
 
yawforce = dataref_table("sim/flightmodel/forces/N_plug_acf")
planemass = dataref_table("sim/flightmodel/weight/m_total")
 
-- Default values (can be overwritten for each plane)
local copilotfactor = 2
local copilot_active = false
local follow_heading = 0
local deviation = 0
local force = 0
 
--position of the interface
local XMin=SCREEN_WIDTH - 80
local YMin=SCREEN_HIGHT - 100
local XMax=XMin + 70
local YMax=YMin + 20
 
-- Create a custom commands to switch on
function copilot_toggle()
	force = 0 
	if copilot_active then
		copilot_active = false
	else
		if datRAlt > 50 then
			copilot_active = true
			follow_heading = current_heading
			force = math.floor(planemass[0] * copilotfactor)
		end
	end
end
 
function forces_do()
	if copilot_active then
		deviation = math.floor((follow_heading - current_heading + 180) % 360) -180
		if deviation == 0 then
			-- no deviation -> no force
			yawforce[0]= 0
			return
		end
		if deviation > 0 then
			-- deviation to left
			yawforce[0]= force
			return
		end
		if deviation < 0 then
			-- deviation to right
			yawforce[0]= -force
			return
		end
	end
end
 
function show_forces()
	if copilot_active then
		graphics.set_color(0, 0, 0, 0.5)
		graphics.draw_rectangle(XMin, YMin, XMax, YMax)
		graphics.set_color(0.0, 0.6, 0.0, 0.7)
		graphics.set_width(2)
		graphics.draw_line(XMin, YMin, XMin, YMax)
		graphics.draw_line(XMin, YMax, XMax, YMax)
		graphics.draw_line(XMax, YMax, XMax, YMin)
		graphics.draw_line(XMax, YMin, XMin, YMin)
		graphics.set_color(1, 1, 1, 0.8)
		draw_string_Helvetica_12(XMin + 5, YMin + 5, "  Copilot")
	end
end
 
create_command("lua/copilot_toggle", "Copilot helper toggle", "copilot_toggle()", "", "")
do_every_frame("forces_do()")
 
-- Disable show_forces() if it works OK, just for debug (or delete whole function...)
-- do_every_draw("show_forces()")

 CowlFlaps

Binding the CowlFlaps to a Joystick axis for the DC-3

Show lua code
-- CowlFlaps DC-3
-----------------
 
axisValues = dataref_table("sim/joystick/joystick_axis_values")
if PLANE_ICAO == "DC3" or PLANE_ICAO == "DC3F" then
	set_axis_assignment( 33, "none", "normal" )
	set_button_assignment( X52 + 6, "sim/flight_controls/tail_wheel_lock_toggle" )
	DataRef( "datCowlFlapsRatio1", "sim/cockpit2/engine/actuators/cowl_flap_ratio", "writeable", 0)
	DataRef( "datCowlFlapsRatio2", "sim/cockpit2/engine/actuators/cowl_flap_ratio", "writeable", 1)
	do_every_frame([[
    datCowlFlapsRatio1=axisValues[33]
    datCowlFlapsRatio2=axisValues[33]
	]])
end