Hello Guest it is May 13, 2021, 02:54:54 AM

Author Topic: Lexical scope and Global Variables in Lua  (Read 1137 times)

0 Members and 1 Guest are viewing this topic.

Lexical scope and Global Variables in Lua
« on: October 01, 2017, 07:03:20 PM »
Hi All,
have proven to myself, yet again, how little I know about Lua.

Writing some code yesterday to help a bloke out and elected to put two signals in the SigLib (signal load script) and have two
global variables AtSpeed and IsStopped be written with the state of the input pin.

My intention was to use the variables in m3, m4 and m5 scripts but came unstuck, the variable was not valid in
the macros.

My understanding was that any code in the screen load script was available to all other code within Mach, clearly
my understanding is wrong/incomplete/completely out to lunch!

I've read several explanations of lexical scoping but just when I thought I had it fixed in my mind its goes astray on me.
Does anyone have an explanation or a link to an explanation that may help? Making intelligent use of scoping is a vital
part to extracting the most from Lua.

Craig
My wife left with my best friend...
     and I miss him!

Offline DazTheGas

*
  •  753 753
  • DazTheGas
    • View Profile
Re: Lexical scope and Global Variables in Lua
« Reply #1 on: October 02, 2017, 10:54:35 AM »
Mach4 uses 2 states/chunks  of lua and cannot share variables between them.1 is for the GUI and the other for the interpretor. An idea would be to populate a register which can be read or written be both lua states.

DazTheGas
Theres always time for a choccy biccy
Re: Lexical scope and Global Variables in Lua
« Reply #2 on: October 02, 2017, 11:14:33 AM »
Hi DazTheGas,
thanks for the explanation, makes sense too....the GUI being a separate chunk than the interpreter. The register idea is a good way around it,
thanks.

Craig
My wife left with my best friend...
     and I miss him!
Re: Lexical scope and Global Variables in Lua
« Reply #3 on: October 19, 2017, 01:17:52 AM »
 out and elected to put two signals in the SigLib (signal load script) and have two
global variables AtSpeed and IsStopped be written with the state of the input pin.