## Recursive Require

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.
yetneverdone
Party member
Posts: 378
Joined: Sat Sep 24, 2016 11:20 am
Contact:

### Recursive Require

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
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

Lucyy
Prole
Posts: 42
Joined: Thu Oct 15, 2015 6:57 am
Contact:

### Re: Recursive Require

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

yetneverdone
Party member
Posts: 378
Joined: Sat Sep 24, 2016 11:20 am
Contact:

### Re: Recursive Require

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?

Lucyy
Prole
Posts: 42
Joined: Thu Oct 15, 2015 6:57 am
Contact:

### Re: Recursive Require

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

yetneverdone
Party member
Posts: 378
Joined: Sat Sep 24, 2016 11:20 am
Contact:

### Re: Recursive Require

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.

Lucyy
Prole
Posts: 42
Joined: Thu Oct 15, 2015 6:57 am
Contact:

### Re: Recursive Require

The variable will be in the table the function returns

yetneverdone
Party member
Posts: 378
Joined: Sat Sep 24, 2016 11:20 am
Contact:

### Re: Recursive Require

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?

Lucyy
Prole
Posts: 42
Joined: Thu Oct 15, 2015 6:57 am
Contact:

### Re: Recursive Require

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

yetneverdone
Party member
Posts: 378
Joined: Sat Sep 24, 2016 11:20 am
Contact:

### Re: Recursive Requireb

I believe you cant do that in lua. Ive tried

Code: Select all

filename:gsub(".lua","") = require (etc etc)

Before. Throws an error

Lucyy
Prole
Posts: 42
Joined: Thu Oct 15, 2015 6:57 am
Contact:

### Re: Recursive Require

What error does it throw?

### Who is online

Users browsing this forum: No registered users and 54 guests