Hi. I’m trying to dynamically load a lua module based on an id:
[Simplified example]
local id = 1
local modules = {
"game.first",
"game.second",
"game.third",
}
local my_module = require(modules[id])
And I’m getting an error “module ‘game.first’ not found”.
But when I do
local my_module = require("game.first")
Everything seems to work fine. I’m thinking it’s because the module itself has to be referenced somewhere to be included?
It seems like a defold thing to include only things that are mentioned/needed. Is that what’s happening? Is there a workaround ?
If you really need to load code dynamically you can also use dofile(). This loads and interprets a chunk of lua in place. This would be the easiest drop-in replacement.
You can do
local my_module = dofile(“game/first.lua”)
However, since your app bundle will not contain your .lua files as is normally, you’d have to copy them manually.
Another possible route would be to bundle dynamic lua files as custom resources and then go resource.load(“game/first.lua”). Then interpret the result using loadstring().
The latter is probably safer, and would likely work on every platform, through no guarantees. Both would probably require some tinkering to get working reliably.
Neither of these approaches I would really recommend, though I use dofile() during development for hot reloading things that are tricky to hot reload using Defold’s regular mechanism.
Using dofile() could unlock potentially unlock powerful modding features for a community, now that I think about it.