Passing information from one module to another

If Merry story is to your liking than there’s more:

SCOPES:
When you write application, think of it as the globe inside which there is everything. As for files, think of it as independent countries with no connection to each other and having no means of communication on default (but you can use bussiness card as in Merry example)

When you write ‘local Andy’ in file, you create full right citizen in country but only country knows about him. If you omit ‘local’ keyword this is as you would shout to the whole world ‘this is Andy!’ and now whole glob knows there is something called Andy in the world - it’s called as global variable as oposed to local one created with ‘local’. When in your country you would call for Andy, first country will be searched if there is such citizen - he have priority over everything else called Andy. It is local scope of file. If no Andy is found, application will try to check if there is something called Andy know on the whole world. And there will be global Andy (but only if you omited ‘local’ earlier). BuT it’s very insufficient to use globals because each time we must serch whole world to find it. This is the reason why we prefere to create ourMerry using card because we will know exact location of real Merry instead searching it each time

If you have function or conditional code in file, think of it as region of country. If inside it you have another local Andy, then this regional Andy is more important then that Andy in the country and even more important then global Andy. When you call for Andy in function then first this region will be searched for him. If none then country and at last globe will be searched. However, if some Andy is local inside region, as wr leave region then this regional Andy is no longer know in country until we enter region again. So if you have something with local inside function, then this something is only known in region but not in country

In programing it is constant flow scheme: region knows about country and globe, country knows only globe and globe knows only itself. But other way is not possible. As country doesn’t know about region and globe doesn’t know about country

About colon, don’t use it yourself for now unless there is such need in corona api and documentation states it.

So what is colon used for. It is used for use of concept called Object Oriented Programming. But what it really is? [DON’T read what next if it can confuse you] For example, you have glow function in your utils.lua. Each time you must pass object to this function to make it glow, right? So programmers thought - why we cannot program as situations happens in real world, it will more intuitive (but we are talking about computer so it does’t must be easier with code to mimic real world but we will try)’ In real world you say person to eat, there is no concept as giving person to ‘eat’ and it will make him do it (in programing yes but we are trying to mimic real world).
So programmers created some means to achieve it (main representative concept is called ‘class’).
Why it is different? Instead passing person to something called ‘eat’, using OOP we can just tell person to eat directly. It’s called object oriented because we focuse on person object not functions/actions

Thank you piotrz55. I’m starting to understand a little bit. But I still don’t get, why in the example above, The .move works! the .glow DOESN’T why?

Always show us your errors :slight_smile:

I don’t understand the word “showbus”

I don’t know what you mean.

sorry…

Corrected, read again (writting generaly from phone)

It has so many things, that I need to be very patience.

I guess I had to re-start the app.

I never did that before.

But now with the same code it’s working, when I call the function on complete I have this

onComplete=utility.glow(glowCoin)})

and it works!!!


Now that it’s to move 1 object – so I pass the object like this

–function M.glow (obj)

so inside the parenthesis I pass an “obj”

but what about if I have many objects to move or to be affected inside the function?

Like I have this function, it’s big and large.

If I can have this function in the utility.lua

and just change the objects, I think it will be wonderful.

local function ballTouch( event )     if "began" == event.phase then         audio.play (peau)         ball.isFocus = true         txPe.isVisible = true         local function resetPelota ()             txPe.isVisible = false             txPe.y = 730         end         transition.to(txPe, {time=2000, y=680, onComplete=resetPelota})                  txBall.isVisible = true         local function resetBall ()             txBall.isVisible = false             txBall.y = txPe.y + 40         end         transition.to(txBall, {time=2000, y=(680 + 40), onComplete=resetBall})                  ball.x0 = event.x - ball.x         ball.y0 = event.y - ball.y     elseif ball.isFocus then         if "moved" == event.phase then             ball.x = event.x - ball.x0             ball.y = event.y - ball.y0                      if ball.x == 226 then             transition.to(ball, {time=100, x=226, y=665})             ball:removeEventListener ("touch", ballTouch)             audio.play(win)             coinPelota.isVisible = true             transition.to(coinPelota, {time=1000, x=glowCoin.x, y=40, onComplete=utility.glow(glowCoin)})             addToScore(10)         end                  elseif "ended" == phase or "cancelled" == phase then             ball.isFocus = false         end     end     -- Return true if the touch event has been handled.     return true end

As you can see, I have

– a ball object

– a txPe

– txBall

– coinPelota

and also functions inside functions…

And for my game I need like 12 functions like this, and that is just in one page

and I need like 10 pages…

So I have to write the same function

local function ballTouch( event )   end

like 150 times

all I have to change it’s that instead of “ball” now it will be “bed” or “toy”

is there a way to make this work?

In other thread we have written you how to do it, eg. globaldata.lua

He told me that there is a way that you can have, many functions in one module
or file, like – myFunctions.lua, so when you need a function you just call that function
instead of writing the same function, over and over again in every file.

So instead of having 1459 lines of code
you can have 478 lines of code – something like that

It’s basic and essential programming knowledge you are lacking (general knowledge not lua/corona specific)

I suggest you to leave Corona for a while and learn first about generic programming concepts and lua specification :slight_smile:

Victor, I would not ever want to call someone a bad programmer, unless of course they had 30 years of experience and was still making mistakes that a new programmer makes.  Keep in mind you’ve only been doing this for a couple of months and it takes time to gain the experience and education to avoid making some of the common pitfall’s that you’re running into.  Don’t be dis-heartened.  Keep at it.  Heck, you got an app up in the app store in a couple of months coming from ZERO experience.  That’s a pretty big accomplishment and I for one am pretty proud of you, knowing that you’re getting a somewhat late start into programming. 

That said, I’m sure there are plenty of ways that your code can improve.  Heck, I’ve got 30+ years of programming experience and my first Corona project would be called “bad” by this guy.  I knew it was bad when I was writing it, but I hadn’t learned enough about Lua yet to avoid things.  It takes time and patience to get good at this stuff.

Now that said, yes, you can easily have a separate Lua file with your functions in it.  I have one I call “utility.lua”.  It has all of my random functions that I need.  In some cases, I extend existing Lua objects like string and table by adding my own string and table functions.  Other times the functions remain part of the “utility” object.  Let’s look at an example.  For brevity, I’m going to remove all the code and only leave the function definitions.

  local M = {} function M.print\_r ( t ) -- table printer end M.printTable = M.print\_r  -- Alias it function M.testNetworkConnection() end function M.saveTable(t, filename) end function M.loadTable(filename) end function M.urlencode(str) end function M.makeTimeStamp(dateString) end function string:trim() end   function string:split( inSplitPattern, outResults ) end return M  

Then in each Lua module where I want these functions, I do:

local utility = require(“utility”)

Then to use the functions:

utility.print_r(someTable)

for instance.  In the case of string trim and split, these are added to the string object.  I use the : instead of the . because I want them to accept the string as self parameter if they are called in an object oriented fashion.

@Rob, true words :slight_smile:

However, I see that he needs more time to learn lua itself (and more advanced programing) more, because even with presented code, dot and colon concepts still seem for him too complex. Hope he overcome it :slight_smile:

dot and  colon concepts took me time to learn.  He will figure it out eventually.

I have 20+ years under my belt with ActionScript and PHP.  I’ve built successful internal applications for fortune 500 companies in my lifetime.  I am not classically trained in programming, but regardless, I have been put down by more “knowledgeable” programmers many times.  And while it doesn’t feel particularly nice, I did pay heed, and became dedicated to fine tune my knowledge.

Over time I have learned coding is quite like art.  There are many ways to approach the same thing.  And many coders will indeed clash over mundane techniques, that the end user could care less about.  Of course there are certain “best practices” but at the end of the day if your code works for what you need it to do – without diminishing your users experience – then it’s just as good as the best coder in the world.  But it’s very important that you continually seek out knowledge and experiment at every opportunity, as insignificant as it may seem at the time.  It’s not called computer science for nothing.

@piotrz55 - It’s important to help educate, so if you have links you can share, please do so.  We all started at the beginning. :slight_smile:

Best of luck!

No links sorry :P, just a few years at school as extra subject and self learning of programming and lua picked recently for Corona especially :slight_smile:

Hi guys! I was away for a few days.

–Rob, thanks for your words. I guess you more than anyone else knows that I did really start from zero, a big zero.

I’m doing my best, but I need a lot more.

–Piotrz55, I can not just go on and study programming. There is no place I can go and say: teach me how to program.

Right now I wish I could find a teacher, or a school that I con go to. Remember I’m 52. so college takes a program for C+ or so for 4 years, but at my age, no collage for me.

I am a music teacher, for more that 30 years. All I know it’s that when I teach some child the G scale or the Cm7 Chord in a progression, or how to play a diminished seventh, if they don’t get it, I say the same thing in a different way. If still they don’t get it, I say it in another way, or another, for a week or a month or a year, until they get it.

So when you say --“in other thread we have written you how to do it, eg. globaldata.lua”–

I know, and still I don’t get it.

About the colon : or the . dot that Rob mention. I have no idea what you are talking about. But this post will be the day I can understand that concept. All I can tell you… I will learn for sure.

I think I have enough intelligence, and desire, to learn. If you guys are patience enough to tell me things in many ways, until I get it.

And yes, writing code, or making an app, it is an art form. a new art in this century.

The art that I learn 30 years ago it’s called “Music”

sadly for me, that art, it’s dying, little by little.

Now let’s keep learning.

I made a file – utility.lua – it’s in the same folder as main.lua

inside that file I did this:

local M = {} function M.move ( )     transition.to(cat, {time=100, x=1000, y=700}) end return M

That’s it. – This is what I don’t get.
 
To me, “cat” it’s a variable name.

In another file – page1 – usually I write something like this

local cat = display.newImage("catImage.png") cat.x = 100 cat.y = 100

So if I want to move the cat from 100 to 1000

what do I do? –

Do I just time

M.move (  )

I understand you. I didn’t mean it as ‘go study programming’ because I now well that each one have own circumstances (I also myself don’t study programming).
What I ment, was that there are some basic blocks you must learn, otherwhise it’s painful experience where you think 'what are they talking about?!'all the time.

About learnig itself, nowadays thanks to internet, there is no need for full courses to learn, examples and tutorials are e everywhere. And also community willing to help as on this forum :slight_smile:

Thought I’d chime in to make a quick suggestion –
One of the things that really helped me grasp basic/universal programming concepts, early on, was Stanford’s ‘Programming Methodology’ course. I *Highly recommend watching it, and also downloading the tools so you can do the projects.
You’ll see what the first project is if you watch it, but when I got Karel to setup a checker board and play a game of checkers - with himself :stuck_out_tongue: - I was SO excited haha :slight_smile:
https://itunes.apple.com/us/itunes-u/programming-methodology/id384232896?mt=10

While the first video has a lot to do with the class structure etc… he does go over a few key programming points, so while you might be tempted to skip the first video I’d suggest watching it. :slight_smile:

-Saer

Let’s say utility is your country called Util. In this country lives Merry (our M). Merry can perform ‘move’ action/function. When Merry performs ‘move’, she takes something called ‘cat’ (she doesn’t know what it is but she knows it’s called cat) and changes it’s position. However, there is nothing called ‘cat’ in Util - when we ask her to move she will protest because there will be nothing to move. What’s more, at the end we have "return M’- let’s call it Merry’s business card.

In country Page1, we create painting and we called it ‘cat’ and we changed it position. Let’s see… indeed we have cat in Page1 and action using ‘cat’, but hey, this countries are apart and they know nothing about themselves