Multiple Levels

Hello I recently posted a question , but decided to change it to here. I am making a game where I have an Easy , Medium , and Hard. The difference is the speed , enemies, etc. How can I do it so based on which one I choose I go to that difficulty without having to create multiple scenes. After the character dies I want to put a restart button that restarts to the difficulty that you were on. I was thinking of creating a table and having one file containing all the information or each difficulty have its own file .

Don’t repeat yourself!  By that I mean don’t duplicate your code when its only a few variables that are changing.

What you should think about is creating a separate configuration file with your game parameters in for easy/medium/hard in a lua table.

Your game would require that file and read the values for the difficulty level set.

Then all you need to do is pass a parameter to the scene to tell it what difficulty setting you want to use.  If you are using composer for your scene transitions you can pass variables between scenes - see the “params” table here -> https://docs.coronalabs.com/api/library/composer/gotoScene.html

So in a nutshell you would end up with something like this:

---------------------------------------- --First file - This would be saved as gameConfig.lua ---------------------------------------- local gameConfig = { ["easy"] = { time = 30000, enemies = 5, --More game variables... }, ["medium"] = { time = 20000, enemies = 10, --More game variables... }, ["hard"] = { time = 10000, enemies = 20, --More game variables... } } return gameConfig ---------------------------------------- --Second file - Lets call it gameScene.lua ---------------------------------------- local composer = require( "composer" ) local scene = composer.newScene() --Get the game config file local gameConfig = require("gameConfig") ------------------------------------------------------------------ --Create Scene ------------------------------------------------------------------ function scene:create( event ) local group = self.view --These params would have been passed from you menu scene where you select the difficulty... local sceneParams = event.params --Assume we just pass "easy", "medium" or "hard" local difficulty = sceneParams.difficulty --Now we can get the variables that control the game difficulty local time = gameConfig[difficulty].time local enemies = gameConfig[difficulty].enemies end --There would be more composer code below here but I just cropped it off for simplicity!

This explanation is extremely broad but hopefully will get you thinking along the right lines…

@phil_s Will I also be able to use this to create a score screen depending on which difficulty it is set on?

Don’t repeat yourself!  By that I mean don’t duplicate your code when its only a few variables that are changing.

What you should think about is creating a separate configuration file with your game parameters in for easy/medium/hard in a lua table.

Your game would require that file and read the values for the difficulty level set.

Then all you need to do is pass a parameter to the scene to tell it what difficulty setting you want to use.  If you are using composer for your scene transitions you can pass variables between scenes - see the “params” table here -> https://docs.coronalabs.com/api/library/composer/gotoScene.html

So in a nutshell you would end up with something like this:

---------------------------------------- --First file - This would be saved as gameConfig.lua ---------------------------------------- local gameConfig = { ["easy"] = { time = 30000, enemies = 5, --More game variables... }, ["medium"] = { time = 20000, enemies = 10, --More game variables... }, ["hard"] = { time = 10000, enemies = 20, --More game variables... } } return gameConfig ---------------------------------------- --Second file - Lets call it gameScene.lua ---------------------------------------- local composer = require( "composer" ) local scene = composer.newScene() --Get the game config file local gameConfig = require("gameConfig") ------------------------------------------------------------------ --Create Scene ------------------------------------------------------------------ function scene:create( event ) local group = self.view --These params would have been passed from you menu scene where you select the difficulty... local sceneParams = event.params --Assume we just pass "easy", "medium" or "hard" local difficulty = sceneParams.difficulty --Now we can get the variables that control the game difficulty local time = gameConfig[difficulty].time local enemies = gameConfig[difficulty].enemies end --There would be more composer code below here but I just cropped it off for simplicity!

This explanation is extremely broad but hopefully will get you thinking along the right lines…

@phil_s Will I also be able to use this to create a score screen depending on which difficulty it is set on?