Lua variables visibility and events management - Printable Version +- Logic Machine Forum (https://forum.logicmachine.net) +-- Forum: LogicMachine eco-system (https://forum.logicmachine.net/forumdisplay.php?fid=1) +--- Forum: Scripting (https://forum.logicmachine.net/forumdisplay.php?fid=8) +--- Thread: Lua variables visibility and events management (/showthread.php?tid=2992) |
Lua variables visibility and events management - Andey - 18.11.2020 Hello. I want to clear following programming points. Please read this, correct or confirm my understanding of script variables usage. Default variable declaration in 'global'. Global variable has script file scope - it is visible for all blocks of script file and can be declared anywhere in script file (inside and outside function within script file). Global variable can't be used in other script files, and there is no possibility to declare it as 'extern' variable as it is possible in C language. Local variable can be declared with 'local' keyword. Local variable can be declared inside function block and it is visible only in block where it is declared. If global and local variables with the same name are declared in the script only block where local variable is declared refers to local variable. All other parts of script code refers to global variable. Event loads script every time when particular event occurs. And script is unloaded when script ends. This means that all script variables are initialized when event starts script file and all variables are uninitialized when script end it's job. So if I want to create a counter to count number of events (for example) I can't to use global variable in the script because this variable will be initialized as nil each time when event will occurs. But in this case I do not understand constructs like Code: require('luamodbus') 'mbtcp' variable is nil each time when event script starts, so it is not necessary to check does this variable is initialized. Does my reasoning is correct? RE: Lua variables visibility and events management - admin - 18.11.2020 You're correct about how variables work. But each script is a separate OS process. You cannot share variables between processes directly. For this you can use storage. The construct is mostly meant for resident scripts but it will work in a event script even though it's not necessary. If you want to implement a counter that can be reset then the increment must be an atomic operation. Use this to increment a storage value by 1: Code: storage.exec('incr', 'my_key_name') RE: Lua variables visibility and events management - Andey - 18.11.2020 Thank you for an answer, Admin. It is true, that each event is a new OS process? I mean that same event source creates new process (threat) to execute event script. If is true it means that there is possibility to get running several scripts simultaneously from same event. For example, script calls os.sleep(1000) method. If event will occurs two times per second (somebody turn switcher on and off quickly) then second instance of the script will starts while first script still was no finished. Am I correct? PS. Actually I am more familiar with C/C++, so some script concepts are not clear for me. RE: Lua variables visibility and events management - admin - 19.11.2020 Yes, each event is a separate process so a single script can have multiple instances. RE: Lua variables visibility and events management - Andey - 19.11.2020 Thank you. |