[SOLVED]Require all files in a folder

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.
User avatar
wesleylucas
Prole
Posts: 12
Joined: Thu May 09, 2013 11:08 pm
Location: Brazil

[SOLVED]Require all files in a folder

Post by wesleylucas »

Hey guys, I'm making a scene-based game and each scene will be a file in the 'scripts' folder. :monocle:
I've already do it in ruby, but I have no idea on how to do that in Lua.
Anyone know how to?

Thanks for the attention. ^^
Last edited by wesleylucas on Fri May 10, 2013 12:12 pm, edited 1 time in total.
def or function doesn't matter, you understand what I mean anyway.
davisdude
Party member
Posts: 1154
Joined: Sun Apr 28, 2013 3:29 am
Location: North Carolina

Re: Require all files in a folder

Post by davisdude »

So it would be easier if you at least put an example of your code as a reference, but I think I know what you want.
In your main.lua put require "NAME_OF_FOLDER/NAME_OF_FILE"
Don't actually put NAME_OF_FOLDER and whatever. I think you know what I mean. :awesome:
GitHub | MLib - Math and shape intersections library | Walt - Animation library | Brady - Camera library with parallax scrolling | Vim-love-docs - Help files and syntax coloring for Vim
ferdielance
Prole
Posts: 6
Joined: Tue May 07, 2013 10:35 am

Re: Require all files in a folder

Post by ferdielance »

You could use love.filesystem.enumerate to get the names of all files in the folder as strings, then use a for loop to iterate over the keys and values in the list that returns (see the example in the docs), and then use each string as an argument to require each file, I think.
User avatar
kikito
Inner party member
Posts: 3153
Joined: Sat Oct 03, 2009 5:22 pm
Location: Madrid, Spain
Contact:

Re: Require all files in a folder

Post by kikito »

I did this some time ago:

https://github.com/kikito/fay/blob/mast ... equire.lua

It extends require with some methods. The usage for requiring the files in a folder is:

Code: Select all

require 'require' -- or require 'lib.require', depending on where you put it
local d = require.tree('path.to.directory')
d.file1 -- contents returned by path/to/directory/file1.lua
d.file2 -- contents returned by path/to/directory/file2.lua
d.subdir.file3 -- contents returned by path/to/directory/subdir/file3.lua
As a bonus, you also have require.relative:

Code: Select all

local f = require.relative(..., "hello") -- if the path to the current file is 'foo.bar.baz', this requires 'foo.bar.hello'
Note that require('file') without methods works exactly like before.
When I write def I mean function.
ferdielance
Prole
Posts: 6
Joined: Tue May 07, 2013 10:35 am

Re: Require all files in a folder

Post by ferdielance »

That's an elegant addition. I assume other code projects can use it with credit?
User avatar
kikito
Inner party member
Posts: 3153
Joined: Sat Oct 03, 2009 5:22 pm
Location: Madrid, Spain
Contact:

Re: Require all files in a folder

Post by kikito »

Sure, consider this an explicit permission to use it.

All my source code is MIT-licensed. That little snippet was not big enough to make it a full lib.
When I write def I mean function.
User avatar
wesleylucas
Prole
Posts: 12
Joined: Thu May 09, 2013 11:08 pm
Location: Brazil

Re: Require all files in a folder

Post by wesleylucas »

kikito wrote:I did this some time ago:

https://github.com/kikito/fay/blob/mast ... equire.lua

It extends require with some methods. The usage for requiring the files in a folder is:

Code: Select all

require 'require' -- or require 'lib.require', depending on where you put it
local d = require.tree('path.to.directory')
d.file1 -- contents returned by path/to/directory/file1.lua
d.file2 -- contents returned by path/to/directory/file2.lua
d.subdir.file3 -- contents returned by path/to/directory/subdir/file3.lua
As a bonus, you also have require.relative:

Code: Select all

local f = require.relative(..., "hello") -- if the path to the current file is 'foo.bar.baz', this requires 'foo.bar.hello'
Note that require('file') without methods works exactly like before.
Thank you! That's exactly what I mean, better actually!
Credits for you in my game!
def or function doesn't matter, you understand what I mean anyway.
User avatar
kikito
Inner party member
Posts: 3153
Joined: Sat Oct 03, 2009 5:22 pm
Location: Madrid, Spain
Contact:

Re: [SOLVED]Require all files in a folder

Post by kikito »

I'm glad it helped you, good luck with your game! :)
When I write def I mean function.
kargon
Prole
Posts: 13
Joined: Tue Sep 09, 2014 2:45 am

Re: [SOLVED]Require all files in a folder

Post by kargon »

Hi.

Been searching around for this functionality. I downloaded kikito's require.lua and put it in my game base directory. However, I'm having problems trying to get it to work properly.

I currently have a /characters/ (off my base game directory) that has a bunch of files I want to include. What is the proper syntax to include files from another directory? What is the proper syntax to include just 1 file from another directory?

Thanks so much!

Note: I have read the examples above and I'm still having trouble (I'm new to all this!).
kargon
Prole
Posts: 13
Joined: Tue Sep 09, 2014 2:45 am

Re: [SOLVED]Require all files in a folder

Post by kargon »

OK!

Forgot to remove .lua, so that part works.

Now I just need to figure out how to get all files from a directory to be auto required.

Thanks in advance!
Post Reply

Who is online

Users browsing this forum: No registered users and 1 guest