Unlocking levels

Hey guys
i read a blog about creating a level selection menu.

https://googleweblight.com/?lite_url=https://coronalabs.com/blog/2014/08/12/tutorial-building-a-level-selection-scene/&ei=KNCbm3sS&lc=en-IN&s=1&m=828&host=www.google.co.in&ts=1466827695&sig=AKOVD66psxdxbsKaIktribNLCOtH-sluig

.so i have one doubt
suppose i want to unlock level2 when my score in level1 goes above 20000, then what i need to do???
one more doubt i have…suppose i want one star when my score goes above 5000, two star when my score goes above 15000, and three star when my score goes above 20000, how should i do this???
thanks

I am making example based on the link provide

local M = {} M.maxLevels = 50 M.settings = {} M.settings.currentLevel = 1 M.settings.soundOn = true M.settings.musicOn = true M.settings.levels = {} -- These lines are just here to pre-populate the table. -- In reality, your app would likely create a level entry when each level is unlocked and the score/stars are saved. -- Perhaps this happens at the end of your game level, or in a scene between game levels. M.settings.levels[1] = {} M.settings.levels[1].stars = 3 M.settings.levels[1].score = 3833 if M.settings.levels[1].score \>= 20000 then M.settings.unlockedLevels = 2 end if M.settings.levels[1].score \>= 5000 then M.settings.levels[1].stars = 1 end if M.settings.levels[1].score \>= 15000 then M.settings.levels[1].stars = 2 end if M.settings.levels[1].score \>= 20000 then M.settings.levels[1].stars = 3 end -- levels data members: -- .stars -- Stars earned per level -- .score -- Score for the level return M

Thanks… i got it

I have one more doubt

How it will know that my score has gone this much higher ?? for that, should i need to load the level1.lua module ??? how this file will know that my score has gone beyond 20000 ??

srry for my bad english 

Or the following code will do ?

local lvl1 = require “level1”

M.settings.levels[1].score = lvl1.score 

Im a bit confused 

Let me clarify a little better. The code above should be in your “mydata.lua” . mydata.lua is a way to toss data from scene to scene easily. 

For example lets say you make a level called “level1.lua” (you can call it what ever you want) . Some code like this should be in their

--level1.lua local myData = require("mydata) -- this is calling the mydata.lua file Local myScore = 20000 -- this your score what ever value you want it to be myData.settings.levels[1].score = myScore

You seem to have some confusion about modules

Check out this tutorial: https://coronalabs.com/blog/2012/08/28/how-external-modules-work-in-corona/

It still doesnt work for me :frowning:

This is my code:

game.lua: local myData = require "myData" local myScore = 3000 myData.settings.levels[1].score = myScore myData.lua : local M = {} M.maxLevels = 5 M.settings = {} M.settings.currentLevel = 1 M.settings.unlockedLevels = 1 M.settings.soundOn = true M.settings.musicOn = true M.settings.levels = {} M.settings.levels[1] = {} M.settings.levels[1].stars = 0 M.settings.levels[1].score = 0 if M.settings.levels[1].score \> 2000 then M.settings.levels[1].stars = 3 end M.settings.levels[2] = {} M.settings.levels[2].stars = 0 M.settings.levels[2].score = 0 M.settings.levels[3] = {} M.settings.levels[3].stars = 0 M.settings.levels[3].score = 0 M.settings.levels[4] = {} M.settings.levels[4].stars = 0 M.settings.levels[4].score = 0 -- levels data members: -- .stars -- Stars earned per level -- .score -- Score for the level return M

my score is greater than 2000 but still i cannot see three stars…

please help :slight_smile: :) 

You are setting Ito zero
Remove this line line
M.settings.levels[1].score = 0

then it is giving me the following error:

mydata.lua:12:attempt to compare number with nil

stack traceback:

   [C]: in function ‘error’

   ?: in function ‘gotoScene’

   main.lua:3: in main chunk 

this is my code:

game.lua:

local myData = require “myData”

local myScore = 3000
myData.settings.levels[1].score = myScore

myData.lua :

local M = {}
M.maxLevels = 5
M.settings = {}
M.settings.currentLevel = 1
M.settings.unlockedLevels = 1
M.settings.soundOn = true
M.settings.musicOn = true
M.settings.levels = {}
M.settings.levels[1] = {}
M.settings.levels[1].stars = 0
if M.settings.levels[1].score > 2000 then
M.settings.levels[1].stars = 3
end

M.settings.levels[2] = {}
M.settings.levels[2].stars = 0
M.settings.levels[2].score = 0
M.settings.levels[3] = {}
M.settings.levels[3].stars = 0
M.settings.levels[3].score = 0
M.settings.levels[4] = {}
M.settings.levels[4].stars = 0
M.settings.levels[4].score = 0

– levels data members:
– .stars – Stars earned per level
– .score – Score for the level
return M

Change this

if M.settings.levels[1].score \> 2000 then M.settings.levels[1].stars = 3 end

To this

if M.settings.levels[1].score and M.settings.levels[1].score \> 2000 then M.settings.levels[1].stars = 3 end

still not working…showing no errors

but cannot see three stars :( 

this module does not auto save. If you restart this simulator that data you to mydata is going to go away. You need to save all you data. Read this blog to see how to do that. https://coronalabs.com/blog/2014/10/14/tutorial-saving-and-loading-lua-tables-with-json/

i too was was thinking about that

Will figure out by reading that blog

Thanks anyways for replying :) 

I am making example based on the link provide

local M = {} M.maxLevels = 50 M.settings = {} M.settings.currentLevel = 1 M.settings.soundOn = true M.settings.musicOn = true M.settings.levels = {} -- These lines are just here to pre-populate the table. -- In reality, your app would likely create a level entry when each level is unlocked and the score/stars are saved. -- Perhaps this happens at the end of your game level, or in a scene between game levels. M.settings.levels[1] = {} M.settings.levels[1].stars = 3 M.settings.levels[1].score = 3833 if M.settings.levels[1].score \>= 20000 then M.settings.unlockedLevels = 2 end if M.settings.levels[1].score \>= 5000 then M.settings.levels[1].stars = 1 end if M.settings.levels[1].score \>= 15000 then M.settings.levels[1].stars = 2 end if M.settings.levels[1].score \>= 20000 then M.settings.levels[1].stars = 3 end -- levels data members: -- .stars -- Stars earned per level -- .score -- Score for the level return M

Thanks… i got it

I have one more doubt

How it will know that my score has gone this much higher ?? for that, should i need to load the level1.lua module ??? how this file will know that my score has gone beyond 20000 ??

srry for my bad english 

Or the following code will do ?

local lvl1 = require “level1”

M.settings.levels[1].score = lvl1.score 

Im a bit confused 

Let me clarify a little better. The code above should be in your “mydata.lua” . mydata.lua is a way to toss data from scene to scene easily. 

For example lets say you make a level called “level1.lua” (you can call it what ever you want) . Some code like this should be in their

--level1.lua local myData = require("mydata) -- this is calling the mydata.lua file Local myScore = 20000 -- this your score what ever value you want it to be myData.settings.levels[1].score = myScore

You seem to have some confusion about modules

Check out this tutorial: https://coronalabs.com/blog/2012/08/28/how-external-modules-work-in-corona/

It still doesnt work for me :frowning:

This is my code:

game.lua: local myData = require "myData" local myScore = 3000 myData.settings.levels[1].score = myScore myData.lua : local M = {} M.maxLevels = 5 M.settings = {} M.settings.currentLevel = 1 M.settings.unlockedLevels = 1 M.settings.soundOn = true M.settings.musicOn = true M.settings.levels = {} M.settings.levels[1] = {} M.settings.levels[1].stars = 0 M.settings.levels[1].score = 0 if M.settings.levels[1].score \> 2000 then M.settings.levels[1].stars = 3 end M.settings.levels[2] = {} M.settings.levels[2].stars = 0 M.settings.levels[2].score = 0 M.settings.levels[3] = {} M.settings.levels[3].stars = 0 M.settings.levels[3].score = 0 M.settings.levels[4] = {} M.settings.levels[4].stars = 0 M.settings.levels[4].score = 0 -- levels data members: -- .stars -- Stars earned per level -- .score -- Score for the level return M

my score is greater than 2000 but still i cannot see three stars…

please help :slight_smile: :) 

You are setting Ito zero
Remove this line line
M.settings.levels[1].score = 0

then it is giving me the following error:

mydata.lua:12:attempt to compare number with nil

stack traceback:

   [C]: in function ‘error’

   ?: in function ‘gotoScene’

   main.lua:3: in main chunk 

this is my code:

game.lua:

local myData = require “myData”

local myScore = 3000
myData.settings.levels[1].score = myScore

myData.lua :

local M = {}
M.maxLevels = 5
M.settings = {}
M.settings.currentLevel = 1
M.settings.unlockedLevels = 1
M.settings.soundOn = true
M.settings.musicOn = true
M.settings.levels = {}
M.settings.levels[1] = {}
M.settings.levels[1].stars = 0
if M.settings.levels[1].score > 2000 then
M.settings.levels[1].stars = 3
end

M.settings.levels[2] = {}
M.settings.levels[2].stars = 0
M.settings.levels[2].score = 0
M.settings.levels[3] = {}
M.settings.levels[3].stars = 0
M.settings.levels[3].score = 0
M.settings.levels[4] = {}
M.settings.levels[4].stars = 0
M.settings.levels[4].score = 0

– levels data members:
– .stars – Stars earned per level
– .score – Score for the level
return M

Change this

if M.settings.levels[1].score \> 2000 then M.settings.levels[1].stars = 3 end

To this

if M.settings.levels[1].score and M.settings.levels[1].score \> 2000 then M.settings.levels[1].stars = 3 end

still not working…showing no errors

but cannot see three stars :( 

this module does not auto save. If you restart this simulator that data you to mydata is going to go away. You need to save all you data. Read this blog to see how to do that. https://coronalabs.com/blog/2014/10/14/tutorial-saving-and-loading-lua-tables-with-json/