Using 'require' to include Lua modules

BECOME PART OF THE COMMUNITY - Sign up here
  • Hi,

    So what's the proper way to use Lua modules?

    I want to use the 'require' keyword with relative path, but I got 2 issues with it.


    1) it seems to look for file only under :
    /Users/[user]/MALightingTechnology/gma3_1.8.1/shared/resource/lib_plugins/requirements

    I tried to workaround this by adding a symbolic link under 'requirements' to a folder of my choice.

    2) ReloadPlugins does not reload file from that path, even if I add the required file as an installed LuaComponent. Because the file (module) was already "required" so it is not loaded again.


    * Is there a way to edit the search path?
    * Is there another way without using "require"?

  • I found a way !

    after finding this post:
    https://www.reddit.com/.../how_to_force_reload_all.../

    A simple example would be:

    say I have a plugin myPlugin.lua, that want to use a module comp2.lua

    - I add myPlugin.lua as component 1 of the plugin.
    - I add comp2.lua as component 2 of the plugin
    - I set both to be "Installed"

    In myPLugin.lua I do:
    ---------------------
    package.loaded["comp2"] = nil
    local comp2 = require "comp2"
    ---------------------

    That's it. now "RealodPlugins" will work, and reload comp2 if it changes.
    This is a "brutal-force" way, in which we loose loaded module caching. but its good enough.

    The better way would be to have a separate "MyReloadPlugin.lua" that will do the "package.loaded["comp2"] = nil", and is used only in Development time.

  • Here is the full code I am using to allow requiring modules that reside under:


    MALightingTechnology/gma3_library/datapools/plugins


    - Does anyone see any potential issues with using LUA's require module, from this folder?
    - Why doesn't MA3 already include this folder in the package.path ? Is this a bug, or there is a good reason?

  • I agree that the user plugin path very well could be included in package.path as default.


    check out the API function GetPath to simplify your script.
    - these few lines seems to do the job for me:

    Code
    local userplugins = GetPath('plugins')
    if not package.path:find(userplugins) then
      package.path = package.path .. ';' .. userplugins .. '/?.lua'  
    end