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! [import]uid: 101952 topic_id: 19741 reply_id: 319741[/import]

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 [import]uid: 49236 topic_id: 19741 reply_id: 76413[/import]

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:
[lua]local myTwo = require(“two”)
myTwo.y()[/lua]
If you want to provide access to variable z in one.lua, I’d recommend not making it directly accessable, but making a getter and setter for it in one.lua, such as:
[lua]local function getZ()
return Z
end
m.getZ = getZ
local function setZ(val)
Z = val
end
m.setZ = setZ[/lua]

Then any module which requires one.lua can call one.getZ() or one.setZ(5).

EDIT:
Also, your ‘m’ variable in the modules should be local also (local m = {})

[import]uid: 22076 topic_id: 19741 reply_id: 76430[/import]

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 [import]uid: 101952 topic_id: 19741 reply_id: 76503[/import]

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.
[import]uid: 22076 topic_id: 19741 reply_id: 76509[/import]

Thank you Carbondale

Your explanation of modules gives me a hope :slight_smile:

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…) [import]uid: 101952 topic_id: 19741 reply_id: 76583[/import]

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. [import]uid: 89165 topic_id: 19741 reply_id: 76621[/import]

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” [import]uid: 22076 topic_id: 19741 reply_id: 76595[/import]

Thanks for the info Rodrigo. I’m definitely planning to buy it after seeing a demo of it on the forums yesterday. [import]uid: 22076 topic_id: 19741 reply_id: 76622[/import]

@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. :slight_smile:

Happy 2012!

Cheers,
Rodrigo. [import]uid: 89165 topic_id: 19741 reply_id: 76632[/import]

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. [import]uid: 49236 topic_id: 19741 reply_id: 76634[/import]

Hey @lemsim - I agree with all you said above! :slight_smile:
Thanks,
Rodrigo. [import]uid: 89165 topic_id: 19741 reply_id: 76636[/import]

@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 [import]uid: 49236 topic_id: 19741 reply_id: 76637[/import]

@lemsim - Hahaha,

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

I totally agree again! :slight_smile:
Best wishes mate,

Cheers,
Rodrigo. [import]uid: 89165 topic_id: 19741 reply_id: 76641[/import]

Happy New Year to all :slight_smile:
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 :wink:
[import]uid: 101952 topic_id: 19741 reply_id: 76672[/import]

Ha, Ha,

You`re Welcome @srdjan.markovic!
Cheers, [import]uid: 89165 topic_id: 19741 reply_id: 76677[/import]

I just bought the profiler today also.

And Happy New Year to all of you! [import]uid: 22076 topic_id: 19741 reply_id: 76681[/import]

Just wanted to say that (after heavy testing with huge number of scripts and M.Y.profiler) my example above haven’t caused any memory leak so far…
And, yes, the M.Y.profiler is excellent tool!
It would be nice to have it included as part of Corona package.
It is like having x-ray eyes for code :slight_smile:

Cheers [import]uid: 101952 topic_id: 19741 reply_id: 78377[/import]

Hello guys,
Sorry but we actually just saw this thread after searching for profiler to see where else in the forums this shows up. Thank you for all the feedback it is much appreciated!
Just to answer some questions presented in this thread
@srdjan.markovic

“Is the sending of tables as parameters and calling functions from parameter can cause problems?”
No it will not, all tables are passed by reference in Lua. Only a “primitive type” is copied, in Lua these are strings and numbers.


It would be nice to have it included as part of Corona package.

We agree :), that would be very nice indeed!
Oh and have you tried out the newer memory leak detection tools? They can tell you exactly what variables are leaking now.

@Carbondale
One thing that we want to mention about the getter and setter methods. Yes it is true that this is the way it is done in say object oriented programming paradigms (known as encapsulation) but in terms of performance we suggest you make things directly accessible. Desktops have been getting faster and faster so these days program readability trumps program performance. However, using a get() and set() method in a game loop will kill your performance. In our experiences, even calling an empty act() function for the sake of readability and elegance will slow down your game due to the overhead of calling and returning from a function. The overhead becomes even more significant because we are calling functions from Lua which itself is a layer on top of C. Not saying it has to be done this way, just our two cents :slight_smile:

Thanks,
M.Y. Developers

[import]uid: 55057 topic_id: 19741 reply_id: 85679[/import]

Thanks for the great info. I will definitely follow your advice. I’m not even a big OOP afficianado, and directly exposing the variables is simpler and less code anyway - I guess I got carried away with my advice :slight_smile: [import]uid: 22076 topic_id: 19741 reply_id: 85688[/import]