July 2018
Beginner
202 pages
5h 42m
English
Lua provides an easy and powerful method to set the global environment per function. This means that any variables without the local modifier will be recorded in the environment of the function, rather than in the global table. This can be achieved by setting the _ENV variable inside a function.
The _ENV variable, by default, points to _G. This means any new variable will be registered in _G. But if you declare _ENV to be a new table, for example, variables will be registered to it instead of _G:
function SayHelloCustomEnv() local _ENV = {print=print} foo = "hello" local bar = "world" print(foo .. " " .. bar)endfunction SayHelloDefaultEnv() foo = "hello" local bar = "world" print(foo .. " " .. bar)end-- foo and bar are not in ...Read now
Unlock full access