Storyboard and modules

Hello,

Here is a stupid? question:

I’ve got 4 files: mail.lua, menu.lua, scene.lua and the_module.lua

In the the_module.lua one I display an image and I want to “unload” that image when I change scene.

Here is the code:

main.lua

widget = require( "widget" )  
storyboard = require "storyboard"  
  
\_W,\_H = display.contentWidth,display.contentHeight  
  
storyboard.gotoScene( "menu" )  

menu.lua

local menuScene = storyboard.newScene()  
  
function menuScene:createScene( event )  
 local group = self.view  
  
 local myText = display.newText("Welcome to the menu scene!",0,0,native.systemFont,26)  
 myText.x = \_W/2  
 myText.y = 100  
  
 local function playF()  
 storyboard.gotoScene( "scene" )  
 end  
  
 local playBtn = widget.newButton{  
 id = "play",  
 left = 0,  
 top = 0,  
 width = 150, height = 100,  
 label = "Play",  
 onRelease = playF,  
 }  
 playBtn.x = \_W/2  
 playBtn.y = \_H/2  
  
 group:insert(myText)  
 group:insert(playBtn)  
end  
  
function menuScene:enterScene( event )  
 local group = self.view  
  
end  
  
function menuScene:exitScene( event )  
 local group = self.view  
  
end  
function menuScene:destroyScene( event )  
 local group = self.view  
  
end  
  
menuScene:addEventListener( "createScene", menuScene )  
menuScene:addEventListener( "enterScene", menuScene )  
menuScene:addEventListener( "exitScene", menuScene )  
menuScene:addEventListener( "destroyScene", menuScene )  
  
return menuScene  
  

scene.lua

local theModule  
  
local myScene = storyboard.newScene()  
  
function myScene:createScene( event )  
 local group = self.view  
  
 theModule = require "the\_module"  
  
 local bg = display.newRect(0,0,\_W,\_H)  
 bg:setFillColor(255,255,255)  
  
 local myText = display.newText("Welcome to my scene!",0,0,native.systemFont,26)  
 myText:setTextColor(0,0,0)  
 myText.x = \_W/2  
 myText.y = 100  
  
 local function backF()  
 storyboard.gotoScene( "menu" )  
 end  
  
 local backBtn = widget.newButton{  
 id = "back",  
 left = 0,  
 top = 0,  
 width = 150, height = 100,  
 label = "Go back",  
 onRelease = backF,  
 }  
 backBtn.x = \_W/2  
 backBtn.y = \_H/2  
 group:insert(bg)  
 group:insert(myText)  
 group:insert(backBtn)  
end  
  
function myScene:enterScene( event )  
 local group = self.view  
  
end  
function myScene:exitScene( event )  
 local group = self.view  
  
 theModule.objGroup:removeSelf()  
 theModule.objGroup=nil  
end  
  
function myScene:destroyScene( event )  
 local group = self.view  
  
end  
myScene:addEventListener( "createScene", myScene )  
myScene:addEventListener( "enterScene", myScene )  
myScene:addEventListener( "exitScene", myScene )  
myScene:addEventListener( "destroyScene", myScene )  
  
return myScene  
  

And, at last, the_modle.lua

local public = {}   
  
local objGroup = display.newGroup()  
  
local img = display.newImageRect( "ilustracion\_contadorpuntos\_x\_normal.png", 195, 93 )  
img.x = \_W/2  
img.y = \_H/2 - 100  
  
objGroup:insert(img)  
  
public.objGroup = objGroup  
return public  

Anybody knows what am I doing wrong?
How can I “unload” the img?

Thanks in advance! [import]uid: 44013 topic_id: 31605 reply_id: 331605[/import]

Anybody can help me, please? [import]uid: 44013 topic_id: 31605 reply_id: 126491[/import]

I don’t quite understand what you are trying to do. [import]uid: 26491 topic_id: 31605 reply_id: 126627[/import]

Anybody can help me, please? [import]uid: 44013 topic_id: 31605 reply_id: 126491[/import]

Hi Joshua!

It’s a simplifyed example. I’ve got a menu scene and when I choose an option in the corresponding scene I call an external module. When I return to the menu scene, I want to unload what the external module has loaded, but in this code the objects loaded by the external module remains in screen.

How can I do to unload the img? [import]uid: 44013 topic_id: 31605 reply_id: 126769[/import]

@alberto1

Is there more code than what you’re showing in the file ‘the_module.lua’?
For this answer, I will assume the answer is yes and that there are functions, etc.
I suggest not using modules at all. Instead, wrap all your variables and functions in a table and return that.

That is do this instead:
the_module.lua

local public = {} -- Call it whatever you want  
 public.img = display.newImageRect( "ilustracion\_contadorpuntos\_x\_normal.png", 195, 93 )  
 public.img.x = \_W/2  
 public.img.y = \_H/2 - 100  
return public  

Now, to use this simply do the following in scene.lua :

local theModule -- Just make a local  
local myScene = storyboard.newScene()  
   
function myScene:createScene( event )  
 theModule = require "the\_module"  
  
 ... Your other code ..  
  
end  
  
...   
  
function myScene:destroyScene( event )  
 theModule = nil -- This effectively unloads your module and the image, but there is a better way... see my next snippet   
end  
   
-- ...  

The above code will unload the module, and the image (if I remember the rules correctly), but I would personally also manually call removeSelf() on the image. A nice way to do this is to be a little more sophisticated in the way you set up the code in the_module.lua.

Try the following instead:

the_module.lua [code]local public = {} – Call it whatever you want
function public:init()
self.img = display.newImageRect( “ilustracion_contadorpuntos_x_normal.png”, 195, 93 )
self.img.x = _W/2
self.img.y = _H/2 - 100
end

function public:cleanup()
self.img:removeSelf()
self.img = nil
end

return public
[/code]

Now, to ‘unload this’ simply do the following in scene.lua :[code]
local theModule – Just make a local
local myScene = storyboard.newScene()

function myScene:createScene( event )
theModule = require “the_module”
theModule:init()

… Your other code …

end

function myScene:destroyScene( event )
theModule:cleanup()
theModule = nil – This effectively unloads your module and the image, but there is a better way… see my next snippet
end

– …
[/code]

Please let me know if this works for you and if I have been clear or unclear.

Also, if you are interested, I am in the midst of releasing a project I’ve been working on called “SSK Corona”, that is “Super Starter Kit for Corona”. You can download it here: https://github.com/roaminggamer/SSKCorona It is still very much in beta, but includes a sampler kit/app where I do something you might find interesting. Specifically, my sampler has a single ‘scene’ that is re-used over and over to load samples. Look under the sampler directory at main.lua, generic_scene.lua, and samplerMgr.lua for clarification. Also, you should look at one of the templates for an outline of how I do the loading/unloading (sampler/ssk_sampler/_templates/*).

Cheers,

Edo out!

[import]uid: 110228 topic_id: 31605 reply_id: 126792[/import]

Hi @emaurina!

Thanks a lot for your reply!
I’m going to try what you coded and I’ll tell you about.
In this example, the_module.lua is all you can see here (no more functions neither variables).

I’m going to take a look at SSK Corona too, it seems interesting…

Cheers!

Alberto. [import]uid: 44013 topic_id: 31605 reply_id: 126795[/import]

I don’t quite understand what you are trying to do. [import]uid: 26491 topic_id: 31605 reply_id: 126627[/import]

Ok, I tried your code and works if I add the display objects in the_module.lua to the group in scene.lua
In the_module I want to add more display objects, so I figured out I must to insert each one in the group in scene.lua

I wonder: is there a better way? [import]uid: 44013 topic_id: 31605 reply_id: 126872[/import]

@alberto

Nope.

I’m afraid you’ll either have to insert all your display objects into a group(s) and then remove the group(s) to remove all the display objects simultaneously,

OR

You’ll have to call removeSelf() on all of the objects you want to remove. (Don’t forget to set any references to the object to nil also. Remember that LUA won’t garbage collect an object if there is still a reference to it.)

Sorry for the much delayed response, but I got a bit involved answering another forum thread: http://developer.coronalabs.com/forum/2012/10/08/calculating-intersecting-lines

I added another sample to SSK: http://www.youtube.com/watch?v=u2iZhklBzBc
-edo out! [import]uid: 110228 topic_id: 31605 reply_id: 126942[/import]

Hi Joshua!

It’s a simplifyed example. I’ve got a menu scene and when I choose an option in the corresponding scene I call an external module. When I return to the menu scene, I want to unload what the external module has loaded, but in this code the objects loaded by the external module remains in screen.

How can I do to unload the img? [import]uid: 44013 topic_id: 31605 reply_id: 126769[/import]

@alberto1

Is there more code than what you’re showing in the file ‘the_module.lua’?
For this answer, I will assume the answer is yes and that there are functions, etc.
I suggest not using modules at all. Instead, wrap all your variables and functions in a table and return that.

That is do this instead:
the_module.lua

local public = {} -- Call it whatever you want  
 public.img = display.newImageRect( "ilustracion\_contadorpuntos\_x\_normal.png", 195, 93 )  
 public.img.x = \_W/2  
 public.img.y = \_H/2 - 100  
return public  

Now, to use this simply do the following in scene.lua :

local theModule -- Just make a local  
local myScene = storyboard.newScene()  
   
function myScene:createScene( event )  
 theModule = require "the\_module"  
  
 ... Your other code ..  
  
end  
  
...   
  
function myScene:destroyScene( event )  
 theModule = nil -- This effectively unloads your module and the image, but there is a better way... see my next snippet   
end  
   
-- ...  

The above code will unload the module, and the image (if I remember the rules correctly), but I would personally also manually call removeSelf() on the image. A nice way to do this is to be a little more sophisticated in the way you set up the code in the_module.lua.

Try the following instead:

the_module.lua [code]local public = {} – Call it whatever you want
function public:init()
self.img = display.newImageRect( “ilustracion_contadorpuntos_x_normal.png”, 195, 93 )
self.img.x = _W/2
self.img.y = _H/2 - 100
end

function public:cleanup()
self.img:removeSelf()
self.img = nil
end

return public
[/code]

Now, to ‘unload this’ simply do the following in scene.lua :[code]
local theModule – Just make a local
local myScene = storyboard.newScene()

function myScene:createScene( event )
theModule = require “the_module”
theModule:init()

… Your other code …

end

function myScene:destroyScene( event )
theModule:cleanup()
theModule = nil – This effectively unloads your module and the image, but there is a better way… see my next snippet
end

– …
[/code]

Please let me know if this works for you and if I have been clear or unclear.

Also, if you are interested, I am in the midst of releasing a project I’ve been working on called “SSK Corona”, that is “Super Starter Kit for Corona”. You can download it here: https://github.com/roaminggamer/SSKCorona It is still very much in beta, but includes a sampler kit/app where I do something you might find interesting. Specifically, my sampler has a single ‘scene’ that is re-used over and over to load samples. Look under the sampler directory at main.lua, generic_scene.lua, and samplerMgr.lua for clarification. Also, you should look at one of the templates for an outline of how I do the loading/unloading (sampler/ssk_sampler/_templates/*).

Cheers,

Edo out!

[import]uid: 110228 topic_id: 31605 reply_id: 126792[/import]

Thanks a lot @emaurina!
I’ll try creating a group containing all the display objects and removing the group when I go to other scene.

Cheers!
[import]uid: 44013 topic_id: 31605 reply_id: 126981[/import]

Hi @emaurina!

Thanks a lot for your reply!
I’m going to try what you coded and I’ll tell you about.
In this example, the_module.lua is all you can see here (no more functions neither variables).

I’m going to take a look at SSK Corona too, it seems interesting…

Cheers!

Alberto. [import]uid: 44013 topic_id: 31605 reply_id: 126795[/import]

Hi!

I edited the scene.lua and the the_module.lua files to work like @emaurina suggests.
However, when I’ve returned for the first time to the menu and I try to enter again in the scene, this example craches.
I figured out when I call the scene.lua file (and this one calls the_module.lua file), the objects was created again, but seems it isn’t so.

What am I doing wrong? [import]uid: 44013 topic_id: 31605 reply_id: 126984[/import]

@alberto1,

Hi. One more reply… I just wanted to mention that in SSK I provide a utility for creating a set of groups that act as layers:

local layers -- Local reference to display layers   
...  
  
layers = ssk.proto.quickLayers( screenGroup,   
 "background",  
 "skylayer",   
 "scrollers",   
 { "scroll3", "scroll2", "scroll1" },  
 "content",  
 "interfaces" )  

The above call produces a set of groups, bottom to top, like this: \ (screenGroup) |--\ background |--\ skylayer |--\ scrollers |--\ scroll3 |--\ scroll2 |--\ scroll1 |--\ content |--\ interfaces
Later, I use the layers like this: display.newRect( layers.content, 0, 0, width, height ) display.newRect( layers.content, 100, 120, width, height ) ...

Finally, when I want to clean up I do this:

layers:destroy()  

This may not be the best description, but I am still working on docs and tutorials.

Cheers,
Ed
Roaming Gamer, LLC.
SSK for Corona SDK (github) (videos)

PS - Why Ed and not edo? Edo is Ed in Japanese, and Yes, there is more history to why I use that signature, but I think I should be consistent. If people look me up on my site or elsewhere, edo would just be confusing. So, back to Ed. :slight_smile: [import]uid: 110228 topic_id: 31605 reply_id: 127011[/import]

Ok, I tried your code and works if I add the display objects in the_module.lua to the group in scene.lua
In the_module I want to add more display objects, so I figured out I must to insert each one in the group in scene.lua

I wonder: is there a better way? [import]uid: 44013 topic_id: 31605 reply_id: 126872[/import]

Hi Ed!

I appreciate your suggest, but I’m afraid I can’t now change to SSK in the project I am working.
I must solve the issue of removing the display objects I added to the group in the_module.lua

Alberto. [import]uid: 44013 topic_id: 31605 reply_id: 127087[/import]

@alberto

Nope.

I’m afraid you’ll either have to insert all your display objects into a group(s) and then remove the group(s) to remove all the display objects simultaneously,

OR

You’ll have to call removeSelf() on all of the objects you want to remove. (Don’t forget to set any references to the object to nil also. Remember that LUA won’t garbage collect an object if there is still a reference to it.)

Sorry for the much delayed response, but I got a bit involved answering another forum thread: http://developer.coronalabs.com/forum/2012/10/08/calculating-intersecting-lines

I added another sample to SSK: http://www.youtube.com/watch?v=u2iZhklBzBc
-edo out! [import]uid: 110228 topic_id: 31605 reply_id: 126942[/import]

Thanks a lot @emaurina!
I’ll try creating a group containing all the display objects and removing the group when I go to other scene.

Cheers!
[import]uid: 44013 topic_id: 31605 reply_id: 126981[/import]