how does luup manage local, global variables

Hi,
Does anyone know of a tutorial about how LUUP manages variables. I have a couple of questions about how to define my variables in a LUUP scene and how they interact with other scenes that run at the same time.
Is there a difference if I define a new variable with the word “local” before it ?

Would that same value be available to another scene that runs at the same time? If I want to have a global variable where do I define it? in luup startup? how do I access it?

All scenes operate in the same context, so a global variable set in one scene will be visible immediately from another. Plugins are different and get their own global variable space.

If you declare a variable “local”, then you’ve made a new variable (possibly hiding access to the global with the same name) until the end of the scope, effectively until the next “end” in your code. Local variables are never available outside their code blocks, which means that two scenes that declare local variables with the same name will never interfere with each other.

(Disclaimer: Seasoned Lua programmers will notice the deliberate mistakes in my explanation.)

Am I right in believing that the Startup Lua code also runs in the scene context?

[quote=“futzle, post:2, topic:179420”]All scenes operate in the same context, so a global variable set in one scene will be visible immediately from another. Plugins are different and get their own global variable space.

If you declare a variable “local”, then you’ve made a new variable (possibly hiding access to the global with the same name) until the end of the scope, effectively until the next “end” in your code. Local variables are never available outside their code blocks, which means that two scenes that declare local variables with the same name will never interfere with each other.

(Disclaimer: Seasoned Lua programmers will notice the deliberate mistakes in my explanation.)[/quote]

How is the variable treated if I omit the “local” when declaring it in a scene script?

How is the variable treated if I omit the "local" when declaring it in a scene script?

…as a global.

[quote=“akbooer, post:5, topic:179420”]

How is the variable treated if I omit the “local” when declaring it in a scene script?

…as a global.[/quote]

Thanks akbooer and futzle!

One last question if I may.

If I have a scene with this script:

local lightLevel = luup.variable_get(DIM_SID, “LoadLevelStatus”, DEVICE_NO_DIM) or 0
lightLevel = tonumber(lightLevel)

is it correct or would this be correct

local lightLevel = luup.variable_get(DIM_SID, “LoadLevelStatus”, DEVICE_NO_DIM) or 0
local lightLevel = tonumber(lightLevel)

would the first scenerio treat the second lightLevel declaration as a new global var?

would the first scenerio treat the second lightLevel declaration as a new global var?

No. Once you have declared a variable as local, all references within the same scope will be to that same variable.

Thanks RexBeckett!

You’ll find an online version of the excellent “Programming in Lua” book linked to here [url=http://www.lua.org/pil/#2ed]Programming in Lua