during the last weeks I’ve been writing my first Defold game as a hobby with my kids. So far everything went well and we tested the game on my(MacOS) dev machine. However, when trying to bundle the game for windows (or HTML5 etc.) I get errors for all Lua modules.
I have included the modules like this so far: local m = require("/path/my_module")
This works on the dev machine just fine, but when building for Windows it gives: the module '//path/my_module' can't be found
After some search I tried to replace the separator with “.”, as they should be replaced with the OS separator. local m = require("path.my_module")
This way it bundles for Windows, however, it no longer runs one my dev machine and I get the module 'path.my_module' can't be found
Any ideas what I am doing wrong and how to do it correctly? Thanks!
Case is correct, there must be something else wrong. I’m also wondering about the error message - shouldn’t it have replaced the “.” with the correct separator there already?
The structure is like this (both paths are directories in the project root): /path1/my_module.lua /path2/my_script.script
my_script is trying to access the lua module using the above mentioned local m = require "/path1/my_module" (which works for building in the IDE, but not when bundling) local m = require "path1.my_module" (which works for bundling, but not in the IDE)
I can’t seem to find a way that works for both cases.
Thanks for that! After playing around with your test project I found the cause of the problem. Defold does not like to mix separators, even in different require instructions. So if the same project contains some require statements using “/” as separator and some using “.” it will not work.
It looks like Defold takes the first require statement it finds during a build and only accepts the separators found in that one. I have enhanced your sample project to demonstrate this.
local test2 = require("path1.test")
local test1 = require("/path1/test")
function init(self)
print("main")
test1.hello()
test2.hello()
end
Defold will not accept the second require statement - it doesn’t matter which one is first.
I have now changed all my require statements to using “.” notation and that works, so my problem is resolved.
If my assumption is correct, that is a weird behaviour of Defold but explains my observations. Unless there is a reason behind this behaviour it might be a good idea to change how Defold handles mixed separators in future versions, though.
Yes, maybe. I tried the following using Lua 5.1.5:
local test2 = require("path1.test")
local test1 = require("/path1/test")
print("main")
test1.hello()
test2.hello()
for k,v in pairs(package.loaded) do
print(k,v)
end
I just meant the same error you are getting: module '/path1/test' not found for the second require statement (no matter if the one with “.” or “/” is first). I assume this error is always thrown when Defold encounters a path with separators that are different from the first require statement.