Using modules without module(..., package.seeall)

Hi folks

Like many noobs, I’ve been learning outdated and depricated tutorials, the latest of which was starting all my modules with module(…, package.seeall)

Then I came across this blog…

https://coronalabs.com/blog/2011/09/05/a-better-approach-to-external-modules/

I’ve now hit a brick wall. I’ve lost 2 days re-writing all my modules and code and re-re-writing, starting all over again and so and so forth. :wacko:

Let me point out that my code worked fine before I removed module(…, package.seeall)

Making my modules into local tables works fine until I try and call them more than once, here’s one of my short modules and the call made to it…

clock.lua

local clock = {}

clockGroup = display.newGroup()
clocks = {}

for i = 1, #loadClock.animClock do
   clocks[i] = display.newImageRect( clockGroup, loadClock.animClock[i], 160, 160)
   clocks[i].x = _W*0.92
   clocks[i].y = _H*0.82
   clocks[i].isVisible = false
end

clockGroup.currentClock = 1
clocks[clockGroup.currentClock].isVisible = true

clock.clocks = clocks

return clock

main.lua

local clock = require(“clock”)

The clock works fine the first time round but when I require the clock a second time it doesn’t reset.

I’ve tried package.loaded[“clock”] = nil and clock.remove = true

This happens with every module I call more than once, not just the above example, what am I missing?

Any help would be greatly appreciated. :slight_smile:

Yes, a module required once is not required twice since it is already on memory.

The _G.package.loaded[“clock”] = nil  should have worked to make your code reload the module. I recommend you double check your code.

Thanks for the reply Renato but no help I’m affraid.

My code has no errors, as I said before it works until I change the module from the depricated type to the table type.

The code loads images of a clock into a table so it can be animated by using .isVisible= false loading the next image and using isVisible=true

It works the first time but when I require (“clock”) again the clock doesn’t animate it just sits there, it’s not just the clock, it’s every module! It’s something to do with requiring the module more than once.

package.loaded[“clock”] = nil has no effect, I’ve reworked the code so many times I’m ready to give up all together.

I’ve read and read all the module documentation, all the blogs on modules but none of them tell you how to use the modules in this format, is the problem with re-requiring modules over and over? How do you use modules, do you only require them once?

Can anyone shed some light on this please, please!

Ah, I see. Your problem is that you have images displayed on the screen but that module. Try removing the images before setting package.loaded[“clock”]  = nil.

Also, instead of reloading the clock module, you could simply create reset function inside that module that would make the objects be in the original configuration. That is a better approach than using package.loaded[“clock”]  = nil and reloading the module

Thanks very much Renato that’s a very good idea about creating a reset function within the module.

After reading your original reply “Yes, a module required once is not required twice since it is already on memory” and in your second reply “That is a better approach than using package.loaded[“clock”]  = nil and reloading the module”

It made me think about modules differently. Yes you are right of coarse, silly me.

I now know where I went wrong and maybe this might help other newbie’s with the same problem.

I was still treating the re-worked modules the same as the depricated way by requiring them every time I wanted to load them but I now realise that I just use the returned value instead, in my case “clock” and reset it within my main code by re-assigning all the values in clock.clocks[i]

By reqiring the module over and over I was making copies of it, bad idea.

I should only be working with the returned table, in my case, clock{}

It feels so good to come out of the dark and into the light!

Thanks again Renato!

Hmm, i’ve been looking at the modules as well - trying to tidy up code to make it manageable.

Any chance of posting corrected code . For newbies the example of the code that caused the problem then the solved code would let them look down and follow the code and see exactly where the problem was.

T.

Hi T

Don’t forget I’m new to all this but I am quite happy to tell you how I now deal with modules.

Giving you an example of my particular code would surely confuse the hell out of newbies so I’m going to break it down into tiny chunks.

The old way was starting the module with the line module(…, package.seeall) but this made everything in it a global variable and we don’t want to do this for memory’s sake. Also that command is depricated.

Let’s say we want to put all our graphics into a module and call the module graphics.lua

The best way is to start the module with a local variable assigned to a table like this, local graphics = {}

and return it at the end of the module with the line, return graphics

All your vaiables within the module are local, for example:

local scoreCard = display.newImageRect(“images/scoreCard.png”, 400, 144)

local livesCard = display.newImageRect(“images/livesCard.png”, 400, 144)

Then insert the scoreCard and livesCard variables into the table like this:

graphics.scoreCard = scoreCard

graphics.livesCard = livesCard

Now for the part where I got stuck…

You only need to require that module once elsewhere, like at the top of your code for level1.lua, for example:

graphics = require (“graphics”)

Then when you want to manipulate each graphic, like making it appear on the screen, use:

graphics.scoreCard.isVisible = true

or move it:

transition.moveTo( graphics.scoreCard, { x=_W*0.36, y=_H*0.73, time=500 } )

It’s important to remember that you only require the module once, if you require it a second time things get messed up!

The idea is to only use the variable (which is actually a table) that required the module from now on.

Also notice how I named the module graphics ( graphics.lua ) and the table variable graphics ( local graphics = { } ) and the variable to require the module graphics (graphics = require (“graphics”))

This keeps things really simple and trust me, when you are new to Corona like me keeping things simple is very important.

Don’t get carried away with making modules for everything!

For a start you don’t want to break up your main code and put everything in modules, just parts of the code that will be used over and over by other levels in your game or very large functions that make scrolling through your code a pain in the butt.

I made a module for loading all my graphics for level1, a module to keep all my score data in and another for an animated clock I created which uses 60 different images.

I hope my very simplified explanation helps somebody, and please, any pro Corona programmers that spot something wrong with this post I would welcome some constructed feedback, but please keep your replies simple for us newbies!

It’s good practice to localize required module which can speed app and does n’t polute global variables.

[lua]
local graphics = require (“graphics”)
[\lua]

If you do not use ‘local’ then creating module gives you nothing at all! It’s because you once again make it global as module (…) does!

You can include module in each file you use it without problem. The trick is to know that with first occurance of require() of this file it is executed, so if you have some naked display.new… then objects will be created. This happens only once and then it won’t happen again despite how many in other files you wil include it.

@QuizMaster,  first of all thank you for that basic modules tutorial, it’s given me the push to learn more about how to use them as I’ve avoided them up until now.

Secondly, you mention that you animate your clock by showing/hiding lots of different images, so I’ve taken my new found knowledge of modules to see if I could write a clock module, and this is what I’ve come up with…

clock.lua

local clock={} function clock:new() clock.face=display.newCircle(display.contentCenterX, display.contentCenterY, 100) clock.face:setFillColor(.3,0,0) clock.face:setStrokeColor(1,1,1) clock.face.strokeWidth=2 clock.hourHand=display.newRect(display.contentCenterX, display.contentCenterY,70,5) clock.hourHand.anchorX=.15 clock.hourHand.rotation=-90 clock.minuteHand=display.newRect(display.contentCenterX, display.contentCenterY,100,5) clock.minuteHand.anchorX=.15 clock.minuteHand.rotation=-90 clock.secondHand=display.newRect(display.contentCenterX, display.contentCenterY,110,3) clock.secondHand:setFillColor(1,1,0) clock.secondHand.anchorX=.2 clock.pivot=display.newCircle(display.contentCenterX, display.contentCenterY, 4) clock.pivot:setFillColor(0,0,0) clock.secondHand.rotation=-90 clock.markTime=0 end function clock.enterFrame(self, event) local timePassed = system.getTimer() - clock.markTime if timePassed \>= 1000 then -- 1000 = 1 second clock.secondHand.rotation = clock.secondHand.rotation + 6 if clock.secondHand.rotation == 360 then clock.secondHand.rotation = 0 end if clock.secondHand.rotation == 270 then clock.minuteHand.rotation = clock.minuteHand.rotation + 6 clock.hourHand.rotation = clock.hourHand.rotation + .5 end if clock.minuteHand.rotation == 360 then clock.minuteHand.rotation = 0 end if clock.hourHand.rotation == 360 then clock.hourHand.rotation = 0 end clock.markTime = system.getTimer() end end function clock:start() clock.markTime = system.getTimer() -- Get the system time Runtime:addEventListener("enterFrame", self ) end return clock

main.lua

local clock=require("clock") -- load the clock module clock:new() -- draw the clock clock:start() -- start the clock

Yes, a module required once is not required twice since it is already on memory.

The _G.package.loaded[“clock”] = nil  should have worked to make your code reload the module. I recommend you double check your code.

Thanks for the reply Renato but no help I’m affraid.

My code has no errors, as I said before it works until I change the module from the depricated type to the table type.

The code loads images of a clock into a table so it can be animated by using .isVisible= false loading the next image and using isVisible=true

It works the first time but when I require (“clock”) again the clock doesn’t animate it just sits there, it’s not just the clock, it’s every module! It’s something to do with requiring the module more than once.

package.loaded[“clock”] = nil has no effect, I’ve reworked the code so many times I’m ready to give up all together.

I’ve read and read all the module documentation, all the blogs on modules but none of them tell you how to use the modules in this format, is the problem with re-requiring modules over and over? How do you use modules, do you only require them once?

Can anyone shed some light on this please, please!

Ah, I see. Your problem is that you have images displayed on the screen but that module. Try removing the images before setting package.loaded[“clock”]  = nil.

Also, instead of reloading the clock module, you could simply create reset function inside that module that would make the objects be in the original configuration. That is a better approach than using package.loaded[“clock”]  = nil and reloading the module

Thanks very much Renato that’s a very good idea about creating a reset function within the module.

After reading your original reply “Yes, a module required once is not required twice since it is already on memory” and in your second reply “That is a better approach than using package.loaded[“clock”]  = nil and reloading the module”

It made me think about modules differently. Yes you are right of coarse, silly me.

I now know where I went wrong and maybe this might help other newbie’s with the same problem.

I was still treating the re-worked modules the same as the depricated way by requiring them every time I wanted to load them but I now realise that I just use the returned value instead, in my case “clock” and reset it within my main code by re-assigning all the values in clock.clocks[i]

By reqiring the module over and over I was making copies of it, bad idea.

I should only be working with the returned table, in my case, clock{}

It feels so good to come out of the dark and into the light!

Thanks again Renato!

Hmm, i’ve been looking at the modules as well - trying to tidy up code to make it manageable.

Any chance of posting corrected code . For newbies the example of the code that caused the problem then the solved code would let them look down and follow the code and see exactly where the problem was.

T.

Hi T

Don’t forget I’m new to all this but I am quite happy to tell you how I now deal with modules.

Giving you an example of my particular code would surely confuse the hell out of newbies so I’m going to break it down into tiny chunks.

The old way was starting the module with the line module(…, package.seeall) but this made everything in it a global variable and we don’t want to do this for memory’s sake. Also that command is depricated.

Let’s say we want to put all our graphics into a module and call the module graphics.lua

The best way is to start the module with a local variable assigned to a table like this, local graphics = {}

and return it at the end of the module with the line, return graphics

All your vaiables within the module are local, for example:

local scoreCard = display.newImageRect(“images/scoreCard.png”, 400, 144)

local livesCard = display.newImageRect(“images/livesCard.png”, 400, 144)

Then insert the scoreCard and livesCard variables into the table like this:

graphics.scoreCard = scoreCard

graphics.livesCard = livesCard

Now for the part where I got stuck…

You only need to require that module once elsewhere, like at the top of your code for level1.lua, for example:

graphics = require (“graphics”)

Then when you want to manipulate each graphic, like making it appear on the screen, use:

graphics.scoreCard.isVisible = true

or move it:

transition.moveTo( graphics.scoreCard, { x=_W*0.36, y=_H*0.73, time=500 } )

It’s important to remember that you only require the module once, if you require it a second time things get messed up!

The idea is to only use the variable (which is actually a table) that required the module from now on.

Also notice how I named the module graphics ( graphics.lua ) and the table variable graphics ( local graphics = { } ) and the variable to require the module graphics (graphics = require (“graphics”))

This keeps things really simple and trust me, when you are new to Corona like me keeping things simple is very important.

Don’t get carried away with making modules for everything!

For a start you don’t want to break up your main code and put everything in modules, just parts of the code that will be used over and over by other levels in your game or very large functions that make scrolling through your code a pain in the butt.

I made a module for loading all my graphics for level1, a module to keep all my score data in and another for an animated clock I created which uses 60 different images.

I hope my very simplified explanation helps somebody, and please, any pro Corona programmers that spot something wrong with this post I would welcome some constructed feedback, but please keep your replies simple for us newbies!

It’s good practice to localize required module which can speed app and does n’t polute global variables.

[lua]
local graphics = require (“graphics”)
[\lua]

If you do not use ‘local’ then creating module gives you nothing at all! It’s because you once again make it global as module (…) does!

You can include module in each file you use it without problem. The trick is to know that with first occurance of require() of this file it is executed, so if you have some naked display.new… then objects will be created. This happens only once and then it won’t happen again despite how many in other files you wil include it.

@QuizMaster,  first of all thank you for that basic modules tutorial, it’s given me the push to learn more about how to use them as I’ve avoided them up until now.

Secondly, you mention that you animate your clock by showing/hiding lots of different images, so I’ve taken my new found knowledge of modules to see if I could write a clock module, and this is what I’ve come up with…

clock.lua

local clock={} function clock:new() clock.face=display.newCircle(display.contentCenterX, display.contentCenterY, 100) clock.face:setFillColor(.3,0,0) clock.face:setStrokeColor(1,1,1) clock.face.strokeWidth=2 clock.hourHand=display.newRect(display.contentCenterX, display.contentCenterY,70,5) clock.hourHand.anchorX=.15 clock.hourHand.rotation=-90 clock.minuteHand=display.newRect(display.contentCenterX, display.contentCenterY,100,5) clock.minuteHand.anchorX=.15 clock.minuteHand.rotation=-90 clock.secondHand=display.newRect(display.contentCenterX, display.contentCenterY,110,3) clock.secondHand:setFillColor(1,1,0) clock.secondHand.anchorX=.2 clock.pivot=display.newCircle(display.contentCenterX, display.contentCenterY, 4) clock.pivot:setFillColor(0,0,0) clock.secondHand.rotation=-90 clock.markTime=0 end function clock.enterFrame(self, event) local timePassed = system.getTimer() - clock.markTime if timePassed \>= 1000 then -- 1000 = 1 second clock.secondHand.rotation = clock.secondHand.rotation + 6 if clock.secondHand.rotation == 360 then clock.secondHand.rotation = 0 end if clock.secondHand.rotation == 270 then clock.minuteHand.rotation = clock.minuteHand.rotation + 6 clock.hourHand.rotation = clock.hourHand.rotation + .5 end if clock.minuteHand.rotation == 360 then clock.minuteHand.rotation = 0 end if clock.hourHand.rotation == 360 then clock.hourHand.rotation = 0 end clock.markTime = system.getTimer() end end function clock:start() clock.markTime = system.getTimer() -- Get the system time Runtime:addEventListener("enterFrame", self ) end return clock

main.lua

local clock=require("clock") -- load the clock module clock:new() -- draw the clock clock:start() -- start the clock