Another example; if I wanted to load the game’s configuration I would like to do the following:
require "game.Config"
As of right now, when I attempt to do this, I get the following error:
module 'lang.Common' not found:
I’ve tried several variations of adding the folder path src/lang on the LuaEngine->addSearchPath, using CCFileUtils, etc. I’m just not having any luck.
Also, I made sure that these files could be loaded by executing the script from within the command line.
require("lang.Common"):getString("key")
require("src.lang.Common")
require("src.lang.Common"):getString("key") -- prints 'attempt to index a boolean value'
require("src.lang"):getString("Common")
require("lang"):getString("Common")
Am I doing this right?
Lua’s documentation doesn’t provide anything in regards to the value being returned by the require function at http://www.lua.org/pil/8.1.html. I expected it to say it returns a table. So I’m not sure what the :getString("key") part does.
My next plan of action is to figure out if I can see the resources directory being copied in its entirety to the device. I’ll keep you updated.
Yes. I see that the files are being copied over to the correct location:
$ ls ~/Library/Developer/CoreSimulator/Devices/AD4BF7BE-9BA3-4A1D-A2B6-F938C248CF13/data/Applications/3A9EA3BD-3D4F-4D78-B23B-5992442447F7/MyApp.app/src/lang/Common.lua
God damn. I will PAY someone to give me the answer! This is so ridiculously simple and I don’t feel like taking another 3 days to figure this out
Are my Lua search paths not correct; as set with LuaEngine?
Are my main project search paths not correct; as set with CCFileUtils?
Is this not even supported? Am I not able to load Lua files within subdirectories? Do I need to add each of the subdirectories in the search path?
I’ve been stuck on this problem for more than five hours and tried every conceivable permutation. Running from the command line works PERFECTLY! So why is this not working when I run it on a device or simulator?
I just added every one of the directories to the search paths. This is NOT ideal as now I have to rename module file names that have similar names to other modules.
I renamed the Common module to Primitives and added the lang/ directory to the search path:
OK… I figured it out. I was trying so hard to load the file from the src/ directory that I failed to load the file while including the src directory when loading the module. This works:
require("src.lang.Common")
As you may have noticed, one of the clues that lead me to this conclusion was this command that emitted “attempted to index a boolean value”:
require("src.lang.Common"):getString("key")
Because of the way I have my modules setup, everything that gets loaded within the module becomes a global variable. I do not create a module table and return the table within the module similar to the following:
local mymodule = {}
function mymodule.foo()
print "bar"
end
return mymodule
Instead, I’m doing something like the following:
function foo()
print "bar"
end
Because of that, require was returning a boolean value true to indicate that the module loaded successfully.
So the only trick is to ensure you provide the path from the src/ directory when loading modules in a subdirectory. It makes complete sense, as the base directory of your project is one directory up from the src/ directory. But, since I have included src/ as part of the search path it allows you to load Lua files that exist within the src/ directory without issue. That being said, it doesn’t allow you to load files, within sub-directories, within folders that have been added to the addSearchResolutionOrder(). This is as intended as the search resolution order is meant only to define the order of loading files. It doesn’t actually change the base search path.
I’m sure there is still a way I can load modules relative to the src/ directory, without providing the src. within the module’s path, but that’ isn’t a problem for me. I’m perfectly fine with adding src. to the module path for now.