Best practice for setting up scene

Hi all,

Just a quick question about setting up your scene. Is it better to setup all the display objects in a function outside the scene:create (at the top) and call the setup function in the scene:create section, or is it fine just to create all the objects in the scene:create section (not using a function). Just wondering as ive seen it done both ways. Is there a best practice for this?

Cheers
MARK

Most people create things in scene:create() unless it’s something really complex, then having a more manageable function at the top might make more sense.

Generally separate functions are great if you’re going to be re-creating the object multiple times. If your scene:create gets really long, you might want to group things together and break it into chunks.

Rob

I find it makes more sense to have most of the ‘functional’ parts of your code either in modules outside the scene file, or in functions separate from the scene methods: create(), show(), etc.

Why?  Because, all too often, I see users (new and old) write long, long, messy, long, complex, … you get the idea, code in the scene methods, and then because the file is so long, they goof up basic stuff about scene management.  i.e. It just gets to be a disorganized mess mixed up with the scene management code.

However, if you put all the code that does building and other work in a separate module, you can simply require the module in a lightweight scene file, pass the scene group to the module (for managing objects created there), and keep the scene file easily read and managed.  This also makes your code much more portable in case you want to re-use bits of it in the future.

One thing I didn’t mention though, is project size.  If you’re doing a very short or small project (less than say 1000 lines of code), you might be fine embedding all the functional code in the scene management methods.  Adding a module structure to a small project may simply complicate things for no purpose.

Here are some samples of composer scenes I put together for the Corona Geek Show (dig through the git for more):

Oh, and be sure to peruse the games we made:

https://github.com/roaminggamer/CoronaGeek/tree/master/Hangouts/ICanMakeThat

Most of these have a version that uses composer to wrap the game.

EX: github.com/roaminggamer/CoronaGeek/tree/master/Hangouts/ICanMakeThat/ZigZagBoomClone/app/framed

This tutorial/project might help you too:
 

https://coronalabs.com/blog/2015/04/14/tutorial-the-basic-game-template/

Rob

Great, thanks for the replies guys. Game I’m working on is pretty small so think ill keep it to the game.lua for now.

@roaminggamer - I’ve been working through your Hangouts for a while which has been really helpful. I’m alot more comfortable with the module setup now but I’m still wrapping my head round the layer format. Should have it soon though. 

Thanks again

Most people create things in scene:create() unless it’s something really complex, then having a more manageable function at the top might make more sense.

Generally separate functions are great if you’re going to be re-creating the object multiple times. If your scene:create gets really long, you might want to group things together and break it into chunks.

Rob

I find it makes more sense to have most of the ‘functional’ parts of your code either in modules outside the scene file, or in functions separate from the scene methods: create(), show(), etc.

Why?  Because, all too often, I see users (new and old) write long, long, messy, long, complex, … you get the idea, code in the scene methods, and then because the file is so long, they goof up basic stuff about scene management.  i.e. It just gets to be a disorganized mess mixed up with the scene management code.

However, if you put all the code that does building and other work in a separate module, you can simply require the module in a lightweight scene file, pass the scene group to the module (for managing objects created there), and keep the scene file easily read and managed.  This also makes your code much more portable in case you want to re-use bits of it in the future.

One thing I didn’t mention though, is project size.  If you’re doing a very short or small project (less than say 1000 lines of code), you might be fine embedding all the functional code in the scene management methods.  Adding a module structure to a small project may simply complicate things for no purpose.

Here are some samples of composer scenes I put together for the Corona Geek Show (dig through the git for more):

Oh, and be sure to peruse the games we made:

https://github.com/roaminggamer/CoronaGeek/tree/master/Hangouts/ICanMakeThat

Most of these have a version that uses composer to wrap the game.

EX: github.com/roaminggamer/CoronaGeek/tree/master/Hangouts/ICanMakeThat/ZigZagBoomClone/app/framed

This tutorial/project might help you too:
 

https://coronalabs.com/blog/2015/04/14/tutorial-the-basic-game-template/

Rob

Great, thanks for the replies guys. Game I’m working on is pretty small so think ill keep it to the game.lua for now.

@roaminggamer - I’ve been working through your Hangouts for a while which has been really helpful. I’m alot more comfortable with the module setup now but I’m still wrapping my head round the layer format. Should have it soon though. 

Thanks again