Communicating between Modules?!

So i have started a new game project. This time around im trying to code my game in a better manner, having my game mechanic code in its own .lua file, and id like to have each level in its own .lua file. The problem im running into, is that i am able to communicate from 1 module to the other, but not vice versa.

for example. in my "level1" file, i do the following

1
local mechanic = require("mechanic")

I declare each module as a global variable in main.lua like the following:
g_menu = require("g_menu")
g_inst = require("g_inst")
g_play = require("g_play")

this way, you can call module g_menu's function from module g_inst or vice versa. Or from any module to call any module's funciton. script like the following:
g_menu.getLocalGroup() in module g_inst.

Great, thanks for the reply, ill give that a try right now!

To get what you are after, you need to organise your files accordingly. You are after modular programming, where you want to add a particular module and get it to work. Right?

I will tell you the easiest way, the one that you have set out on. Your main file should have the logic of your game, the level related stuff can be in the level files.

If you were using plain lua, not the coronaSDK sandboxed version, you would have a few more commands that would give you a lot more flexibility.

If you also have a look at the sample from Johnathan Beebe's Ghost Vs Monsters, it will show you a way, but I guess there each level file is the logic in itself.

You can also read up on how to use external modules/functions
Part #1 here
Part #2 here
Part #3 here

cheers,

?:)

Thank you so much for your post, i was still kind of unsatisfied on making this work how i wanted, but your tutorials seem to be just what the doctor ordered, ill read them over and let you know how it goes, thank you again.

consider to setup your modules without

module(...,package.seeall)

there are some problems with that...
When you require a module, all functions become global.
and that is bad in several ways.
it's easy to set up your "modules" without making the functions global and risk memory leaks and crashes you can not fix.

---> http://blog.anscamobile.com/2011/09/a-better-approach-to-external-modules/

cheers
-finefin

Yea i actually began this whole process by running across john beebees recent blog post that you put there. Ive been trying to use his method that he explains, and like i stated in my first post, using his method, im able to communicate from my level file to functions in my mechanic file, but i cant call functions from the level file in my mechanic file ( hope that makes sense ) Everyone else must just have way more coding experience than me, because everyone on that blog post just says " oh yea thats perfect ". Ive had a hard time implementing, but ill keep trying!

everyone says "oh yeah perfect" because they know that they have to try to keep everything "local" - and the module function creates global objects.

so basically, you index all your functions inside a table and "return" that table to any other module that asks for it (via "require").

in that way your functions won't fly around in space (memory) and won't block variable names.

try this (bad code):

main.lua

1
2
require ( "mod1" ) -- require first module without function call 
require ( "mod2" ).myFunction () -- call the function of second module

Awesome, thanks for the in depth post. I did get it working how i would like by looking at your examples. My only question is on this line of code:

1
require ( "mod1" ).myFunction () -- output: MOD 1

yeah, that was quick and dirty, I know
but I think you can do that without memory issues and everything is garbage collected... prove me wrong :)

but sure, normally you would do something like this

1
2
local mod1 = require ( "mod1" )
local modFunction = mod1.myFunction()
views:1547 update:2011/10/4 8:06:35
corona forums © 2003-2011