Inserting objects across Lua files

I have read that some developers code individual Lua files for each level. I would like to go the route of having a level config file. The issue I am coming across is that I cannot seem to insert the objects I create in the config file into localGroup in my main file. Unfortunately, I get a generic Director error message. It is probably something obvious, but I am too close to the tree to see the forest.

The following lines are in my main file…

 local levelConfig = require("levelConfig")  
 local generateLevel = levelConfig.generateLevel  
 --  
 --  
 --  
 generateLevel({ level = vlevel })  

…and the following is the code in the config file.

 function generateLevel( params )  
 local level = params.level  
 if params then  
 if level == 1 then  
 local wall10 = display.newImage("graphics/wall.png", 500, 450)  
 physics.addBody(wall10, "static", {density=1.0, friction=1.0, bounce=0.5})  
 wall10.myName="wall"  
 localGroup:insert(wall10)  
 elseif level == 2 then  
 print ("Level2 = " .. level .. ".")  
 elseif level == 3 then  
 print ("Level3 = " .. level .. ".")  
 end  
 end  
 end  

The wall is actually created on the screen…it just isn’t in the right display group. I have tried “localGroup:insert(wall10)” in both files at different times with the same (bad) results. Is there something I need to do that can make the groups visible across Lua files or am I on the wrong rack completely??? (Is that why folks default to individual files for each level?)

Thanks,

David [import]uid: 71572 topic_id: 13065 reply_id: 313065[/import]

It doesn’t look like you’re returning anything from the levelConfig file since there is no return statement. If you want the wall to be in the localGroup of the main file then insert the variable you have assigned to it, in this case you would have to insert generateLevel in to the localGroup. [import]uid: 27965 topic_id: 13065 reply_id: 47977[/import]

Good point calebr2048! :slight_smile: I have added that piece but still am having issues.

Main file…

 local levelConfig = require("levelConfig")  
 local generateLevel = levelConfig.generateLevel  
 --  
 --  
 local gameElements = generateLevel({ level = vlevel });  
 --  
 --  
 localGroup:insert( gameElements )  

…and config file.

function generateLevel( params )  
 local level = params.level  
 if params then  
 if level == 1 then  
 local wall10 = display.newImage("graphics/wall.png", 500, 450)  
 physics.addBody(wall10, "static", {density=1.0, friction=1.0, bounce=0.5})  
 wall10.myName="wall"  
 elseif level == 2 then  
 print ("Level2 = " .. level .. ".")  
 elseif level == 3 then  
 print ("Level3 = " .. level .. ".")  
 end  
 return object;  
 end  
end  

I should probably also mention that the level won’t have just one wall. Presumably, a level may have any number of objects that would be grouped in a display object with that display object returned to the main file for insertion into the localGroup.

Sorry if I appear to be dense this morning…

David [import]uid: 71572 topic_id: 13065 reply_id: 47980[/import]

Got it. I needed more coffee…and to read through more Director documentation. Apparently a similar issue was raised last December and reply #79 from nglasier had what I needed. Sorry to have repeated an issue. [import]uid: 71572 topic_id: 13065 reply_id: 47994[/import]