Using removed modules again?

I wonder how I can use removed modules again. When doing something like this:

-- Module local mod={} local gfx={} local another=require("anotherModule") local createGraphics= function(group) -- create Graphics here gfx.ball=display.newCircle... end local deleteStuff = function() package.loaded[("anotherModule] = nil another = nil end mod.createGraphics = createGraphics mod.deleteStuff = deleteStuff return mod

When I call the deleteStuff function from the mainfile where I have required the module above the local another variable is set to nil after the “anotherModule” was “unrequired” and set to nil.

When using the above module again I get an error like this:

attempt to index upvalue “another”

How can I make sure the local another is created again?

Just call a function that requires the module again. Also check if the module exists before removing it.

-- Module local mod={} local gfx={} local another=require("anotherModule") local createGraphics= function(group) -- create Graphics here gfx.ball=display.newCircle... end local loadStuff = function() if not another then another = require("anotherModule") end end local deleteStuff = function() if another then package.loaded[("anotherModule] = nil another = nil end end mod.createGraphics = createGraphics mod.deleteStuff = deleteStuff return mod

Thx again for your help!

Does this mean the second time I “create” another makes it a global variable?

No, “another” is defined as local, so it stays local.

Just call a function that requires the module again. Also check if the module exists before removing it.

-- Module local mod={} local gfx={} local another=require("anotherModule") local createGraphics= function(group) -- create Graphics here gfx.ball=display.newCircle... end local loadStuff = function() if not another then another = require("anotherModule") end end local deleteStuff = function() if another then package.loaded[("anotherModule] = nil another = nil end end mod.createGraphics = createGraphics mod.deleteStuff = deleteStuff return mod

Thx again for your help!

Does this mean the second time I “create” another makes it a global variable?

No, “another” is defined as local, so it stays local.