问题
I have a lua file that will require another lua file to run but I can't hard code the file name. Can i use the require function on a variable or do i need to figure out an alternate approach to what i am doing?
For example
local path = "mypath.txt"
local level = require path
回答1:
Yes, you can. require "module"
is just a syntactic sugar for require("module")
that only works if call a function with single argument that is a string or table constructor. Use proper call in form of require(path)
and it will work.
来源:https://stackoverflow.com/questions/11836434/can-you-call-require-on-a-variable