Page 1 of 2

Recursive Require

Posted: Mon May 15, 2017 4:33 pm
by yetneverdone
Okay, ive implemented headchant's recursiveRequire function like this

Code: Select all

function recursiveRequire(folder,tree)
 local tree = tree or {}
  for i,file in ipairs(love.filesystem.getDirectoryItems(folder)) do
    local filename = folder .. "/" .. file
    if love.filesystem.isDirectory(filename) then
        recursiveRequire(filename)
    elseif file:match(".lua") == ".lua" then
			require(filename:gsub(".lua",""))
		end
  end
  return tree
 end
It works, but how can i use for example kikito's love-loader module? In that module's wiki, it says i should use
local loader = require("love-loader")
so that i can reference it when using its functions, but with the recursive implementation above, i cant seem to refer to that module.

Help

Re: Recursive Require

Posted: Tue May 16, 2017 6:22 am
by Lucyy
It seems to me that the tree parameter in your function is never used, besides for returning it unchanged. And since they say to load Kikito's Love-loader as local loader = require("love-loader") I believe that you need to store that module in a variable.

Have you tried storing the required files in the returned tree variable? Maybe that'll work

Re: Recursive Require

Posted: Tue May 16, 2017 6:28 am
by yetneverdone
Lucyy wrote: Tue May 16, 2017 6:22 am It seems to me that the tree parameter in your function is never used, besides for returning it unchanged. And since they say to load Kikito's Love-loader as local loader = require("love-loader") I believe that you need to store that module in a variable.

Have you tried storing the required files in the returned tree variable? Maybe that'll work
Idk about the tree, it's from headchant's boilerplate.
Yes, i need to store that module in a variable, but if I used the recursive require system, i wont be able to store that module in a variable, unless i do

Code: Select all

LIBS = {}
table.insert(LIBS, require(etc etc))
LIBS[key of module] 
is not convenient.

What do you suggest about the tree?

Re: Recursive Require

Posted: Tue May 16, 2017 6:41 am
by Lucyy
In the function you posted the tree parameter is never used and gets returned unchanged at the end of the function, I'm assuming it's supposed to be used to store the libraries in.

This would mean you would need to change

Code: Select all

require(filename:gsub(".lua",""))
into

Code: Select all

table.insert(tree, require(filename:gsub(".lua","")))
This also means that you would want the files required from any subdirectories to be inserted into the same table, therefore you need to pass your current tree to the function call as well.

Code: Select all

tree = recursiveRequire(filename, tree)
EDIT:
This would change the function into:

Code: Select all

function recursiveRequire(folder,tree)
    local tree = tree or {}
    for i,file in ipairs(love.filesystem.getDirectoryItems(folder)) do
        local filename = folder .. "/" .. file
        if love.filesystem.isDirectory(filename) then
            tree = recursiveRequire(filename, tree)
        elseif file:match(".lua") == ".lua" then
	    table.insert(tree, require(filename:gsub(".lua","")))
	end
    end
    return tree
 end

Re: Recursive Require

Posted: Tue May 16, 2017 6:48 am
by yetneverdone
Lucyy wrote: Tue May 16, 2017 6:41 am In the function you posted the tree parameter is never used and gets returned unchanged at the end of the function, I'm assuming it's supposed to be used to store the libraries in.

This would mean you would need to change

Code: Select all

require(filename:gsub(".lua",""))
into

Code: Select all

table.insert(tree, require(filename:gsub(".lua","")))
This also means that you would want the files required from any subdirectories to be inserted into the same table, therefore you need to pass your current tree to the function call as well.

Code: Select all

tree = recursiveRequire(filename, tree)
EDIT:
This would change the function into:

Code: Select all

function recursiveRequire(folder,tree)
    local tree = tree or {}
    for i,file in ipairs(love.filesystem.getDirectoryItems(folder)) do
        local filename = folder .. "/" .. file
        if love.filesystem.isDirectory(filename) then
            tree = recursiveRequire(filename, tree)
        elseif file:match(".lua") == ".lua" then
	    table.insert(tree, require(filename:gsub(".lua","")))
	end
    end
    return tree
 end
But still, i can't access the module in the proper way using a variable.

Re: Recursive Require

Posted: Tue May 16, 2017 6:50 am
by Lucyy
The variable will be in the table the function returns

Re: Recursive Require

Posted: Tue May 16, 2017 6:52 am
by yetneverdone
Lucyy wrote: Tue May 16, 2017 6:50 am The variable will be in the table the function returns
Whats that variable's name or reference then? Would that be according to the name of the module?

Re: Recursive Require

Posted: Tue May 16, 2017 6:58 am
by Lucyy
I'm gonna assume it'll be a number index, if you want to call it by name (e.g. libs["modulename"]), you could replace

Code: Select all

table.insert(tree, require(filename:gsub(".lua","")))
with

Code: Select all

tree[filename:gsub(".lua","")] = require(filename:gsub(".lua",""))
And that should allow you to access it by using libs["module"]

I haven't tested this though, and it's been a while since I last wrote anything in Lua ;o

Re: Recursive Requireb

Posted: Tue May 16, 2017 7:23 am
by yetneverdone
I believe you cant do that in lua. Ive tried

Code: Select all

filename:gsub(".lua","") = require (etc etc)
Before. Throws an error

Re: Recursive Require

Posted: Tue May 16, 2017 7:26 am
by Lucyy
What error does it throw?