Skip to content

Lua Scripting

MOARdV edited this page Jun 3, 2017 · 3 revisions

Lua scripting provides a great deal of extensibility to MAS. Using a Lua script in a collider action or variable makes it possible to do far more than RPM or even basic MAS allows.

Lua scripts themselves should be placed in text files, with an extension of ".lua" by convention. The Lua functionality is provided via the MoonSharp C# Lua interpreter. For MAS, MoonSharp is configured in "Hard Sandbox" mode, meaning that the only Lua standard libraries that are enabled are the Strings library, the Math library, global constants, and tables and table iterators.

To allow MAS to load these Lua scripts into the MAS script environment, a config node must be included somewhere in a config file that tells MAS about these Lua scripts, such as (from MOARdV/Scripts/MAS_Scripts.cfg):

MAS_LUA
{
	name = MAS_Scripts

	script = MOARdV/Scripts/MAS_Clock.lua
	script = MOARdV/Scripts/MAS_MJComp.lua
}

The name field simply provides a name for the node. The name is used when reporting errors to make it easier to track down the problem script.

Each Lua script should be listed on a separate script line, as shown above.

Within each Lua script file, one or more functions and local variables may be listed. For instance, the MAS_Clock.lua script listed above includes four Lua functions, such as

-- Return either UT (mode 1) or MET (mode 2).  Or 0 if the unit is off.
function MAS_Clock_Time()
	if fc.GetPersistentAsNumber("MAS_Clock_On") < 1 then
		return 0
	elseif fc.GetPersistentAsNumber("MAS_Clock_ClockMode") > 0 then
		-- MET
		local met = fc.MET()
		-- Clamp MET to display limits
		met = math.min(met, 3599999)
		return met
	else
		-- Only keep HH/MM/SS of UT
		return fc.TimeOfDay(fc.UT())
	end
end

There are no MAS-imposed restrictions on how many functions go in a single script, but it's a good rule-of-thumb to organize each script file around a common theme. For instance, MAS_Clock.lua has the functions relevant to the Clock / Time panel.

Clone this wiki locally