What is the way to reload lua scripts at runtime?

I want to reload lua scripts at runtime. What are the ways to do this? Do you just need to reinitialize the lua system and then re-read all the lua files?

+5
source share
2 answers

If (a) the Lua scripts are in modules , and (b) the modules do not affect global tables or tables that go beyond the module, you can use package.loaded.????? = nilto call requireto reboot the module:

> require "lsqlite3"
> =sqlite3.version
function: 0x10010df50
> sqlite3.version = "33"
> return sqlite3.version
33
> require "lsqlite3"
> return sqlite3.version
33
> package.loaded.lsqlite3 = nil
> return sqlite3.version
33
> require "lsqlite3"
> return sqlite3.version
function: 0x10010c2a0
> 

, , () () 't , script .

+13

include(filename):

function evalfile(filename, env)
    local f = assert(loadfile(filename))
    return f()
end

function eval(text)
    local f = assert(load(text))
    return f()
end

function errorhandler(err)
    return debug.traceback(err)
end

function include(filename)
    local success, result = xpcall(evalfile, errorhandler, filename)

    --print(string.format("success=%s filename=%s\n", success, filename))
    if not success then
        print("[ERROR]\n",result,"[/ERROR]\n")
    end
end

function include_noerror(filename)
    local success, result = xpcall(evalfile, errorhandler, filename)
    --print(string.format("success=%s filename=%s\n", success, filename))
end
0

All Articles