require internal libraries

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
deb75
Prole
Posts: 38
Joined: Sun Apr 18, 2021 1:11 pm

require internal libraries

Post by deb75 »

Hello,

In my project, I have some separate libraries that are put into "project/lib" directory, e.g. :
project/lib/map
project/lib/map/init.lua
project/lib/map/functions.lua
project/lib/map/config.lua
...

In the "main.lua", I call it with :

Code: Select all

local map = require "lib.map"
However, I get an error because the "init.lua" has

Code: Select all

local config = require "map.config"
but love does not find this subpackage of map library.

It works again if I modify the previous line :

Code: Select all

local config = require "lib.map.config"
However, I do not want to put in the separate library a path ("lib") which is out of the library.

I can also include "lib/map" into the package.path in the "main.lua" but I am sure there is a
better (more portable, more sustainable) way to organize separate library.

How would you do ?

Regards
grump
Party member
Posts: 947
Joined: Sat Jul 22, 2017 7:43 pm

Re: require internal libraries

Post by grump »

A well written library should account for that and allow to be required from any path. I personally would steer clear from libs that don't do this because it's a red flag, and you're probably dealing with bad code that will have more problems down the line.

You can modify package.path (pure Lua) or call love.filesystem.setRequirePath (LÖVE) to point the loader to the right directories.

Code: Select all

love.filesystem.setRequirePath(love.filesystem.getRequirePath() .. ';lib/?.lua;lib/?/init.lua')
User avatar
zorg
Party member
Posts: 3436
Joined: Thu Dec 13, 2012 2:55 pm
Location: Absurdistan, Hungary
Contact:

Re: require internal libraries

Post by zorg »

The require function does pass the "current path" to the file it's requiring, which you can get with the vararg (...) construct in the file scope; you can use that to implement your libraries to handle (mostly) arbitrary paths.
Here's some references: http://kiki.to/blog/2014/04/12/rule-5-b ... ple-files/
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.
applebappu
Prole
Posts: 37
Joined: Thu Jun 24, 2021 5:49 pm

Re: require internal libraries

Post by applebappu »

I tend to just have one big folder for any modules I'm going to include and throw them all in there. At most, I might have a "lib" folder and a "classes" folder, with no subfolders in them. That eliminates the need to figure out module pathing, and lets me just put:

Code: Select all

local Foo = {
    ....stuff....
}
return Foo
in the module and

Code: Select all

Foo = require "./modules/Foo"
in main.lua, and call it a day.

That said, this solution only really works when you code all of those modules from scratch yourself (which I tend to do) and don't use other people's libraries, which could have any kind of structure at all. It also wouldn't scale well for like a AAA project, but I'm just one lady and I make small games so it works.

More germane to your question, though, I see you've got a kind of circular inclusion going on there, where you've got one module requiring another one that loads after it? Am I understanding that right? That would definitely cause issues, since code executes linearly (including your "require"s).
deb75
Prole
Posts: 38
Joined: Sun Apr 18, 2021 1:11 pm

Re: require internal libraries

Post by deb75 »

Hi,

Thanks for all yours answeers.

I read the link provided by zorg and indeed, it answeers quite well to my question, I think I will follow their final advocations

@applebappu : where do you see a circular inclusion ? this is just the map/init.lua requiring submodules

Regards
applebappu
Prole
Posts: 37
Joined: Thu Jun 24, 2021 5:49 pm

Re: require internal libraries

Post by applebappu »

Ah, I mis-read then. Never mind!
Post Reply

Who is online

Users browsing this forum: Bing [Bot], Google [Bot] and 33 guests