How do i change the source directory?

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
jakles
Prole
Posts: 2
Joined: Thu Mar 30, 2023 11:48 am

How do i change the source directory?

Post by jakles »

I want all the source files to be under the /src/ folder, how would i change that? :cry:
User avatar
marclurr
Citizen
Posts: 98
Joined: Fri Apr 22, 2022 9:25 am

Re: How do i change the source directory?

Post by marclurr »

You can pass the root directory as an argument to the "love" command

Code: Select all

> love src
The only issue is you'll have to put all your assets and any data local to the game in there too. If you want to have all of that separated in your project you'll have to write some script to move all code and assets together for running.
User avatar
zorg
Party member
Posts: 3435
Joined: Thu Dec 13, 2012 2:55 pm
Location: Absurdistan, Hungary
Contact:

Re: How do i change the source directory?

Post by zorg »

You don't change anything, you just put your code in the /src/ folder (except main.lua, that needs to be in the project root folder) and require your lua files with src. prepended to them.
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.
jakles
Prole
Posts: 2
Joined: Thu Mar 30, 2023 11:48 am

Re: How do i change the source directory?

Post by jakles »

Thank you very much guys! Passing the directory argument did it.
I don't know how I didn't think of that :D
User avatar
pgimeno
Party member
Posts: 3541
Joined: Sun Oct 18, 2015 2:58 pm

Re: How do i change the source directory?

Post by pgimeno »

Just for the giggles, I tried (and succeeded) to have main.lua in src/ too. But it's a dirty trick, not recommended for production; just a funny experiment.

I first added this at the top of conf.lua (which DOES need to be in the top folder):

Code: Select all

package.path = "src/?.lua;" .. package.path
But there's still a problem: boot.lua won't try to require main.lua if it doesn't find it in the root folder. I worked around that by creating a directory called... main.lua, in the top folder. Since "something" called main.lua exists, boot.lua is happy and proceeds to require it. Then the search path specifies to search in src/ first, and that's where the actual main.lua is, so it is run.
User avatar
zorg
Party member
Posts: 3435
Joined: Thu Dec 13, 2012 2:55 pm
Location: Absurdistan, Hungary
Contact:

Re: How do i change the source directory?

Post by zorg »

pgimeno wrote: Thu Mar 30, 2023 3:35 pm I first added this at the top of conf.lua (which DOES need to be in the top folder):

Code: Select all

package.path = "src/?.lua;" .. package.path
This is also good for having all code files in src be required without the need to prepend src. to the filenames.
...then again, you probably would want to call love.filesystem.setRequirePath instead of just directly setting package.path maybe.
pgimeno wrote: Thu Mar 30, 2023 3:35 pm But there's still a problem: boot.lua won't try to require main.lua if it doesn't find it in the root folder. I worked around that by creating a directory called... main.lua, in the top folder. Since "something" called main.lua exists, boot.lua is happy and proceeds to require it. Then the search path specifies to search in src/ first, and that's where the actual main.lua is, so it is run.
This sounds like something that would need a bit of fine-tuning in the source, so that boot.lua checks whether the main.lua is actually a file (or symlink) or not... since if there is a folder called main.lua, but you didn't set up further shenanigans, then it'll probably error out currently with an unhelpful error message, no?
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
pgimeno
Party member
Posts: 3541
Joined: Sun Oct 18, 2015 2:58 pm

Re: How do i change the source directory?

Post by pgimeno »

I'm more of the opinion that adding too many checks to protect users against themselves bloats the program for use cases that are too rare to be of any significance - like having a directory called main.lua as in this hack.

That said, I'd prefer if require('main') is wrapped in a pcall rather than checking in advance if it exists, because as evidenced above, it might not error after all. It's not just that it's easier to ask for forgiveness than permission (EAFP); it actually enables a feature in this case.
Post Reply

Who is online

Users browsing this forum: slime and 12 guests