Fast way to require files?

General discussion about LÖVE, Lua, game development, puns, and unicorns.
Post Reply
User avatar
xFade
Prole
Posts: 41
Joined: Mon Dec 23, 2013 6:04 pm

Fast way to require files?

Post 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 :P
(ง'̀-'́)ง
User avatar
HugoBDesigner
Party member
Posts: 403
Joined: Mon Feb 24, 2014 6:54 pm
Location: Above the Pocket Dimension
Contact:

Re: Fast way to require files?

Post 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".
@HugoBDesigner - Twitter
HugoBDesigner - Blog
User avatar
xFade
Prole
Posts: 41
Joined: Mon Dec 23, 2013 6:04 pm

Re: Fast way to require files?

Post 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
(ง'̀-'́)ง
User avatar
substitute541
Party member
Posts: 484
Joined: Fri Aug 24, 2012 9:04 am
Location: Southern Leyte, Visayas, Philippines
Contact:

Re: Fast way to require files?

Post 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.)
Currently designing themes for WordPress.

Sometimes lurks around the forum.
User avatar
Inny
Party member
Posts: 652
Joined: Fri Jan 30, 2009 3:41 am
Location: New York

Re: Fast way to require files?

Post 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.
User avatar
Roland_Yonaba
Inner party member
Posts: 1563
Joined: Tue Jun 21, 2011 6:08 pm
Location: Ouagadougou (Burkina Faso)
Contact:

Re: Fast way to require files?

Post by Roland_Yonaba »

The Great Headchant implemented something similar in his boilerplate. It is a recursive way to require files.
See here.
User avatar
kikito
Inner party member
Posts: 3153
Joined: Sat Oct 03, 2009 5:22 pm
Location: Madrid, Spain
Contact:

Re: Fast way to require files?

Post 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.
When I write def I mean function.
User avatar
xFade
Prole
Posts: 41
Joined: Mon Dec 23, 2013 6:04 pm

Re: Fast way to require files?

Post 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.
(ง'̀-'́)ง
Post Reply

Who is online

Users browsing this forum: No registered users and 5 guests