Very noob question about modular coding etc...

As former Flash user I have found that "A Better Approach to External Modules" blog post by Jonathan gives an excellent insight into the way how to make code modular and easier to control. BUT! As former Flash user I have BIG problem with "targeting" of variables and methods. This is really driving me crazy last two days...please help!!!

I'll try to describe the problem the best I can:

I have two lua files "one.lua" and "two.lua" which are loaded in the main.lua file with "require" (as seen in the example below). Questions are:

  1. How to call function x (main.lua) from one.lua file?
  2. How to call function y (two.lua) from one.lua file?
  3. How to read variable v (main.lua) from two.lua file?

Thanks!

Hi

Not sure about this but:

1- remove local in front of function x in main. Then it should be available in both one.lua and two.lua

2- require two.lua. in one.lua

3- use: local temp = _G.v

Hope this is correct.

Mo

I would structure the code so as not to call back into main.lua from another module.
There may be a good reason to do this that I'm not aware of, but Id avoid it if possible.

As to calling two.lua function y from one.lua:

two.lua needs to return m.
one.lua needs to require("two"), and then can call function y, such as:

1
2
local myTwo = require("two")
myTwo.y()

Thank you all.

What I am trying to do is to have "Class" file for every object I have in my project. (if it is possible - of course) This is what I managed so far regarding calling main.lua function from one.lua:

(if I want to call function printOK in "main.lua" from "one.lua" file)

-- main.lua --

local master = {}
local function printOK()
    print("OK")
end
master.printOK = printOK
local one = require "one"
one.setTarget(master)

-- one.lua --
local m = {}
local target
local function setTarget(t)
    target = t
end
m.setTarget = setTarget

...some event
    target.printOK()
end...

return m

...and this works just fine. I think that sending "target" tables as function parameters can be the answer to everything I asked here. BUT, as newbie, I would like to know if this way of solving things have flaws regarding memory optimization or some other problems. Is the sending of tables as parameters and calling functions from parameter can cause problems?

Thanks again

That's very interesting that you set target to a function in main.lua. I wouldn't normally do that, but I don't know that it does any harm (it just seems confusing to me).

What you may also have read is that once a module is "required" by any of your modules, then when it is subsequently required by another of your modules, that 2nd (or nth) require points to the same copy, including its variables and functions. This is a nice way to share data across multiple modules.

I don't know of any particular problems relating to this way of doing things. I'm somewhat of a Corona newbie myself, but I haven't run into any issues with my (relatively simple) apps so far.

Thank you Carbondale

Your explanation of modules gives me a hope :-)

I was concerned that having table with lots of code (thousands of lines...) and sending it as parameter, doubles the amount of code. But now I guess it's only instance of original table...
I think that by having "central" table in main.lua file and sending it as reference to all modular scripts I am creating some kind of "telephone central" which can be used for all kind of communications between all scripts that use reference to "central" table...

It would be nice addon to have some kind of profiling tool to visually see how memory is used by the code (like Sun's J2ME profiler...)

I'm glad if could help a bit - I'm still learning myself. I think all the modules which have been "required" are loaded to global system table called "packages.loaded" which you can inspect. So when another require is done, the system first checks that table and if there, just points you to it. Most of what I've learned about this particular modular approach was from Jonathan Beebe and others on the forums, plus my own experimentation.

There is what looks like a very nice profiler called "Corona Profiler", sold by a 3rd party for about $10. I've seen it mentioned in the forums recently, and it looks really nice. I think I'll pick it up soon.

EDIT
"package.loaded", not "packages.loaded"

Hey guys,

Just FYI, I`ve bought yesterday the Corona Profiler of M.Y.developers and that tool is just amazing IMHO.

So for $9 bucks it is a interesting helper tool for us Corona/Lua Devs btw.

PS: It`s just my own opinion as a customer of.

Cheers,
Rodrigo.

Thanks for the info Rodrigo. I'm definitely planning to buy it after seeing a demo of it on the forums yesterday.

@Carbondale you`re welcome!

As you said above I`ve done the same thing as you (looked impressed with the graphs made by the Corona Profiler yesterday) and so I got it via M.Y.developers site: http://www.mydevelopersgames.com/site/ (in this link you can see 3 DEMOS of the Profiler running and those graphs are better yet).

IMO it was a good investment. However now I am able to see my big memory leaks mistakes and sometimes the reality is not so good to be knowing, lol. :)

Happy 2012!

Cheers,
Rodrigo.

Hi guys.

Thanks a lot of thid threat. It always nice to learn new things about Lua.

In term of the profiler, I second that (amazing) Since i bought it I found so many flaws in my game it is not even funny. A game that look like was almost (since it ran ok on my ios device) turned out to be full of holes and memory leaks and stupid programming mistakes. Because the profiler shows you line by line where your app spent the most time (or the most memory) you can really tell where it is missed up. No guess work.

Cannot say how much Profiler helped me. Just one request: More documentation(especially on the memory profling two modes) I really feel the app is very powerful but I am only using only a small portion of it potential. But I also know that those guys are busy cooking even better things for us so it is all good!

You cannot wrong with the Profiler!

Mo.

Hey @lemsim - I agree with all you said above! :)

Thanks,
Rodrigo.

@Rodrigo: Where is all the fun if you agree with everything I said? ha! ha! Thanks and have yourself (and all the people here) a n Happy New Year 2012 full of app sales:)

Mo

@lemsim - Hahaha,

"Happy New Year 2012 full of app sales:)"

I totally agree again! :)

Best wishes mate,

Cheers,
Rodrigo.

Happy New Year to all :-)
I started this thread with three questions and got more than expected. Thank you for info about profiler. I am going to buy it...

Best regards, Coronians ;-)

Ha, Ha,

You`re Welcome @srdjan.markovic!

Cheers,

I just bought the profiler today also.

And Happy New Year to all of you!

views:2176 update:2012/1/4 9:12:54
corona forums © 2003-2011