storyboard scenes and modules

Hey all,
many thanks for your patience already. I was just wondering, I have a homescreen scene and I can go into it, but my other modules, which are not scenes, are still active, my modules that contain my hero, and enemy etc. How do I “turn off” those modules when I enter a new scene, or should I make all my modules into scenes? I am trying to utilize the storyboard API. Thanks in advance.
SM [import]uid: 79586 topic_id: 30162 reply_id: 330162[/import]

anyone? [import]uid: 79586 topic_id: 30162 reply_id: 120901[/import]

Hi Paul,
by active I mean loaded or running. It seems my hero is still running in the background when I change to a new scene, and so are all of my other modules. Your example helps and I will give it a try. Thanks!
p.s. do you know anything about troubleshooting the physics stuff? [import]uid: 79586 topic_id: 30162 reply_id: 120917[/import]

i don’t know what you do exactly. You want external modules to return sth, so you can use it as a object in your scene, where you can destroy it on scene change. Insert it in the scene group or destroy it with you own function in scene:exitScene(event)

Let’s assume you’ve an external module and create a display object within that module without returning it. This could be a problem, but i hope you don’t do that?

[import]uid: 98393 topic_id: 30162 reply_id: 121145[/import]

anyone? [import]uid: 79586 topic_id: 30162 reply_id: 120901[/import]

Hi Paul,
by active I mean loaded or running. It seems my hero is still running in the background when I change to a new scene, and so are all of my other modules. Your example helps and I will give it a try. Thanks!
p.s. do you know anything about troubleshooting the physics stuff? [import]uid: 79586 topic_id: 30162 reply_id: 120917[/import]

@Paul, I actually have the same exact problem. When loading my external module for my player, everything works fine when transitioning from scene1 to scene2. However, when I make the call to transition back to scene1, I see terminal data that says my player is still active! I’m transitioning fine but I can’t figure out how to unload/remove the player module.

I tried to implement the code as you specified above, but I get a Runtime error when loading the hero module in scene2. I even created a test project with your test data above, and received the same error. Very strange. Do you have any other suggestions? @Middleton, did you ever get this sorted for yourself? Any information would be great. Thanks!

[import]uid: 135394 topic_id: 30162 reply_id: 121704[/import]

Hi Paul,
That makes sense, I think I am not returning the display object. I will try that. Thanks. [import]uid: 79586 topic_id: 30162 reply_id: 121723[/import]

I think you can also create a destroy function in your external module like

  
function hero:destroy()  
 display.remoe(hero)  
hero = nil  
  
end  

I’m not sure about what’s best. [import]uid: 98393 topic_id: 30162 reply_id: 121732[/import]

I am still having trouble setting my module to be an object. maybe I just need take out the modules and do it all in a scene? [import]uid: 79586 topic_id: 30162 reply_id: 121736[/import]

@Paul, thanks for the advice! That didn’t really work, but there must be some way to do it.

@Middleton, I was just about to post this question to the main storyboard forum. Would you rather do it since you are the originator of the post? Let me know because I think we are both in the same boat. [import]uid: 135394 topic_id: 30162 reply_id: 121737[/import]

feel free to post away! [import]uid: 79586 topic_id: 30162 reply_id: 121740[/import]

hmmm… maybe I’m crazy but I can’t find the option to post in the Storyboard forum. This is not my day. [import]uid: 135394 topic_id: 30162 reply_id: 121743[/import]

Hi paul,
thanks again for the advice. I copied your code exactly, but maybe I am missing something because I am still getting errors. what does return M do? Does this code work when you do it? Thanks.
SM [import]uid: 79586 topic_id: 30162 reply_id: 121752[/import]

i don’t know what you do exactly. You want external modules to return sth, so you can use it as a object in your scene, where you can destroy it on scene change. Insert it in the scene group or destroy it with you own function in scene:exitScene(event)

Let’s assume you’ve an external module and create a display object within that module without returning it. This could be a problem, but i hope you don’t do that?

[import]uid: 98393 topic_id: 30162 reply_id: 121145[/import]

what do you mean by active? An example would be great!

Suppose you’ve a hero.lua (external module)

local M = {}  
local function createHero()  
 local Hero = display.newImageRect( "hero.png", 100, 100 )   
 return Hero  
end  
  
M.createHero = createHero  
  
return M  

game.lua

local hero = require ("hero")  
  
local storyboard = require( "storyboard" )  
local scene = storyboard.newScene()  
   
function scene:createScene( event )  
 local group = self.view  
  
 local Hero\_one  
 Hero\_one = hero.createHero()  
  
 group:insert(Hero\_one) -- will be destroyed when scene changes  
  
 -- do sth with hero obj  
 transition.to(Hero\_one, {time = 200, x = 400})  
end  
  

See, that’s how I use external modules, either my own or from Community Exchange. I don’t know what you mean by active modules. As you can see my hero object will be properly destroyed. (“unloaded”) [import]uid: 98393 topic_id: 30162 reply_id: 120916[/import]

Oh, sry for the confusion. I made a mistake!

[lua]M.createHero [/lua]
should be
[lua]M.createHero = createHero[/lua]

you return the table M with all your assigned values
you can also write it this way

[lua]M = { createHero = createHero}[/lua]

http://lua.gts-stolberg.de/en/Tables.php#
http://www.coronalabs.com/blog/2011/09/05/a-better-approach-to-external-modules/
just fresh in:
http://www.coronalabs.com/blog/2012/08/28/how-external-modules-work-in-corona/ [import]uid: 98393 topic_id: 30162 reply_id: 121822[/import]

@Paul, I was staring at the “better external modules” tutorial all day Tuesday but didn’t put it together with this situation. I greatly appreciate your help, and I’m off and running! @Middleton, getting your pieces of the hero/player module to work nice with your scene takes some trial and error.

@AnscaStaff: can someone move this to the Storyboard forum so that it can help other users? Thanks! [import]uid: 135394 topic_id: 30162 reply_id: 121879[/import]

Just to put a bookend on this, I think it should be noted somewhere that Runtime events can’t be called for modules. Just found that out the hard way. [import]uid: 135394 topic_id: 30162 reply_id: 122050[/import]

That helps a lot Paul. Thanks. Now I am having trouble having nesting functions in my module. Do I need to make a table for each of them? For instance, what if I wanted to have 2 heroes in your example, instead of just one? How would I do that? have 2 heroes, with 2 different images? Thanks again for all the help. Learning is fun. :slight_smile:

SM [import]uid: 79586 topic_id: 30162 reply_id: 122071[/import]