Page 1 of 1
Fast way to require files?
Posted: Thu May 15, 2014 12:37 am
by xFade
Code: Select all
local files = love.filesystem.getDirectoryItems(fdir)
function love.load()
print("Loading...")
for i,v in ipairs(files) do
print("Requiring..."..v)
-- require = files This does not work :,(
end
end
Is there a nice way to require files using a for loop? I tried the above it did not work
Re: Fast way to require files?
Posted: Thu May 15, 2014 12:58 am
by HugoBDesigner
I also wanted to know this... I was going to use this for an old project.
To not make a pointless post, I think that require(text) works in this case too. But I'm not sure. I always use require "text".
Re: Fast way to require files?
Posted: Thu May 15, 2014 1:42 am
by xFade
Code: Select all
function love.load()
print("Loading...")
for i,v in ipairs(files) do
print("Requiring..."..v)
require(v) -- This being line 20
end
end
It requires it however.... It gives me an error 'main.lua:20: module'intro.lua' not found
Re: Fast way to require files?
Posted: Thu May 15, 2014 2:53 am
by substitute541
You should check first if the file is a .lua file. If it is, strip the ".lua" from the filename and require it. If it isn't, you can either ignore it, or check if it's a folder and require all the files in that folder (using recursion.)
Re: Fast way to require files?
Posted: Thu May 15, 2014 2:59 am
by Inny
Probably a bad idea to require files that way, since the order of files isn't specified.
However, as an alternative, you could autoload files based on the first attempt to lookup something. I.E. you set the global metatable to have an __index that will look for a file whose name matches the item you're attempting to read.
Code: Select all
setmetatable(_G, {
__index = function(_, key)
if love.filesystem.exists(key..'.lua') then
local value = require(key)
rawset(_G, key, value)
return value
end
end,
})
I haven't tested this particular piece of code, but I did something like it in the past. It obviously requires the file loaded to return a value.
Re: Fast way to require files?
Posted: Thu May 15, 2014 8:19 am
by Roland_Yonaba
The Great Headchant implemented something similar in his boilerplate. It is a recursive way to require files.
See
here.
Re: Fast way to require files?
Posted: Thu May 15, 2014 8:43 am
by kikito
I made this some time ago:
https://github.com/kikito/fay/blob/mast ... equire.lua
It takes a folder like this:
Code: Select all
lib
├── bar.lua
├── baz
│ ├── x.lua
│ └── y.lua
└── foo.lua
And then you can do this:
Code: Select all
require 'require'
local lib = require.tree(lib)
lib.bar -- contents of lib/bar.lua
lib.baz.x -- contents of lib/baz/x.lua
As a bonus, it also allows requiring stuff using relative paths.
Code: Select all
-- lib/bar/x.lua
require 'require'
local y = require.relative(..., 'y') -- requires 'lib/bar/y.lua' without having to specify "lib.bar.y"
I did it for a Ludum Dare some time ago so the code is not as clean as I'd like it to be. In particular,
I'd make it return a local table instead of overriding the global require.
Re: Fast way to require files?
Posted: Thu May 15, 2014 11:23 am
by xFade
Code: Select all
function love.load()
print("Loading...")
for i,v in ipairs(files) do
print("Requiring..."..v)
v = v:sub(1,-5)
require(fdir..v)
end
gamestate = "intro"
IntroScreen:start()
end
Thank you all for replying, the above code works fine.