Need help going backwards in Folder Structure

Questions about the LÖVE API, installing LÖVE and other support related questions go here.
Forum rules
Before you make a thread asking for help, read this.
Post Reply
mastermarkus
Prole
Posts: 10
Joined: Sun Jun 25, 2017 5:29 pm

Need help going backwards in Folder Structure

Post 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.
User avatar
raidho36
Party member
Posts: 2063
Joined: Mon Jun 17, 2013 12:00 pm

Re: Need help going backwards in Folder Structure

Post 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, '^[^%.]+' )
mastermarkus
Prole
Posts: 10
Joined: Sun Jun 25, 2017 5:29 pm

Re: Need help going backwards in Folder Structure

Post 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?
User avatar
zorg
Party member
Posts: 3469
Joined: Thu Dec 13, 2012 2:55 pm
Location: Absurdistan, Hungary
Contact:

Re: Need help going backwards in Folder Structure

Post 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.
Me and my stuff :3True Neutral Aspirant. Why, yes, i do indeed enjoy sarcastically correcting others when they make the most blatant of spelling mistakes. No bullying or trolling the innocent tho.
User avatar
raidho36
Party member
Posts: 2063
Joined: Mon Jun 17, 2013 12:00 pm

Re: Need help going backwards in Folder Structure

Post 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.
mastermarkus
Prole
Posts: 10
Joined: Sun Jun 25, 2017 5:29 pm

Re: Need help going backwards in Folder Structure

Post by mastermarkus »

Ok thanks for the help guy's.
Post Reply

Who is online

Users browsing this forum: Ahrefs [Bot], Google [Bot] and 10 guests