Difference between revisions of "NETIO Lua Tips & Philosophy"
Line 27: | Line 27: | ||
== Global variables == | == Global variables == | ||
− | For sharing variables between scripts, their initialization must be done without keyword '''local'''. Conventions in Lua specify that global variables should start with '''_G.''' (e.q. _G.globalVariable = true). After initialization global variable is available in all scripts. After restarting device, all global variables are deleted. It is recommended to use different names for local and global variables. | + | For sharing variables between scripts, their initialization must be done without keyword '''local'''. Conventions in Lua specify that global variables should start with '''_G.''' (e.q. '''_G.globalVariable = true'''). After initialization global variable is available in all scripts. After restarting device, all global variables are deleted. It is recommended to use different names for local and global variables. |
Revision as of 10:13, 17 October 2019
Script starting
LUA Scripts in NETIO are event driven. That means if some event is triggered the script run exactly once. One of "triggers" is for example System started up. You can find list of all events called "triggers" in NETIO Lua Reference.
Periodical scipts
In NETIO there is no way for triggering scripts periodically (e.g. every 10 seconds) usng standard triggers. For such scripts the trigger System started up and the function delay() must be used. This function calls itself and allows to run some procedures periodically.
- Example switching output 1 every 10 seconds:
DoPeriod = 10 -- repeat period in seconds ------------------------------------------------- function doPeriodically() -- your periodical code -- your periodical code -- your periodical code -- your periodical code -- your periodical code -- your periodical code delay(DoPeriod,function() doPeriodically() end) end doPeriodically()
- This script can be launch using e.g. System started up trigger.
- Only way how to disable these scripts is restart of NETIO, or usage of global variables.
Debugging
To debug your code, use log() and logf() functions. See the output in system events log.
Global variables
For sharing variables between scripts, their initialization must be done without keyword local. Conventions in Lua specify that global variables should start with _G. (e.q. _G.globalVariable = true). After initialization global variable is available in all scripts. After restarting device, all global variables are deleted. It is recommended to use different names for local and global variables.