Lua tips & tricks
Jump to navigation
Jump to search
This page contains some lua tips & tricks that might be you creating better code and instruments
Inline functions
Lua allows you to create inline functions. With the function inline you can reduce code and keep things more organized. Below you will find an example without and one with function inlining.
-- Typical way for a callback
function callback()
print("Button has been pressed")
end
button_add("pressed.png", "released.png", 0, 0, 256, 256, callback)
-- Inline the callback
button_add("pressed.png", "released.png", 0, 0, 256, 256, function()
print("Button has been pressed")
end)
You can apply this trick with all API functions that require a callback function, like timer_start, xpl_dataref_subscribe, fsx_variable_subscribe, etc. etc.)
Inline boolean statements
You can reduce code when inlining your boolean statements. See example below.
-- Typical way
value = 12
if value > 2 then
visible(txt_id, true)
else
visible(txt_id, false)
end
-- Inline
visible(txt_id, value > 2)
-- Combining multiple variables
some_value = 12
some_boolean = true
some_state = "INIT"
visible(txt_id, ( (some_value > 2) and some_boolean) or (some_state == "INIT") )