luaenvironment-variablesenvironmentzerobrane

_ENV is not working in zerobrane studio on windows


When print(_ENV) is used in zerobrane studio on windows, it results into nil. Can we set _ENV variable to its expected use? As an example of code,

a = 15                      -- create a global variable
      _ENV = {g = _G}             -- change current environment
      a = 1                       -- create a field in _ENV
      g.print(_ENV.a, g.a)

This code throws the error in zerobrane studio on windows.


Solution

  • _ENV was introduced in Lua 5.2.

    See https://www.lua.org/manual/5.2/manual.html#8.1

    ZeroBrane runs the Lua 5.1 interpreter by default. You can pick another one via

    main menu -> Project -> Lua Interpreter

    In Lua 5.1 you could do something like this:

    a = 15
    setfenv(1, {g = _G})
    _ENV = g.getfenv()
    a = 1
    g.print(_ENV.a, g.a)
    

    I haven't spent too much thought on it. So probably it is not 100% equivalent.