Page 2 of 2
Re: Recursive Require
Posted: Tue May 16, 2017 7:29 am
by yetneverdone
Lucyy wrote: ↑Tue May 16, 2017 7:26 am
What error does it throw?
It's a compiler error. Basically in lua you cant do that I suppose
Re: Recursive Require
Posted: Tue May 16, 2017 7:36 am
by Lucyy
I can't really test it myself right now as I don't have Lua or Love installed on my work computer, but I put the following code in an online compiler:
Code: Select all
local filename = "math.lua"
local tree = {}
tree[filename:gsub(".lua","")] = require(filename:gsub(".lua",""))
print(tree["math"].min(10, 100))
And it compiles and runs fine for me o.o
Compiler I used
Re: Recursive Require
Posted: Tue May 16, 2017 9:24 am
by yetneverdone
Lucyy wrote: ↑Tue May 16, 2017 7:36 am
I can't really test it myself right now as I don't have Lua or Love installed on my work computer, but I put the following code in an online compiler:
Code: Select all
local filename = "math.lua"
local tree = {}
tree[filename:gsub(".lua","")] = require(filename:gsub(".lua",""))
print(tree["math"].min(10, 100))
And it compiles and runs fine for me o.o
Compiler I used
[/quote
Oh, i didn't notice that you're indexing in the tree. That works i think, im going to check it later
Re: Recursive Require
Posted: Tue May 16, 2017 9:25 am
by Lucyy
Let me know if it works!
Re: Recursive Require
Posted: Tue May 16, 2017 4:25 pm
by yetneverdone
Lucyy wrote: ↑Tue May 16, 2017 9:25 am
Let me know if it works!
it works! thanks alot
Re: Recursive Require
Posted: Sun May 28, 2017 5:54 am
by therektafire
Hey, I was just now looking for a way to do a recursove require, and for love2d as well
Before I saw this thread I came up with this function, I'm a pretty big noob at Lua so it's probably totally wrong LOL, but oh well at least I tried
Basically it is supposed to take the name of your directory where your libraries are, the library directory itself, and a table with the names of the modules you want (in case you don't want all the modules) and it is supposed to return a table with the acquired modules. Here is what I have so far
Code: Select all
function recuire(libsDir, libName, requiredModules)
--create table to store the modules
local modules = {}
local currentModule = 1
--fill module table with required modules and return it
for currentModule, #requiredModules, 1 do
modules[requiredModules[currentModule]] = require "./"..libsDir..libName.."/"..requiredModules[currentModule]
end
return modules
end
Since require doesn't need you to put the .lua at the end of your module file names I don't think I really need to use gsub in this case. BUT I'm not sure if require can use file paths like how I use it here, so if it can't then, well idk hehe, I'll just have to git gud enough to figure it out
Re: Recursive Require
Posted: Tue May 30, 2017 6:14 am
by Davidobot
If you don't mind the module being a global variable, you can use the built-in global table to assign it a value and use it conveniently:
Code: Select all
_G[MODULE_NAME] = require("flux.lua")
MODULE_NAME.to(...) -- this will work
References -
1,
2,
3
EDIT: Just realised this post is a bit old, sorry about that.