Page 1 of 1

Need help going backwards in Folder Structure

Posted: Sun Jun 25, 2017 5:54 pm
by mastermarkus
So i'm working on an event driven GUI system, but i got into problem with Folder Structure's

My Folder Structure is like This:
root -> Libraries -> GUI-library Folder -> Modules -> lua files

root = game root folder
Libraries = where i hold my libraries


One of my lua files is in Modules folder how can i require another lua files in same folder "Modules" without doing something like this:

Code: Select all

Text = require("Libraries/SimplifyRoot/Modules/Text")
SimplifyRoot is my guilibrary root folder

What i want to do is something like this, so when other people use my library in their game it would not error:

Code: Select all

require("Modules/Text")
Also the SimplifyRoot folder holds only one main lua file that calls lua files in Modules folder.

Re: Need help going backwards in Folder Structure

Posted: Sun Jun 25, 2017 11:46 pm
by raidho36
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, '^[^%.]+' )

Re: Need help going backwards in Folder Structure

Posted: Mon Jun 26, 2017 11:53 am
by mastermarkus
Okey, let's say i have folder structure like this
root -> luafolder > test.lua

As i understand it's impossible to get ParentFolder "luafolder" of test.lua file without specifying the root -> lua folder in the first place?
So, if i was making Gui library it would be easier to put all code in one massive lua file, so it's easier for user to use it and not to have mess around configuring folder paths?

Re: Need help going backwards in Folder Structure

Posted: Mon Jun 26, 2017 12:27 pm
by zorg
It's not "easier" per-se, unless if you find dumping all your code into one file easier to read/edit/understand.

But understand this, a löve project will always have its folder structure start from where its main.lua is (or in other words, that will be the root directory of it), and using the code raidho wrote above means you can get the part of the path you, as a library author, shouldn't care about, and which can be anything. After that, you can append subfolders to that path, and then you can separate code or assets into those as you see fit, any project using your lib would work, regardless of their own folder structure.

Re: Need help going backwards in Folder Structure

Posted: Mon Jun 26, 2017 12:37 pm
by raidho36
In other words, just pass entire module name into require every time. If you have a module deep down the hierarchy and it needs to require a sibling module, pass entire sibling module name into require. It will not work if you change the module folder structure, but it's supposed to.

Re: Need help going backwards in Folder Structure

Posted: Mon Jun 26, 2017 12:45 pm
by mastermarkus
Ok thanks for the help guy's.