Loading a file and returning its environment
snippet in lua

Loading a file and returning its environment

user125

function include(scriptfile)
    local env = setmetatable({}, {__index=_G})
    assert(loadfile(scriptfile, 't', env))()
    return setmetatable(env, nil)
end

file1 = include'File1.lua'
A = 1

print(A)       -- 1
print(file1.A) -- 5