Code flow for multiple stages

I am researching Corona for building an iPhone app similar to Moron Test. This means that I should implement over 30 stages with each one having its own game logic and its own interactive objects (>5 in each stage).

How would someone organise such a project in Corona? I am aware of Groups and Libraries, but I can’t see how I could isolate each stage’s code from the others’ as I would do with Cocos2d (with Scenes and Layers) or with Unity3D (with Levels). Is Corona capable for games with such a complexity? I can’t imagine 30 stages with 5000 lines of code within a single main.lua file.

Am I missing something? Are there any examples for stage management in Corona? Any showcase apps?

Thanks! [import]uid: 7356 topic_id: 1638 reply_id: 301638[/import]

Hi, I don’t know but I second this question. Does anybody have insight into this ?

Thanks! [import]uid: 8118 topic_id: 1638 reply_id: 4770[/import]

Look at the examples. There is a reason why there are multiple lua files. [import]uid: 6066 topic_id: 1638 reply_id: 4802[/import]

I was wondering the same thing. I made a quiz game with GameSalad and it was pretty easy to do what your talking about with “scenes”. With Corona, I imagine you would do this with modules. Each level would be it’s own module file that would dynamically get loaded as the user progresses. All of the common code and general game logic would go in main.lua. The tricky part would be avoiding duplicate code.

It would be nice to get some multi-level examples from Ansca because it seems like if this is done incorrectly the resulting game could suffer serious performance problems. [import]uid: 8194 topic_id: 1638 reply_id: 4945[/import]

Corona is similar to AS1 or AS2 in Flash but, with a better GC. So with Beta 6 and newer, when something is removed from the scene, its display parts are all cleared. As long as you clean everything (set things to nil) then you should be fine.

[import]uid: 6066 topic_id: 1638 reply_id: 4963[/import]

So, do you think that for a project with 30+ scenes I could code the scenes in 30 individual modules and have a clear() function in each scene that would remove the display groups when called from main.lua? Has anyone tried such complexity levels with Corona SDK so far?

[import]uid: 7356 topic_id: 1638 reply_id: 5442[/import]

@Magenda,
I have been thinking about how to organize a game with many scenes quite a bit. The module method is definitely appealing because it will keep the main.lua file very tidy. I would just be careful not to duplicate code in the scene modules - only define scene-specific objects, not game logic. Another option I am considering is creating a SQLite database or text file that contains attributes for each scene object.

I have not tried any of these methods yet but I am starting a new project that will have a lot of scenes so I’ll be choosing soon. :slight_smile:

Dave [import]uid: 8194 topic_id: 1638 reply_id: 5443[/import]

@dknell
Well, I was actually thinking of having each module containing everything that relates to a scene (logic, controls, graphics) as it would be a mini-main.lua (each scene as a mini game)!

The modules would just “message” the real main.lua that this scene has finished its purpose with success or loss for the player and main.lua would then call the scene’s clear() function and proceed to the next module/scene. Do you find this approach inefficient? From which perspective exactly? Loading times, memory waste, code repeating or something else? [import]uid: 7356 topic_id: 1638 reply_id: 5444[/import]

Apologies for keep insisting on this, but has anyone tried with a project that stands on many modules? Is the performance good? Do you consider Corona a good choice for a game with many levels/stages? Any other advice on this?

Many thanks! [import]uid: 7356 topic_id: 1638 reply_id: 5681[/import]

Yes, I have.

If you look at the sample movieclip.lua or ui.lua then you should get a pretty good idea on how to make stand alone modules. [import]uid: 6066 topic_id: 1638 reply_id: 5686[/import]

Thanks for replying…

Ansca has documented the module creation, so I guess there will be no big problems with this. My major concern is about the performance when including/calling 30 modules from within main.lua. Isn’t this automatically (in advance) load in memory all the graphics and data that are included in the modules? Imagine the Moron Test with each screen as a module. Would this pattern be efficient in terms of code design ? [import]uid: 7356 topic_id: 1638 reply_id: 5689[/import]

My app AlienHorde has a ton of modules, some that talk to each other. It all works fine and doesnt slow down performance. I think its how complex lua apps are built.

I find it easier to had a lot of modules, each taking care of one task / elements of the application so…

  1. The buttons are in a module called buttons.lua
  2. The death sequence is in a module called blood.lua
  3. The intro is in a module called intro.lua
  4. The main game loop is in a module called runtime.lua

The main.lua only initialises the app and then calls runtime.lua and it goes from there. Each module has an function called loadAssets which sets up the assets the module needs, locally the death sequence needs to load and position the blood and then animate it in.

To aid in this I have a master group which I insert the elements into, which is global. Everything else is locally declared. If 2 modules need to talk to each other then I either use functions to update the local variables in the module which other modules can see ( not declared locally in the module) or I assign values to the objects in question.

You worry about loading assets is easily taken care of by having a load function in each module. [import]uid: 5354 topic_id: 1638 reply_id: 5691[/import]

Thank you Matthew! That is very helpful. I imagine you only load modules as needed. For example:

if dead then  
 bloodMod = require("blood")  
end  

Is that how you do it? That brings up a couple other questions.

  1. Do you unload the module or free up the memory used by each module when it’s not loaded?
  2. You mentioned that you have a global master group (declared in main.lua, I imagine), isn’t that out of scope within other modules? How do you insert objects into that group from other modules, like the blood module?

Thanks again for the awesome feedback!

Dave [import]uid: 8194 topic_id: 1638 reply_id: 5692[/import]

Will get u a proper answer later if you need, not in front of code so making it up a bit.

I require all the modules at the start of the main.lua ( not idea for massive projects, but fine for mine ) and load assets within the modules through a simple load function.

so in main.lua…

-- Setup a master global group  
  
local bloodMod = require("blood")  

in blood.lua

module(..., package.seeall)  
  
-- Setup a local blood group and insert into master group  
  
function loadBlood()  
-- loads assets  
-- insert assets into local blood group  
end  
  
function showBlood()  
--animation would go here  
end  
  

from main.lua or any other module ( with the module(…, package.seeall) ) I can then just call bloodMod.showBlood. I will have loaded the assets previously

For a really simple look at how im doing this see the Joystick example code I put together. http://developer.anscamobile.com/code/joystick and download the example files from http://www.alienhorde.com/files/joystick_1_0.zip.

Understanding how to use modules was the one thing I had trouble with when I first picked up Corona, its easy though, just need examples!

[import]uid: 5354 topic_id: 1638 reply_id: 5693[/import]

I posted a tutorial on how it is done within all of our games:

–> http://ideveloper.kodingen.com/2010/easy-screen-management-in-corona/ [import]uid: 7849 topic_id: 1638 reply_id: 7237[/import]

Having hundreds of modules on screen should not be a problem. There is however a threshold that you have to adjust the number for obviously depending on the complexity of your module’s body content. Too many things will obviously slow down. My game that will be released soon is quite heavy in effects that did impact performance quite a bit when too many things happen at the same time.

Key thing is to make sure that you do not leave module instances that you don’t want on display or in memory over time.

Something I do now is to always create a module:destroy() function that clears all tables, remove all display objects, nil all none display variables before calling removeSelf() on itself, whenever the module is not needed anymore.

Sample
Particle.lua

  
function newParticle()  
 local particle = display.newGroup()  
  
 particle.xVel = 0  
 particle.yVel = 0  
  
 -- ... blha blah balh  
  
 function particle:update()  
 -- does stuff  
 if alpha \< 0.01 then  
 particle:destroy()  
 end  
 end  
  
 function particle:destroy()  
 particle.xVel = nil  
 particle.yVel = nil  
  
 particle:removeSelf()  
 end  
  
end  

In the module or main module that manages the particle, make sure that any references to particle is removed or set to nil when this particle is obviously destroyed. [import]uid: 6066 topic_id: 1638 reply_id: 7268[/import]

Thanks for this code. However, im wondering how can i call this; suppose i need to create a next and pass some parameters. The following code give an error
[blockcode]
/Development/learn/main.lua:31: attempt to index local ‘mytext’ (a nil value)
[/blockcode]

[blockcode]
function newParticle()
local particle = display.newText("", 0, 0, “verdana”, 15 )
particle.xDefault = 0
particle.yDefault = 0
particle.text =“0”
particle:setReferencePoint (display.CenterReferencePoint )
particle:setTextColor( 255, 255, 255)
particle.objectName=“TracingObject”

function particle:update(_x,_y,_text)
particle.xDefault = _x
particle.yDefault = _y
particle.text =_text
end

function particle:destroy()
particle.xDefault = nil
particle.yDefault = nil
particle.text =nil
particle:removeSelf()
end
end
local mytext = newParticle()
mytext:update(“tap to continue”,100,100)
[/blockcode]

I appreciate your feedback [import]uid: 11038 topic_id: 1638 reply_id: 12242[/import]

This is really long so I’ll admit I didn’t read every single post thoroughly, but would this not be a perfect use for director? (Sorry if this has already been mentioned above.)

Director is a godsend and frankly I could never have made the transition from using “Scenes” in Gamesalad without it - all my apps now have a main.lua, menu.lua, game.lua and gameover.lua at the very least! [import]uid: 10144 topic_id: 1638 reply_id: 12257[/import]

Since I’m also working on a project involving several “stages”, I found a good practice to provide each module with a load() function - and possibly a release() one.
Example of load:

[lua]module(…, package.seeall)

do

local mtab = {}

function mtab:load()
– Insert all code here!
end

return mtab

end[/lua]

The “do end” ensures everything stays local, unless explicitly returned. Basically, if you require all modules built this way at very beginning, all you have in memory is just the Lua code of the modules…no memory wasted.
Then you will call the load from the table the module returned, and everything will appear on stage.

@iphone_2010

man, you should return the object before attempting to call any of its methods! :wink:
[import]uid: 5750 topic_id: 1638 reply_id: 12298[/import]

Man thanks for this nice idea. But really im still lost here, maybe because im still new in lua. Anyhow, i feel this is a brilliant idea but i don’t have a clue how to implement it in real life. In my case, i have 5 levels, level1, level2, … level5), how can i implement this approach from main.lua and moving into levels.lua which will include the levels.

Thanks again mate
[import]uid: 11038 topic_id: 1638 reply_id: 12329[/import]