Organising Code in Subdirectories

Hi,

I want to group my code into subdirectories because things are getting cluttered in the root.

I want something like this;

root:

  main.lua

code:

  scenes:

    scene1.lua

    scene2.lua

    scene3.lua

  utils:

    graphicsUtils.lua

    databaseUtils.lua

I can see how to call scene1.lua from main.lua using require with the “.” to specify the subdirectory

But once executing the scene1.lua code, require seems to become “relative” to that file’s location.

How do I require code in a parent directory.

For example, inside scene1.lua I want to require graphicsUtils.lua

So I guess I am asking Either;

Is it possible to specify the parent directory, like in unix I would do; “…/utils/graphicsUtils.lua”

OR is there a way of specifying an absolute path for require, such that it starts again in root, in unix; “/utils/graphicsUtils.lua”

I would prefer to specify absolute paths, that way they are the same throughout the project.

I thought I could avoid the whole problem by manipulating package.path and although this worked on the simulator, it broke on the device.

All require statements operation from the root folder of your application, so you always specify the folder path down to the module being required. You never suffix with ‘.lua’ and the namespace is separated with ‘.’ characters, so the require statement for a file in the utils folder, called ‘stuff.lua’ would be:

require("utils.stuff")

Even if the file making the require is in /levels/support/something/

ok, I must have got something wrong, let me check again.

you are correct, i introducted a typo.

All require statements operation from the root folder of your application, so you always specify the folder path down to the module being required. You never suffix with ‘.lua’ and the namespace is separated with ‘.’ characters, so the require statement for a file in the utils folder, called ‘stuff.lua’ would be:

require("utils.stuff")

Even if the file making the require is in /levels/support/something/

ok, I must have got something wrong, let me check again.

you are correct, i introducted a typo.