[SOLVED] How do you load a Lua module within a subdirectory

This seems like such a simple thing to do but I can’t figure out how to do it! :smile:

All I want to do is require a Lua module, from within my main.lua file like so:

require "lang.Common"

The folder directory structure looks something like this:

res/
main.lua
+ lang/
  + Common.lua
+ game/
  + Config.lua
+ fx/

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.

Thank you!

Try to use it like this.

 require("frontend/utils/Language"):getString("key")
 require("frontend.utils.Language"):getString("key")

The paths are okay if you ask me.

Thank you for the response!

Here’s what I’ve tried so far without any luck:

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

Hmm…

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 :smiley:

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?

OK. I’m done with this problem.

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:

cc.FileUtils:getInstance():addSearchResolutionsOrder("src/lang");
require "Primitives"

I would greatly appreciate if anyone could provide any advice. Preferably I would like to be able to load modules with their full path.

The getString(“key”) was just an example for using a function of the module. Sorry I should have said that :smiley:

The lua documentation for modules is http://lua-users.org/wiki/ModulesTutorial or here http://www.tutorialspoint.com/lua/lua_modules.htm
You can store the given module in a local variable. For example

local myModule = require ("path/to/module/nameOfModule")

We are using lua too and we don’t need to add every directory to the search paths. The main.lua has following addSearchPaths

cc.FileUtils:getInstance():addSearchPath("src")
cc.FileUtils:getInstance():addSearchPath("res")

That’s it. Sorry if it isn’t working for you. Can you provide a small example cocos2d-x project?

Thank you! I’ll take a look at a little later when I have some time :smile:

Unfortunately I don’t have a small project at the moment, because of how I set up the latest project.

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.