External Module, require and duplicates.

Post Reply
DefinNormal
Posts: 1
Joined: Wed Feb 17, 2016 11:38 pm

External Module, require and duplicates.

Post by DefinNormal » Wed Feb 17, 2016 11:50 pm

I am using require in order to load a series of external lua modules. The script I have is below, which does load properly. Honestly, everything is working perfectly except for one thing. I read that using package.loaded[module] = nil would allow me to require that file again, which it does. Unfortunately, it also duplicates the contents of the file, or creates triplicates, etc.. depending on how many times I try and reload the file. I would like to be able to reload it in this manner in order to update changes to the script without having to close/open Mudlet. Is there a way to make Mudlet sort of.. forget.. that the module was previously loaded? package.loaded.test does show the entire contents of the module, and package.loaded[module] = nil does change it to nil, but the module is still loaded into memory. I hope I am explaining this okay. I have been working on it for nearly nine hours now, and I simply can't find a solution. I am also very new to Mudlet and to Lua (as in, a couple of days into it), so any help is greatly appreciated.
Code: [show] | [select all] lua
-- Changes package.path to include the directory where my modules are located.
if not System then
 local path     =  package.path
 local home_dir =  getMudletHomeDir()
 local lua_dir  =  string.format( "%s\%s", home_dir, [[?.lua]] )
 local init_dir =  string.format( "%s\%s", home_dir, [[\System\?.lua]] )
 package.path   =  string.format( "%s;%s;%s", path, lua_dir, init_dir )
 System = true
end

-- Function to load (or reload) modules.
function load_module (module)
  if package.loaded[module] then
    package.loaded[module] = nil
    require(module)
  elseif
    require(module)
  end
end

-- Function in use.
load_module("test")
Inside of test.lua
Code: [show] | [select all] lua
return test

Post Reply

Who is online

Users browsing this forum: No registered users and 7 guests