Require all files in a 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
Psyk
Prole
Posts: 8
Joined: Thu Sep 11, 2014 10:40 pm

Require all files in a directory

Post by Psyk »

How would I require all files in a directory?

Entities - Folder
entity.lua
potion.lua

Would I be right if I did this?

Code: Select all

function includeAllEnts() 
     local directory = "entities"
     local entities = love.filesystem.enumerate(directory)
     
     for i, e in ipairs(entities) do 
      require(e)
     end
end
User avatar
Robin
The Omniscient
Posts: 6506
Joined: Fri Feb 20, 2009 4:29 pm
Location: The Netherlands
Contact:

Re: Require all files in a directory

Post by Robin »

Untested, but I'd say:

Code: Select all

function includeAllEnts() 
     local directory = "entities"
     local entities = love.filesystem.enumerate(directory)
     
     for i, e in ipairs(entities) do 
      require(directory .. "." .. e)
     end
end
Help us help you: attach a .love.
Psyk
Prole
Posts: 8
Joined: Thu Sep 11, 2014 10:40 pm

Re: Require all files in a directory

Post by Psyk »

Robin wrote:Untested, but I'd say:

Code: Select all

function includeAllEnts() 
     local directory = "entities"
     local entities = love.filesystem.enumerate(directory)
     
     for i, e in ipairs(entities) do 
      require(directory .. "." .. e)
     end
end
I get the lua error attempt to call field 'enumerate' a nil value.
User avatar
rmcode
Party member
Posts: 454
Joined: Tue Jul 15, 2014 12:04 pm
Location: Germany
Contact:

Re: Require all files in a directory

Post by rmcode »

Hey there, enumerate() was renamed to love.filesystem.getDirectoryItems().

Maybe this will help you too:
http://love2d.org/forums/viewtopic.php?f=4&t=33619
Psyk
Prole
Posts: 8
Joined: Thu Sep 11, 2014 10:40 pm

Re: Require all files in a directory

Post by Psyk »

rmcode wrote:Hey there, enumerate() was renamed to love.filesystem.getDirectoryItems().

Maybe this will help you too:
http://love2d.org/forums/viewtopic.php?f=4&t=33619
Thanks for letting me know that the enumerate function was renamed!

I thought of something else that works the same and gets the job done. Thanks for your help!

Working Code:

Code: Select all

require "love.filesystem"

function includeAllEnts()
	local dir = "entities"
	local entities = love.filesystem.getDirectoryItems(dir)

	for k, ents in ipairs(entities) do
		trim = string.gsub(ents, ".lua", "")
		require(trim)
	end
end
User avatar
undef
Party member
Posts: 438
Joined: Mon Jun 10, 2013 3:09 pm
Location: Berlin
Contact:

Re: Require all files in a directory

Post by undef »

Shouldn't it rather be:

require(dir .. "/" .. trim) in this code:

Code: Select all

function requireDirectory( dir )
   dir = dir or ""
   local entities = love.filesystem.getDirectoryItems(dir)

   for k, ents in ipairs(entities) do
      trim = string.gsub( ents, ".lua", "")
      require(dir .. "/" .. trim)
   end
end
Otherwise this should only require all files in the root directory, if I'm not mistaken.
I also think passing the directory as a parameter is convenient.

You don't need to require love.filesystem as well, all love modules are loaded unless deactivated in the config file.
twitter | steam | indieDB

Check out quadrant on Steam!
Post Reply

Who is online

Users browsing this forum: Ahrefs [Bot], Bing [Bot] and 4 guests