There is a great youtube tutorial right here explaining how to do exactly what you’re requesting. To me, since there is no significant performance gain when generating modules, having a module for each hero character might be best. Just my opinion though. [import]uid: 135394 topic_id: 30162 reply_id: 122121[/import]
Thanks, that helped me understand modules better. But I still can’t understand how to insert two images into the same function and call them both at the same time. I have a background module with two images, and I want them both to appear when I change to the background scene and I want them to both be in the same module. Any ideas? I think I need to insert them in groups or something. not sure. Thanks.
SM [import]uid: 79586 topic_id: 30162 reply_id: 122161[/import]
Can you throw some code up so we can see what you’re working with? [import]uid: 135394 topic_id: 30162 reply_id: 122236[/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]
Sure, here is an example.
function background ()
bkg1 = display.newRect(100,100, 200, 200)
bkg2 = display.newRect(300,300, 50, 50)
return bkg1
return bkg2
end
background ()
If I return both display objects I get an index error. If I break them up into separate functions, that works fine, until I try and load the module using the storyboard api. Any thought? Thanks.
SM
[import]uid: 79586 topic_id: 30162 reply_id: 122312[/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]
If that is the whole code, remember that you have to create the table for the background images, return the table at the end of the code, and not call the function inside of the module. For example, here is what Paul suggested above, and how it can be implemented in a module:
background.lua:
[lua]local B = {}
local function createBackground()
bkg1 = display.newRect(100,100, 200, 200)
bkg2 = display.newRect(300,300, 50, 50)
return bkg1
end
B.createBackground = createBackground
return B[/lua]
scene1.lua:
[lua]function scene:createScene( event )
local group = self.view
local test
test = background.createBackground()
group:insert(test) – will be destroyed when scene changes
end[/lua]
That’s the whole code, and it works like charm. I’m not sure why it works without returning the second object, but it didn’t throw any errors for me. Let me know if the above works for you. [import]uid: 135394 topic_id: 30162 reply_id: 122362[/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. 
SM [import]uid: 79586 topic_id: 30162 reply_id: 122071[/import]
There is a great youtube tutorial right here explaining how to do exactly what you’re requesting. To me, since there is no significant performance gain when generating modules, having a module for each hero character might be best. Just my opinion though. [import]uid: 135394 topic_id: 30162 reply_id: 122121[/import]
Thanks, that helped me understand modules better. But I still can’t understand how to insert two images into the same function and call them both at the same time. I have a background module with two images, and I want them both to appear when I change to the background scene and I want them to both be in the same module. Any ideas? I think I need to insert them in groups or something. not sure. Thanks.
SM [import]uid: 79586 topic_id: 30162 reply_id: 122161[/import]