How to access modules up a directory [PLEASE HELP!]

General discussion about LÖVE, Lua, game development, puns, and unicorns.
Post Reply
InTheProcess
Prole
Posts: 11
Joined: Thu May 27, 2021 4:19 pm

How to access modules up a directory [PLEASE HELP!]

Post by InTheProcess »

I have been trying to look this up, but there is no tutorial other than LOVES Wiki. In the wiki, it stated that I can change the "require()" function's directory by using "love.filesystem.setRequiredPath(path)". I have no idea what the default value means. Here it is for reference. "?.lua;?/init.lua."
User avatar
zorg
Party member
Posts: 3465
Joined: Thu Dec 13, 2012 2:55 pm
Location: Absurdistan, Hungary
Contact:

Re: How to access modules up a directory [PLEASE HELP!]

Post by zorg »

you can use the ... vararg in the main body of a file you're requiring to get a path you can manipulate, and use that to go up a directory... IF it's not already the one where
- your main.lua is, or
- it is the root of your save folder

You don't need to mess with setRequirePath.
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.
InTheProcess
Prole
Posts: 11
Joined: Thu May 27, 2021 4:19 pm

Re: How to access modules up a directory [PLEASE HELP!]

Post by InTheProcess »

What do you mean by “vararg”. The closest thing I can think of is “Variable Arguments”. I need a cement ration please.
InTheProcess
Prole
Posts: 11
Joined: Thu May 27, 2021 4:19 pm

Re: How to access modules up a directory [PLEASE HELP!]

Post by InTheProcess »

Srry, lol. I need a demonstration
User avatar
zorg
Party member
Posts: 3465
Joined: Thu Dec 13, 2012 2:55 pm
Location: Absurdistan, Hungary
Contact:

Re: How to access modules up a directory [PLEASE HELP!]

Post by zorg »

was gonna ask what you meant by that, but thanks for the clarification :D

Yes, i do mean the variable argument that lua has, the ellipse (...)

Here's an example:

Code: Select all

-- bar/foo.lua

local path = ... -- this is in the file scope, and when requiring this file, it'll contain the require path it was found through

print(path)


-- main.lua

local meh = require "bar.foo"

After you have that path, some string manipulation to cut off the end to some extent is needed; in some libraries it is done that way, i just can't remember the exact solution to that; at that point, you can have just the directory of the required file, and from that, you can have the directory above it... but only if it wasn't already on the same level as main.lua since you can't go above that.
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.
InTheProcess
Prole
Posts: 11
Joined: Thu May 27, 2021 4:19 pm

Re: How to access modules up a directory [PLEASE HELP!]

Post by InTheProcess »

I am pretty confused. I am pretty new to lua and love2d. Here is my situation. I have "state.lua" control my gamestates. One of those is the menu. In the menu script, I have to access a library for the menu gui. Here is a picture of my file structure. Image I want a kind of a tutorial. I just don't understand why some things are the way they are. Maybe show it in action.
InTheProcess
Prole
Posts: 11
Joined: Thu May 27, 2021 4:19 pm

Re: How to access modules up a directory [PLEASE HELP!]

Post by InTheProcess »

I need help understanding it
User avatar
ReFreezed
Party member
Posts: 612
Joined: Sun Oct 25, 2015 11:32 pm
Location: Sweden
Contact:

Re: How to access modules up a directory [PLEASE HELP!]

Post by ReFreezed »

Is there a reason you don't want to use full paths, i.e. require("data.scripts.lib.gui.button")?
Tools: Hot Particles, LuaPreprocess, InputField, (more) Games: Momento Temporis
"If each mistake being made is a new one, then progress is being made."
InTheProcess
Prole
Posts: 11
Joined: Thu May 27, 2021 4:19 pm

Re: How to access modules up a directory [PLEASE HELP!]

Post by InTheProcess »

Oh, I didn't realize that was an option. Thanks, I got it from here.
User avatar
zorg
Party member
Posts: 3465
Joined: Thu Dec 13, 2012 2:55 pm
Location: Absurdistan, Hungary
Contact:

Re: How to access modules up a directory [PLEASE HELP!]

Post by zorg »

I did go on the assumption that you wanted to access a directory that was "above" where your main.lua was; since it's not, paths starting from that folder will work, as said by Refreezed.
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.
Post Reply

Who is online

Users browsing this forum: No registered users and 1 guest