FlyWithLua scripts

FlywithLua is the Lua 5.1 script engine for X-Plane, which makes creating and running scripts easy.
You can download FlyWithLua here: Forum download and how to start First Steps

-- FlyWithLua Hello World script
 
logMsg("Hello World!")

I have created some Lua scripts that may be helpful for you too. The following scripts are available:

NB: sometimes the same variable is used in the various scripts -> just delete duplicate variables (or comment out by using –).


 

Wind Info Script

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

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()")

 

Dampen Toe Brakes

This script helps to dampen the toe brakes if they are too sensitive. It makes it easier to slowly brake.

Show lua code
-- Dampen toe brakes: helps make the toe brakes less sensitive
--------------------------------------------------------------
 
-- Axis number: from rudder panels
axisValues = dataref_table("sim/joystick/joystick_axis_values")
local brakel_axis = 41	-- Axis left
local brakel_val = 0
local braker_axis = 40	-- Axis right
local braker_val = 0
local brake_factor = 2	-- Lineair = 1, higher = more dampened
local brake_plane = 1	-- Change the brake factor or this factor for light planes
 
function Dampen_brake()
	brakel_val = axisValues[brakel_axis]
	brakel_val = (brakel_val ^ brake_factor) * brake_plane
	set("sim/flightmodel/controls/l_brake_add",brakel_val)
 
	braker_val = axisValues[braker_axis]
	braker_val = (braker_val ^ brake_factor) * brake_plane
	set("sim/flightmodel/controls/r_brake_add",braker_val)
end
do_every_frame("Dampen_brake()")
 
function Show_brake()
	string = string.format("% 2.0f", axisValues[brakel_axis] *100)
	draw_string_Helvetica_12(SCREEN_WIDTH - 100, SCREEN_HIGHT - 200, string)
	string = string.format("% 2.0f", brakel_val *100)
	draw_string_Helvetica_12(SCREEN_WIDTH - 70, SCREEN_HIGHT - 200, string)
	string = string.format("% 2.0f", axisValues[braker_axis] *100)
	draw_string_Helvetica_12(SCREEN_WIDTH - 100, SCREEN_HIGHT - 215, string)
	string = string.format("% 2.0f", braker_val *100)
	draw_string_Helvetica_12(SCREEN_WIDTH - 70, SCREEN_HIGHT - 215, string)
end
-- Enable next line of you want to monitor the values
-- do_every_draw("Show_brake()")

 

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