Multiple LUA files for multiple levels, or one LUA file for all levels?

I’ve gone through a fair amount of forum posts about this topic. It seems that creating a separate lua file for each level (if the each level is setup the same) is the “newbie” way of doing it. I see nothing wrong with doing so but I want to do it the right way. My game is kind of similar to Bubble Ball…but not really. The goal is the same on every level except there will be different obstacles. How would I go about creating one lua file to manage all the levels? Can someone show me some example code and explain?

The posts I read on this topic seemed to be case specific, as in the responses people gave would only be helpful to that person’s game. 

Just a little more information on my project. I’ll be using composer, there will be a level select screen and each level has a three star rating (not based on score, but on whether the player collected the stars during play on that level). 

Thank you.

BTW, I attached a screen shot of one of my levels. Each level will be similar. Your player is the marble at the top. You position him somewhere inside the “cage” and when you’re ready, push the start level button and he falls down. You’ve gotta capture the key and make it to the lock in one swift move. The stars are just extra credit (might possibly give players the ability to unlock a new “marble” for each stage if they collect all the stars for that level.

To me it depends on what you’re levels are like.  There is a general programming principle called DRY - Don’t Repeat Yourself.  If your level’s all basically play the same that is you load a background, a map where enemies move and the rules for beating the enemies is basically the same, then you can create a huge table of data that has the filename for images for the level, how many enemies to spawn, where they spawn and so on.  You can have one module of code and let the level data come from this big table.  As it gets more complex, perhaps you have multiple .lua files that have the data table for each level in it, and require in the levelX.lua to get the data for that level (and perhaps level specific code).  But sometimes each level needs it’s own lua file.  Think of a game like a touch and discover adventure game.  Generally each level has a unique puzzle, or things you have to find and store in inventory, or use from inventory to move around.  The code in these games is unique enough to warrant different levels.  I did a kids game once that was a fire safety app.  Each level had a different puzzle or activity for the kids, though there were a few scenes like the map that I could recycle and just change data points on it.

Hope this helps!

Rob

1 Like

It is definitely case specific. Here’s a similar discussion I participated in before, if you haven’t read it already:

https://forums.coronalabs.com/topic/55799-advice-on-storing-level-data-and-diy-editor/

To me it depends on what you’re levels are like.  There is a general programming principle called DRY - Don’t Repeat Yourself.  If your level’s all basically play the same that is you load a background, a map where enemies move and the rules for beating the enemies is basically the same, then you can create a huge table of data that has the filename for images for the level, how many enemies to spawn, where they spawn and so on.  You can have one module of code and let the level data come from this big table.  As it gets more complex, perhaps you have multiple .lua files that have the data table for each level in it, and require in the levelX.lua to get the data for that level (and perhaps level specific code).  But sometimes each level needs it’s own lua file.  Think of a game like a touch and discover adventure game.  Generally each level has a unique puzzle, or things you have to find and store in inventory, or use from inventory to move around.  The code in these games is unique enough to warrant different levels.  I did a kids game once that was a fire safety app.  Each level had a different puzzle or activity for the kids, though there were a few scenes like the map that I could recycle and just change data points on it.

Hope this helps!

Rob

It is definitely case specific. Here’s a similar discussion I participated in before, if you haven’t read it already:

https://forums.coronalabs.com/topic/55799-advice-on-storing-level-data-and-diy-editor/