This forum uses cookies
This forum makes use of cookies to store your login information if you are registered, and your last visit if you are not. Cookies are small text documents stored on your computer; the cookies set by this forum can only be used on this website and pose no security risk. Cookies on this forum also track the specific topics you have read and when you last read them. Please confirm that you accept these cookies being set.

Lua variables visibility and events management
#1
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')

if not mbtcp then
  mbtcp = luamodbus.tcp()
end

'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?
Reply
#2
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')
Reply
#3
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.
Reply
#4
Yes, each event is a separate process so a single script can have multiple instances.
Reply
#5
Thank you.
Reply


Forum Jump: