Passing information from one module to another

What to do? Hmm… oh! We have Marry’s card. Lets use it. First we would like Marry to move objects despite it’s name so let me modify move function

[lua]
function M.move(objectToMove)
transition.to (objectToMove, {time= 2000, x = 300})
end
[/lua]

Now we will introduce Marry to Page1 country. At the top of Page1 we write
[lua]
local ourMerry = require (“utils”) – we must require country Merry lives in and we can refere to this person as we want so I called this person ourMarry
[/lua]

Next you create cat picture as earlier. And then we want ourMerry to move this painting. So we call
[lua]
ourMerry.move(cat) – and our painting moves, yey! :slight_smile:
[/lua]

Lets say you have a file called myfunctions.lua

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

Notice I did two things different than you had.  One I added a paramete r named object to the function call.  This is how you pass data like variables to the function.  Because you want this function to maybe move a dog, you don’t want to name the parameter cat.   In the transition.to, I tell it to move object because that’s this function’s reference to the object you want to move.

Now in your code:

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

The first line above, loads the myfunctions.lua file so that your .lua file can access it.  The variable on the left of the equals sign is a Table (or object if you prefer) that holds all the functions (and potentially variables) that you defined in your myfunction’s M table (M is shorthand for Module.  You could name it “george” if you wanted to… its just a name).

Then you create your cat as you normally would.  Then to move the cat, you want to call your M.move() function, but because we required it into “myfunctions” you have to now prefix the function with myfunctions instead of M.  Also notice I pass the variable cat in the function call.  Now move() knows what object you want to move.

piotrz55.

before I say something else…

THANKS!!!

Now it works!

Finally I got one function in one file, and I use that function in other file

that was like magic.

I feel like I’m learning magic, and you just put reveal a big secret.

Thanks Rob for the post of the M function that help me a lot.

and all the explanation. You are saying the same thing in a different way

and it works!

It doesn’t mean that I understand how it works.

I just know that now, that IT WORKS

I can go ahead and think for a few days , and really understand it

so I can use it for more advanced stuff.

I usually go to bed, and start thinking of all this functions in my head

until I fall sleep.

But with this Mary simple stuff, I got it to work!

Thank you, thank you a lot.

Victor

P.S. I’m really happy for this.

Now add to this, and I don’t recommend you program this way yet, I think you need to stay with standard functions while you’re learning this.

Lets say instead of having a table named myfunctions, you wanted to say:

cat:move()

In this case, I don’t pass cat as a parameter and I used a colon instead of a dot.  This is working in what is known as an “Object Oriented Programming” method.  Cat is an object and it has “methods” that know how to manipulate it.  

To do this, your myfunctions.lua changes to this:

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

Then where you define cat:

local myfunctions = require( "myfunctions" )   local cat = display.newImage("catImage.png") cat.x = 100 cat.y = 100 cat.move = myfunctions.move   cat:move()

It’s just a different way of thinking about it.  However you are not building your cat in an object oriented fashion and while it makes better sense to some, I think you need to have some more experience to a point of where you understand functions, parameter passing, return values and such before you tackle objects.

When you see a : operator, that’s an object oriented type function where there is an implied “self” variable passed to the function as it’s first parameter.  In other words:

myfunctions.move(cat)

cat:move()

in this case are the same.  This will probably confuse you more than help at this point, but when you see examples using :, use :, when you see a . use a dot.

Rob.

I tried both

this

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

and this

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

And you are right, I don’t really understand the difference.

but both of them worked the same

But for right now.

Which one do you think I should use?

The (object)

or the ( )

self

The dot version.

Now you see why I get all confused…

I have this in the utility.lua

local M = {} function M.move (obj)     transition.to(obj, {time=1000, delay=1000, x=1000, y=700}) end function M.glow (obj)     transition.to(obj, {time=100, xScale=1.5, yScale=1.5})     transition.to(obj, {time=100, delay=500, xScale=1, yScale=1}) end return M

I just added another function that I use in my game (notice that it’s the same as the cat test.

I’m passing an object (obj)

and in my page1 I have this

glowCoin = display.newImage ("glowCoin.png") glowCoin.x = display.contentWidth / 2 - 120 glowCoin.y = 40 glowCoin.xScale = 1.2 glowCoin.yScale = 1.2 --function glow () --transition.to(glowCoin, {time=100, xScale=1.5, yScale=1.5}) --transition.to(glowCoin, {time=100, delay=500, xScale=1, yScale=1}) --end utility.glow(glowCoin) local cat = display.newImage ("granja/ardilla.png") cat.x = 100 cat.y = 100 utility.move(cat)

In the comments is the function I “move” to utility.lua

To me it’s exactly the same, unless I made a mistake

When I run the code…

The .move works!

the .glow DOESN’T

Why?

--------------5 minutes later-------------

I notice that I have “local” in cat

and no “local” in glow


So I add “local” in glow to make it the same

I run the code…

Errors------

I have in another line before, some transition

if lamp.x == 87 then             transition.to(lamp, {time=100, x=87, y=455})             lamp:removeEventListener ("touch", lampTouch)             audio.play(win)             coinLampara.isVisible = true             transition.to(coinLampara, {time=1000, x=glowCoin.x, y=40, onComplete=glow})             addToScore(10)         end

Where I use the function “glow” onComplete.

So what do I do?

Do I just type this…

transition.to(coinLampara, {time=1000, x=glowCoin.x, y=40, onComplete=utility.glow(glowCoin)})

?

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?