Unloading a lua module that was required

Hi,

Is there a way to unload a .lua file that you loaded with require?

ie: json = require(“json”)

I have a big problem in that I have two files that contain similar functions that get loaded when a user chooses Option A or Option B.

If they choose B and then go back and choose A, the wrong function which has the same name gets called, because the first file is still in memory.

Any help greatly appreciated!

Greg

* I tried the following but the wrong lua module is staying in memory.

if package.loaded[startup] ~= nil then startup = nil package.loaded[startup] = nil end

potentially simpler/cleaner approach:

-- prep, load both modules: local modules = { A = require("ModuleA"), B = require("ModuleB") } -- later, on user choice of "A" or "B" then: modules[choice]:doStuff()

Hi Dave,

I appreciate the answer but it doesn’t get me out of my bind.

both modules contain a function with the same name:

a.lua --> function letsgo()

b.lua --> function letsgo()

my problem is that when I load b.lua, the function letsgo() in a.lua is being called…

Note to self:  don’t forget the quotes in package.loaded[“file”]… closed!

Try to switch order of the 2 lines in the if-statement. 

Eg.

if package.loaded[startup] ~= nil then package.loaded[startup] = nil startup = nil end

that implies that each module is defining “letsgo” globally.  the approach i suggested assumed the “standard” module metaphor - where a module’s functions are “owned” by a table that the module returns, fe:

-- ModuleA.lua local M = {} function M.letsgo() print("let's go to A") end return M -- ModuleB.lua local M = {} function M.letsgo() print("let's go to B") end return M -- main.lua -- it is now valid to have both modules in memory simultaneously -- and call either as desired, as per prior post, or long-hand: local moduleA = require("ModuleA") local moduleB = require("ModuleB") if (choice=="A") then moduleA.letsgo() else -- ie, choice=="B" moduleB.letsgo() end

potentially simpler/cleaner approach:

-- prep, load both modules: local modules = { A = require("ModuleA"), B = require("ModuleB") } -- later, on user choice of "A" or "B" then: modules[choice]:doStuff()

Hi Dave,

I appreciate the answer but it doesn’t get me out of my bind.

both modules contain a function with the same name:

a.lua --> function letsgo()

b.lua --> function letsgo()

my problem is that when I load b.lua, the function letsgo() in a.lua is being called…

Note to self:  don’t forget the quotes in package.loaded[“file”]… closed!

Try to switch order of the 2 lines in the if-statement. 

Eg.

if package.loaded[startup] ~= nil then package.loaded[startup] = nil startup = nil end

that implies that each module is defining “letsgo” globally.  the approach i suggested assumed the “standard” module metaphor - where a module’s functions are “owned” by a table that the module returns, fe:

-- ModuleA.lua local M = {} function M.letsgo() print("let's go to A") end return M -- ModuleB.lua local M = {} function M.letsgo() print("let's go to B") end return M -- main.lua -- it is now valid to have both modules in memory simultaneously -- and call either as desired, as per prior post, or long-hand: local moduleA = require("ModuleA") local moduleB = require("ModuleB") if (choice=="A") then moduleA.letsgo() else -- ie, choice=="B" moduleB.letsgo() end