Jump to content

[TOPIC: topicViewTemplate]
[GLOBAL: userSmallPhoto]
Photo

Unloading a lua module that was required
Started by greg brady Dec 27 2018 05:21 PM

- - - - -
6 replies to this topic

Best Answer greg brady , 27 December 2018 - 06:48 PM

Note to self:  don't forget the quotes in package.loaded["file"]... closed!

[TOPIC CONTROLS]
[/TOPIC CONTROLS]
[modOptionsDropdown]
[/modOptionsDropdown]
[reputationFilter]
[TOPIC: post.html]
#1

greg brady

[GLOBAL: userInfoPane.html]
greg brady
  • Contributor

  • 283 posts
  • Corona SDK

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



[TOPIC: post.html]
#2

davebollinger

[GLOBAL: userInfoPane.html]
davebollinger
  • Corona Geek

  • 1,318 posts
  • Enterprise

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()


[TOPIC: post.html]
#3

greg brady

[GLOBAL: userInfoPane.html]
greg brady
  • Contributor

  • 283 posts
  • Corona SDK

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...



[TOPIC: post.html]
#4

greg brady

[GLOBAL: userInfoPane.html]
greg brady
  • Contributor

  • 283 posts
  • Corona SDK

  Best Answer

Note to self:  don't forget the quotes in package.loaded["file"]... closed!



[TOPIC: post.html]
#5

Yoger Games

[GLOBAL: userInfoPane.html]
Yoger Games
  • Contributor

  • 108 posts
  • Corona SDK

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


[TOPIC: post.html]
#6

greg brady

[GLOBAL: userInfoPane.html]
greg brady
  • Contributor

  • 283 posts
  • Corona SDK

 

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

Thanks for pointing that out.



[TOPIC: post.html]
#7

davebollinger

[GLOBAL: userInfoPane.html]
davebollinger
  • Corona Geek

  • 1,318 posts
  • Enterprise

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...

 

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





[topic_controls]
[/topic_controls]