Hi there,
I know that the Defold engine builds a list of all necessary lua scripts by parsing require() statements out of the code. However, there are a few scenarios that I think the engine could try to support. Scenarios that would work in regular lua, but that don’t work in Defold. Here they are:
- Importing a lua package
Consider a lua package consisting of a collection of lua modules like this:
- mypackage
- init.lua
- foo.lua
- bar.lua
In regular lua, the contents of mypackage/init.lua can be required like this:
require(‘mypackage’)
But Defold’s require parser looks for a file called “mypackage.lua” and can’t find it. It demands that the statement be written as:
require(‘mypackage.init’)
- Importing and indexing in a single statement
Sometimes I run into a situation where I have a module that contains a singleton-like instance of an object. Suppose I have a file called “model.lua” that contains the following:
local M = {}
M.model = init_my_model_or_whatever()
return M
In consuming code, I want to get access to this model instance, so my options are:
local model = require(‘model’)
model = model.model
But that’s a tad confusing. Another option is:
local libmodel = require(‘model’)
local model = libmodel.model
But again, that’s a tiny bit more work than I’d like. What I really want to do is:
local model = require(‘model’).model
But this isn’t recognized by the Defold require parser.
I’m posting this in the Bugs category, because I think that the rule of thumb should probably be “if it’s syntactically correct in lua, then it’s syntactically correct in Defold”. Even if you don’t feel that it’s necessary to add support for these cases, finding a way to provide more descriptive error messages to the developer might be nice. Tracking down the problem in the second use case took me quite some time because the require parser simply misses the statement all together, and the lua file can’t be found at runtime.