lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


Daniel Collins wrote:

Jerome's post shows a neat way to set the environment before loading the
file. That way you don't need to modify the files at all. So you can
have them run in a shared environment if needed, or they can have their
own environment. I would probably put Jerome's solution in a function
(untested):


That is also a neat way to do lua config files.
<lua>
local config = {
		-- fill in defaults
		}

local cfgfile="foo.cfg"
local f, err = loadfile(cfgfile)
if f then
    setfenv(f,config)
    f()
else
   print("config file ("..cfgfile..") load failed :"..tostring(err))
   print("... using default config")
end
</lua>

The config file then just has Lua statements :

name="fred"
number=1024
namelen=#name
-- etc

When the config file is executed, all of its 'globals' are stored in the environment, eg the config table. As lhf pointed out, you need to set a meta-index to _G for the config file to be able to call any lua functions.

Adrian