Page 1 of 1

How do i change the source directory?

Posted: Thu Mar 30, 2023 11:49 am
by jakles
I want all the source files to be under the /src/ folder, how would i change that? :cry:

Re: How do i change the source directory?

Posted: Thu Mar 30, 2023 12:42 pm
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.

Re: How do i change the source directory?

Posted: Thu Mar 30, 2023 2:24 pm
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.

Re: How do i change the source directory?

Posted: Thu Mar 30, 2023 2:46 pm
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

Re: How do i change the source directory?

Posted: Thu Mar 30, 2023 3:35 pm
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.

Re: How do i change the source directory?

Posted: Fri Mar 31, 2023 11:10 am
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?

Re: How do i change the source directory?

Posted: Fri Mar 31, 2023 11:29 am
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.