loadScript(file [, mode], [,env])
Load a Lua script file. This is similar to Lua's own loadfile() API method, but it uses OpenTx's optional pre-compilation feature to save memory and time during load.
Return values are same as from Lua API loadfile() method: If the script was loaded w/out errors then the loaded script (or "chunk") is returned as a function. Otherwise, returns nil plus the error message.
@status current Introduced in 2.2.0
Example
fun, err = loadScript("/SCRIPTS/FUNCTIONS/print.lua")
if (fun ~= nil) then
fun("Hello from loadScript()")
else
print(err)
endParameters
file(string) Full path and file name of script. The file extension is optional and ignored (seemodeparam to control which extension will be used). However, if an extension is specified, it should be ".lua" (or ".luac"), otherwise it is treated as part of the file name and the .lua/.luac will be appended to that.mode(string) (optional) Controls whether to force loading the text (.lua) or pre-compiled binary (.luac) version of the script. By default OTx will load the newest version and compile a new binary if necessary (overwriting any existing .luac version of the same script, and stripping some debug info like line numbers). You can usemodeto control the loading behavior more specifically. Possible values are:bonly binary.tonly text.T(default on simulator) prefer text but load binary if that is the only version available.bt(default on radio) either binary or text, whichever is newer (binary preferred when timestamps are equal).Add
xto avoid automatic compilation of source file to .luac version.Eg: "tx", "bx", or "btx".
Add
cto force compilation of source file to .luac version (even if existing version is newer than source file).Eg: "tc" or "btc" (forces "t", overrides "x").
Add
dto keep extra debug info in the compiled binary.Eg: "td", "btd", or "tcd" (no effect with just "b" or with "x").
env(integer) See documentation for Lua function loadfile().
Return value
functionThe loaded script, ornilif there was an error (e.g. file not found or syntax error).stringError message(s), if any. Blank if no error occurred.
Notice
Note that you will get an error if you specify mode as "b" or "t" and that specific version of the file does not exist (eg. no .luac file when "b" is used). Also note that mode is NOT passed on to Lua's loader function, so unlike with loadfile() the actual file content is not checked (as if no mode or "bt" were passed to loadfile()).
Last updated
Was this helpful?