Include Other .lua files

My projects getting quite large and I need to split it up into logical files

im using

– Load Interface
require(“interface”);

which loads the file interface.lua, so far so good

now heres the problem / question

in main.lua my code was the following

local interface()
–Do Interface Stuff
end
interface();

but that doesnt work when included with the require(interface), I have to instead remove the local from the function declaration

--------------interface.lua
interface()
–Do Interface Stuff
end
--------------main.lua
require(“interface”);
interface();
whats actually happening here? is it the same or am I opening myself upto memory leaks?
[import]uid: 5354 topic_id: 569 reply_id: 300569[/import]

For the functions in your interface.lua library that you want to make public (accessible outside that library), don’t use local in their function declaration, but use local when you require the library:

local interface = require( “interface” )

…and then you can reference those library functions as “interface.myFunctionName”, etc.

However, for private functions within interface.lua that you don’t access from main.lua, keep the local function declarations, which provide additional optimization.

Note that the reason for “local” isn’t just avoiding memory leaks, but also limiting the complexity of memory lookups within your compiled code – globals take slightly longer to access than locals, all things being equal. [import]uid: 3007 topic_id: 569 reply_id: 1112[/import]

1 Like

Thanks evank, unfortunately im still having trouble getting my head around this, basically I want to build my app using includes similar to php, where once the function is included it is available to the whole app, including other included functions.

If I have 3 files

– 1. main.lua
local monitorMP = require(“monitor”);
local orientationMP = require(“orientation”);
Runtime:addEventListener(“enterFrame”,orientationMP.test);

– 2. monitor.lua
module(…, package.seeall)
function monitorThis(clock)
print(clock)
end

– 3. orientation.lua
module(…, package.seeall)

function test(event)
monitorMP.monitorThis(os.clock())
end
My problem is that orientationMP.test cannot see monitorMP.monitorThis() saying it in nil. If I lose the local from the require statement " local monitorMP = require(“monitor”)" to " monitorMP = require(“monitor”) " the above works. But then isnt monitorMP going to slow down the application / memory access etc…

I am going to be including lots of different files so I would assume that not using the local declaration would be bad?

so the question is how can I get a function from one include to see a function in another include without compromising speed/memory? [import]uid: 5354 topic_id: 569 reply_id: 1147[/import]

If you worry about performance, here are some rules about optimization:

“The First Rule of Program Optimization: Don’t do it.
The Second Rule of Program Optimization (for experts only!): Don’t do it yet.”

  • Michael A. Jackson [import]uid: 4824 topic_id: 569 reply_id: 1149[/import]

Is there a way to require / include other .lua files (functions) so they can both see all variables / objects declared locally in main.lua and also see functions included in other .lua files? [import]uid: 5354 topic_id: 569 reply_id: 1159[/import]

I am also loading multiple .lua files and would highly recommend it, because it reduces the load time for a game to the point that load screens are unnecessary. However, I am currently encountering a serious problem with this method. I have not been able to unload the files, which is preventing me from being able to reload any level that has already been loaded. Do you happen to know how to unload files? [import]uid: 4871 topic_id: 569 reply_id: 1184[/import]

requesting feature loadfile or dofile support
http://developer.anscamobile.com/forum/2010/12/10/loadfile-or-dofile-support-load-code

i think those provide the required functionality in normal lua (not corona)
http://www.lua.org/pil/8.html

[import]uid: 6645 topic_id: 569 reply_id: 13617[/import]

@jmp909: I think I found the solution. You can create each level as a separate data file, as Carlos explains in this forum post: http://developer.anscamobile.com/forum/2010/09/18/loading-another-lua-file#comment-6474

Then, you can replace the code in the createLevel() function with code to load the data file. That way, you only need ONE level module, no matter how many levels you have. An example data file for a level in Ghosts vs. Monsters might look something like this:

[blockcode]
vSlab1,stone,600,200
vSlab2,stone,646,215
vPlank1,wood,633,225
[/blockcode]

In the createLevel() function, you can load the data file depending on what level has been chosen, and then loop through each line, parse the data between commas, and create the objects accordingly.

I think that’s the best solution because once again, you’d only need one level file (as opposed to “level1.lua”, “level2.lua”, and so on) and redundant code would be completely removed.

Once we’re ready to pursue the “big” project, if I have that function modified to parse data from an external file I’ll be happy to share it, or even change the code and re-submit it as a GvM update. [import]uid: 7849 topic_id: 569 reply_id: 13622[/import]

1 Like