Modular Coding......

Hi
Thanks Jonathan and his tutorials I’am trying to improve my coding skills…
All my ui buttons or particles in one file is great thing.
so what I am trying to do :

---- buttons.lua  
local button = {}  
local ui = require("ui")  
  
function newButton(xPos, yPos)  
  
 local doSomething = function(event)  
 if event.phase == "release" then  
 print("do do do")  
 end  
 end  
  
  
 local playB = ui.newButton{  
 defaultSrc = "playButton.png",  
 defaultX = 150,  
 defaultY = 52,  
 overSrc = "playButton.png",  
 overX = 160,  
 overY = 62,  
 onEvent = doSomething,  
 id = "PlayButton",  
 text = "",  
 font = "Helvetica",  
 textColor = { 255, 255, 255, 255 },  
 size = 16,  
 emboss = false  
 }   
  
 playB.x = xPos  
 playB.y = yPos  
end  
  
button.newButton = newButton  
return button  

and then in my levels

  
local buttons = require("buttons")  
local pauseButton = buttons.newButton(100,100)  

but why I can not add this to displayGroup ?
[import]uid: 13156 topic_id: 16326 reply_id: 316326[/import]

i think problem is in global function newButton, make it button.newButton for it to inherite all methods from button table and return it to use in another module

maybe i forgot something, try it [import]uid: 16142 topic_id: 16326 reply_id: 60809[/import]

because you are not returning anything in the function newButton()
but you are expecting a return value to arrive in pauseButton which will be nil as nothing is returned.

so to fix it,

between line 31 and 32, add

return playB

that’s all you need to do. However is the return object from ui library a display object? If not then you will still be unable to add it to the displayGroup.

cheers,

?:slight_smile: [import]uid: 3826 topic_id: 16326 reply_id: 60810[/import]

thank you very much.
no “return playB” it was my mistake. Now is working. :slight_smile:

I’am wondering using this method anyway is not quite good because how function doSomething will know what to do. I mean I’ll write do this and that , stop, this and that but this “things” take place in my “level files”.
For example:
I start timer ABC in level1 then put a button from my brand new buttons library and in doSomething I stop this timer ABC. It’s gonna work ?
[import]uid: 13156 topic_id: 16326 reply_id: 60816[/import]

How about passing a function to the button in your module? so when you create a new button, use

buttons.newButton(100,100, eventHandler)

this eventHandler function will be *here* in the function that will have access to all your objects.

To answer your question, YES, you have correctly identified that it is a very wrong way of programming. IN modular programming, you do not have functions that will be variable inside of the function.

let me explain with an example in real world

if you create an object, lets say Dog, it will have internal function that define things to do. So you can just call dog.play(ball) or dog.eat(bone) or dog.chase(Peter) or dog.chase(cat)

but you cannot have a function inside that is defined to chase Peter otherwise you have missed the whole idea of modularisation.

cheers,

?:slight_smile: [import]uid: 3826 topic_id: 16326 reply_id: 60818[/import]

yes you are perfectly right JayantV

I’am just trying to not use many MANY times the same objects in 20 levels.
Buttons anyway is not good example because play button and hmmm level button you are using only once but menu button I was using about 20 times in all levels and in each level this button was clearing and canceling exactly the same things.

By the way in level1 I created theTimer = timer.performWithDelay… and in button library in doSomething function I wrote timer.cancel(theTimer) and when I created a button in level1 after click it’s stop theTimer ??? :slight_smile: [import]uid: 13156 topic_id: 16326 reply_id: 60824[/import]

Sorry for all this questions but I really trying to understand this…
Let’s forget about doSomething function…
Can I have only buttons (no any other functions ) in buttons library and using it as you said with passing a function to the button : buttons.newButton(100,100, eventHandler) ? This working perfectly ok but I would like to know is this ok in general. Nice to have good ( no bad ) habits.
This will be ok to for particles with score time and stuff like this. [import]uid: 13156 topic_id: 16326 reply_id: 60859[/import]

Hi Jayant
Did you receive an email from me ? I sent it via your web site … [import]uid: 13156 topic_id: 16326 reply_id: 63995[/import]

I am sorry, I cannot recollect having received any email from you. What email ID/name did you use, are you PiotrT on your email id too? send me one to [dev] @ [oz-apps] . [com]

cheers,

?:slight_smile: [import]uid: 3826 topic_id: 16326 reply_id: 63998[/import]

I was sending via web site form. About Us
but no problem , I will send directly right now.
[import]uid: 13156 topic_id: 16326 reply_id: 64000[/import]