Passing information from one module to another

Hi Rob, and Brent

and everyone else.

Someone came to my business, and told me that he is a programmer, in Java.

He saw my code for my game, and he said that I was doing “bad programming”

I guess I already knew that. – but I don’t know how to do “good programming”

at least I’m working hard and studying everyday to get better.

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

So in myFunctions.lua you write many functions that you may need in all your app.

so when you are in page1.lua you just call the function that is in myFunctions.lua

and the game will work.

I guess when I’m in Page1.lua

on top of the file I have to requiere that file

local myFunction = requiere (“myFunctions”) – like you requiere GGData, or Widget

but I don’t really know how to do that.

Can you help me with that please?

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

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

?