I’m working on a module that allows users to require() modules and find instances using a custom path system (e.g. Import('Workspace/SomePart')
). I am providing support for relative paths but to do this I need to know the source script from which the require() is occurring. This easiest way to figure this out is just use getfenv()
as the environment provides the script
variable there. What I’m currently doing is Import('./SomeChild/AnotherChild', script)
where I pass script
in as the second parameter. This is ugly though and I would like to be able to dynamically grab the source script. The problem being getfenv()
disables Luau optimizations and I would like to avoid that. Is there any work-around for this problem?
Correct me if I’m wrong, but you’re pretty much rewriting the require()
function? Also, tbh, I’m not super sure if there is a work around to getfenv()
disabling Luau optimizations, I’ve never actually had to use this.
I don’t think there is any workaround to getfenv. getfenv disables optimizations because it means that the code is dynamic and the code’s behavior can’t be predicted. It may be the case that the optimizations would only be disabled for parts of the ModuleScript, but I wouldn’t put money on it.
This might be a little nicer than what you’re currently doing.
-- module
return function(s)
local script = s
return {
function Import(dir)
-- work your magic
end,
function doYouhaveAnotherFunction(
ifNotThenYouCanDitchTheTable,
andReturnImportDirectly
)
-- more magic
end,
}
end
-- script
local foo = require(bar)(script)
foo.Import('./SomeChild/AnotherChild')
I’m writing a wrapper around it to provide additional functionality. Roblox’s way of handling instances sucks whereas other languages use actual paths which imo are more intuitive.
Thanks for the reply I’m gonna end up doing something similar to this where I just provide the root/source script once.
local Import = Importer.new(script)
Import('./SomeChild/AnotherChild')
This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.