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

Recursive Require

Post 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
Lucyy
Citizen
Posts: 51
Joined: Thu Oct 15, 2015 6:57 am
Contact:

Re: Recursive Require

Post 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
User avatar
yetneverdone
Party member
Posts: 446
Joined: Sat Sep 24, 2016 11:20 am
Contact:

Re: Recursive Require

Post 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?
Lucyy
Citizen
Posts: 51
Joined: Thu Oct 15, 2015 6:57 am
Contact:

Re: Recursive Require

Post 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
User avatar
yetneverdone
Party member
Posts: 446
Joined: Sat Sep 24, 2016 11:20 am
Contact:

Re: Recursive Require

Post 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.
Lucyy
Citizen
Posts: 51
Joined: Thu Oct 15, 2015 6:57 am
Contact:

Re: Recursive Require

Post by Lucyy »

The variable will be in the table the function returns
User avatar
yetneverdone
Party member
Posts: 446
Joined: Sat Sep 24, 2016 11:20 am
Contact:

Re: Recursive Require

Post 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?
Lucyy
Citizen
Posts: 51
Joined: Thu Oct 15, 2015 6:57 am
Contact:

Re: Recursive Require

Post 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
User avatar
yetneverdone
Party member
Posts: 446
Joined: Sat Sep 24, 2016 11:20 am
Contact:

Re: Recursive Requireb

Post 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
Lucyy
Citizen
Posts: 51
Joined: Thu Oct 15, 2015 6:57 am
Contact:

Re: Recursive Require

Post by Lucyy »

What error does it throw?
Post Reply

Who is online

Users browsing this forum: Bing [Bot], Google [Bot] and 80 guests