It's a compiler error. Basically in lua you cant do that I suppose
Recursive Require
Forum rules
Before you make a thread asking for help, read this.
Before you make a thread asking for help, read this.
- yetneverdone
- Party member
- Posts: 448
- Joined: Sat Sep 24, 2016 11:20 am
- Contact:
Re: Recursive Require
My GameDev Website
Going Home:A Pixelated Horror Game
My Repositories!
Follow me lovingly!
Nga pala, pinoy ako.
Going Home:A Pixelated Horror Game
My Repositories!
Follow me lovingly!
Nga pala, pinoy ako.
Re: Recursive Require
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:
And it compiles and runs fine for me o.o
Compiler I used
Code: Select all
local filename = "math.lua"
local tree = {}
tree[filename:gsub(".lua","")] = require(filename:gsub(".lua",""))
print(tree["math"].min(10, 100))
Compiler I used
Last edited by Lucyy on Tue May 16, 2017 9:25 am, edited 1 time in total.
- yetneverdone
- Party member
- Posts: 448
- Joined: Sat Sep 24, 2016 11:20 am
- Contact:
Re: Recursive Require
Lucyy wrote: ↑Tue May 16, 2017 7:36 amI 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
And it compiles and runs fine for me o.oCode: Select all
local filename = "math.lua" local tree = {} tree[filename:gsub(".lua","")] = require(filename:gsub(".lua","")) print(tree["math"].min(10, 100))
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
My GameDev Website
Going Home:A Pixelated Horror Game
My Repositories!
Follow me lovingly!
Nga pala, pinoy ako.
Going Home:A Pixelated Horror Game
My Repositories!
Follow me lovingly!
Nga pala, pinoy ako.
Re: Recursive Require
Let me know if it works!
- yetneverdone
- Party member
- Posts: 448
- Joined: Sat Sep 24, 2016 11:20 am
- Contact:
Re: Recursive Require
My GameDev Website
Going Home:A Pixelated Horror Game
My Repositories!
Follow me lovingly!
Nga pala, pinoy ako.
Going Home:A Pixelated Horror Game
My Repositories!
Follow me lovingly!
Nga pala, pinoy ako.
-
- Prole
- Posts: 12
- Joined: Mon May 22, 2017 3:45 am
Re: Recursive Require
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
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
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
Re: Recursive Require
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:
References - 1, 2, 3
EDIT: Just realised this post is a bit old, sorry about that.
Code: Select all
_G[MODULE_NAME] = require("flux.lua")
MODULE_NAME.to(...) -- this will work
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
personal page and a raycaster
Who is online
Users browsing this forum: Ahrefs [Bot], Google [Bot] and 8 guests