Page 1 of 1

File inclusion for lazy people :D

Posted: Wed Jul 02, 2014 9:16 pm
by Julxzs
Since I'm very lazy when making games in Löve, I tend to let the code do the work of requiring the files I make. To celebrate me signing up to this forum, I'm sharing this with all you lövely people out there :nyu:

Code: Select all

if filesystem then return end
filesystem = {}
filesystem.include = {}

-- Include lua files from inside the game
function filesystem.include.internal(folder)
	for k, v in pairs(love.filesystem.getDirectoryItems(folder)) do
		if love.filesystem.isFile(folder.."/"..v) and v:find(".lua") then
			require(folder.."/"..v:gsub(".lua", ""))
		elseif love.filesystem.isDirectory(folder.."/"..v) then
			filesystem.include.internal(folder.."/"..v)
		end
	end
end
-- Include lua files from appdata
function filesystem.include.external(folder)
	for k, v in pairs(love.filesystem.getDirectoryItems(folder)) do
		if love.filesystem.isFile(folder.."/"..v) and v:find(".lua") then
			love.filesystem.load(folder.."/"..v)()
		elseif love.filesystem.isDirectory(folder.."/"..v) then
			filesystem.include.external(folder.."/"..v)
		end
	end
end
-- Include lua files from the lib folder and return their result
function filesystem.include.lib(name)
	return require("lib/"..name)
end
Sharable github link:
https://gist.github.com/Julzso23/79699044ccf85367710d

Re: File inclusion for lazy people :D

Posted: Thu Jul 03, 2014 2:15 am
by SouL
Well, welcome then and thanks for sharing your efforts :megagrin: