Jump to content

[TOPIC: topicViewTemplate]
[GLOBAL: userSmallPhoto]
Photo

Very noob question about modular coding etc...
Started by srdjan@multimediaworx.com Dec 29 2011 12:38 AM

- - - - -
20 replies to this topic
[TOPIC CONTROLS]
This topic has been archived. This means that you cannot reply to this topic.
[/TOPIC CONTROLS]
[modOptionsDropdown]
[/modOptionsDropdown]
[reputationFilter]
[TOPIC: post.html]
#1

srdjan@multimediaworx.com

[GLOBAL: userInfoPane.html]
srdjan@multimediaworx.com
  • Enthusiast

  • 89 posts
  • Corona SDK

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


[TOPIC: post.html]
#2

lemsim

[GLOBAL: userInfoPane.html]
lemsim
  • Contributor

  • 285 posts
  • Guests

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
uid: 49236 topic_id: 19741 reply_id: 76413


[TOPIC: post.html]
#3

Carbondale

[GLOBAL: userInfoPane.html]
Carbondale
  • Observer

  • 26 posts
  • Corona SDK

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:
local myTwo = require("two")myTwo.y()

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:
local function getZ()  return Zendm.getZ = getZlocal function setZ(val)  Z = valendm.setZ = setZ


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 = {})

uid: 22076 topic_id: 19741 reply_id: 76430


[TOPIC: post.html]
#4

srdjan@multimediaworx.com

[GLOBAL: userInfoPane.html]
srdjan@multimediaworx.com
  • Enthusiast

  • 89 posts
  • Corona SDK

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
uid: 101952 topic_id: 19741 reply_id: 76503


[TOPIC: post.html]
#5

Carbondale

[GLOBAL: userInfoPane.html]
Carbondale
  • Observer

  • 26 posts
  • Corona SDK

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


[TOPIC: post.html]
#6

srdjan@multimediaworx.com

[GLOBAL: userInfoPane.html]
srdjan@multimediaworx.com
  • Enthusiast

  • 89 posts
  • Corona SDK

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...)
uid: 101952 topic_id: 19741 reply_id: 76583


[TOPIC: post.html]
#7

@RSCdev

[GLOBAL: userInfoPane.html]
@RSCdev
  • Corona Geek

  • 1,489 posts
  • Corona SDK

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


[TOPIC: post.html]
#8

Carbondale

[GLOBAL: userInfoPane.html]
Carbondale
  • Observer

  • 26 posts
  • Corona SDK

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"
uid: 22076 topic_id: 19741 reply_id: 76595


[TOPIC: post.html]
#9

Carbondale

[GLOBAL: userInfoPane.html]
Carbondale
  • Observer

  • 26 posts
  • Corona SDK

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


[TOPIC: post.html]
#10

@RSCdev

[GLOBAL: userInfoPane.html]
@RSCdev
  • Corona Geek

  • 1,489 posts
  • Corona SDK

@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.
uid: 89165 topic_id: 19741 reply_id: 76632


[TOPIC: post.html]
#11

lemsim

[GLOBAL: userInfoPane.html]
lemsim
  • Contributor

  • 285 posts
  • Guests

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


[TOPIC: post.html]
#12

@RSCdev

[GLOBAL: userInfoPane.html]
@RSCdev
  • Corona Geek

  • 1,489 posts
  • Corona SDK

Hey @lemsim - I agree with all you said above! :)
Thanks,
Rodrigo.
uid: 89165 topic_id: 19741 reply_id: 76636


[TOPIC: post.html]
#13

lemsim

[GLOBAL: userInfoPane.html]
lemsim
  • Contributor

  • 285 posts
  • Guests

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


[TOPIC: post.html]
#14

@RSCdev

[GLOBAL: userInfoPane.html]
@RSCdev
  • Corona Geek

  • 1,489 posts
  • Corona SDK

@lemsim - Hahaha,

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

I totally agree again! :)
Best wishes mate,

Cheers,
Rodrigo.
uid: 89165 topic_id: 19741 reply_id: 76641


[TOPIC: post.html]
#15

srdjan@multimediaworx.com

[GLOBAL: userInfoPane.html]
srdjan@multimediaworx.com
  • Enthusiast

  • 89 posts
  • Corona SDK

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 ;-)
uid: 101952 topic_id: 19741 reply_id: 76672


[TOPIC: post.html]
#16

@RSCdev

[GLOBAL: userInfoPane.html]
@RSCdev
  • Corona Geek

  • 1,489 posts
  • Corona SDK

Ha, Ha,

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


[TOPIC: post.html]
#17

Carbondale

[GLOBAL: userInfoPane.html]
Carbondale
  • Observer

  • 26 posts
  • Corona SDK

I just bought the profiler today also.

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


[TOPIC: post.html]
#18

srdjan@multimediaworx.com

[GLOBAL: userInfoPane.html]
srdjan@multimediaworx.com
  • Enthusiast

  • 89 posts
  • Corona SDK

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 :-)

Cheers
uid: 101952 topic_id: 19741 reply_id: 78377


[TOPIC: post.html]
#19

M.Y.developers

[GLOBAL: userInfoPane.html]
M.Y.developers
  • Corona Geek

  • 1,517 posts
  • Corona SDK

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 :)

Thanks,
M.Y. Developers

uid: 55057 topic_id: 19741 reply_id: 85679


[TOPIC: post.html]
#20

Carbondale

[GLOBAL: userInfoPane.html]
Carbondale
  • Observer

  • 26 posts
  • Corona SDK

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 :)
uid: 22076 topic_id: 19741 reply_id: 85688


[TOPIC: post.html]
#21

M.Y.developers

[GLOBAL: userInfoPane.html]
M.Y.developers
  • Corona Geek

  • 1,517 posts
  • Corona SDK

@Carbondale

No problem! The simplest solution is often the best. Hope this saves you a lot of time and we appreciate your help.

Thanks,
M.Y. Developers
uid: 55057 topic_id: 19741 reply_id: 85692



[topic_controls]
[/topic_controls]