First of all, the require function doesn't takes file path, it takes module name. It will look for modules in few directories, starting with game directory, but not limited to it. Additionally, require function can load binary compiled modules, not just Lua files. Whatever you pass into require is a
full module name, with slashes and everything else. For convenience's sake it allows you to put sub-module files in a directory and address that sub-module with slash or dot notation which I suggest you use just to remember that it's not a file path. Note that the directory is main module of which your file is sub-module -
not a directory that contains modules. To give a concrete example, the imgui binary module cannot be in "lib" directory because the module is not named "
lib.imgui" and if you attempt this it will fail to load. It's a common mistake to use require that way.
To answer your quesiton though. First argument passed into require is module name. From there on you can work your way to higher level modules.
Code: Select all
local modulename = ...
local parentmodule = string.match ( modulename, '[^%.]+$' )
local rootmodule = string.match ( modulename, '^[^%.]+' )