Lua require module name as variable

Hello!
I’m trying dynamically load module according to some config file where stored path to that module. So code looks like that:

local config_file = sys.load_resource("/resources/config.json")
local config_obj = json.decode(config_file)
local class_instance = require(config_obj.module)
local object = class_instance(config_obj.kwargs)

And config.json looks like that:

{
  "module": "modules.some_class",
  "kwargs": {
    "arg1": "val1",
    "arg2": "val2"
  }
}

And it fails with: module "modules.some_class" not found. But if i just wrote anywhere above require("modules.some_class") it works just fine.

So how I can fix it without requiring all possible modules above? It seems on building stage Defold excludes lua files that didn’t required anywhere. Does Defold had option to include all lua files under specific folder in build or something?

No such option, you have to mention every filename in order for Defold to bundle them.

1 Like