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:

Re: Recursive Require

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

Re: Recursive Require

Post 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
Last edited by Lucyy on Tue May 16, 2017 9:25 am, edited 1 time in total.
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 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
Lucyy
Citizen
Posts: 51
Joined: Thu Oct 15, 2015 6:57 am
Contact:

Re: Recursive Require

Post by Lucyy »

Let me know if it works!
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 9:25 am Let me know if it works!
it works! thanks alot
therektafire
Prole
Posts: 12
Joined: Mon May 22, 2017 3:45 am

Re: Recursive Require

Post by therektafire »

Hey, I was just now looking for a way to do a recursove require, and for love2d as well :ultrahappy: 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
User avatar
Davidobot
Party member
Posts: 1226
Joined: Sat Mar 31, 2012 5:18 am
Location: Oxford, UK
Contact:

Re: Recursive Require

Post 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.
PM me on here or elsewhere if you'd like to discuss porting your game to Nintendo Switch via mazette!
personal page and a raycaster
Post Reply

Who is online

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