[Resolved] Unlock Level, play previous level, Refresh and unlocked levels close again

Hi Guys,

I have a small problem with unlocking levels. I use Peach Pellen´s EGO (which is great btw) to unlock levels. I´ll try and explain it as good as I can:

Let´s say for instance I play the game and unlock level 4. I close the game (or refresh it) and decide I want to play level 2 again. I reach the desired score, refresh the game and when I come back in, Level 4 is locked again, and only level 3 is unlocked (as the score logic of level 2 triggerd the unlocking of level 3 but failed to “remember” level 4 was also unlocked in a previous session ). Any tips on how to change the logic so that level 4 in this example would remain unlocked even if you go back to play a previous level ?

This is my code:

– Main.lua

[lua]-- Global Vars

_W = display.contentWidth
_H = display.contentHeight

score = 0

velocity = 70

force_multiplier = 4

level1unlocked = false
level2unlocked = false
level3unlocked = false
level4unlocked = false
level5unlocked = false
level6unlocked = false
level7unlocked = false
level8unlocked = false
level9unlocked = false

–Require and setup Ego
ego = require “ego”
saveFile = ego.saveFile
loadFile = ego.loadFile

local storyboard = require “storyboard”
storyboard.gotoScene( “menu” )[/lua]

– Menu.lua
[lua]local storyboard = require( “storyboard” )
local scene = storyboard.newScene()

–Check to see if a file for the current level already exists, if it
–doesn’t then create it and set the current level to 1.
local function checkForFile ()
currentLevel = loadFile (“currentLevel.txt”)
if currentLevel == “empty” then
currentLevel = 1
saveFile(“currentLevel.txt”, currentLevel)
end

if level1unlocked == true then
currentLevel = 2

end

if level2unlocked == true then
currentLevel = 3

end

if level3unlocked == true then
currentLevel = 4

end

if level4unlocked == true then
currentLevel = 5

end

if level5unlocked == true then
currentLevel = 6

end

if level6unlocked == true then
currentLevel = 7

end

if level7unlocked == true then
currentLevel = 8

end

if level8unlocked == true then
currentLevel = 9

end

if level9unlocked == true then
currentLevel = 10

end

end
checkForFile()

–Table for levels
local level = {}

local function onSceneTouch(event )
if event.phase == “ended” then

storyboard.gotoScene( event.target.scene )

return true
end
end

local startX = 45

function scene:createScene( event )
local localGroup = self.view

–Load level icons accordingly
local function setupLevels()
for i = 1, 10 do
if tonumber(currentLevel) >= i then
level[i] = display.newCircle( startX*i, 50, 20 )
level[i]:setFillColor(220, 220, 120)
localGroup:insert( level[i] )
level[i].scene = “level”…i…""
level[i]:addEventListener(“touch”, onSceneTouch)
elseif tonumber(currentLevel) < i then
level[i] = display.newImageRect(“images/lock.png”,50,50)
level[i]:setFillColor(255, 255, 255)
level[i].x = startX*i
level[i].y = 50

localGroup:insert( level[i] )
end
end
end
setupLevels()

Reset = display.newImageRect(“images/reset.png”, _W/10,75)
Reset:setReferencePoint(display.CenterReferencePoint)
Reset.x = _W-150
Reset.y = _H-100
Reset.scene = “refresh”
localGroup:insert(Reset)

end

function scene:enterScene( event )

local prior_scene = storyboard.getPrevious()
storyboard.purgeScene( prior_scene )

Reset:addEventListener(“touch”, onSceneTouch)

end

scene:addEventListener( “enterScene”,scene )
scene:addEventListener( “createScene”, scene )

– local function resetData(event)
– saveFile(“currentLevel.txt”, 1)

– end

–resetData()

return scene[/lua]
– Level3.lua, unlock levels logic

[lua]function scene:enterScene( event )
local group = self.view

– to make sure the menu file reads the new state of the boolean levelUnlocked

local prior_scene = storyboard.getPrevious()
storyboard.removeScene( prior_scene )

– Collision event triggering a score update and changing the ballsIn var

function bullet:collision (event)
if event.other.myName == “bar” then
bullet.isLoaded = true
end
if bullet.isLoaded == true and event.other.myName == “basket” and event.phase == “ended” then
score = score + 10
scoreText.text = " " … score
ballsIn = ballsIn + 1
end
end
end

– Unlock level logic when the desired score has been reached

local function checkWin ()
if ballsIn == 1 then

saveFile(“currentLevel.txt”, 4)
level3unlocked = true
end

end
Runtime:addEventListener(“enterFrame”, checkWin)

function scene:exitScene( event )

score = 0
ballsIn = 0

end[/lua] [import]uid: 126207 topic_id: 30500 reply_id: 330500[/import]

i recommend using ice it is much easier to use-

http://developer.coronalabs.com/code/ice

happy coding

-Boxie [import]uid: 113909 topic_id: 30500 reply_id: 122242[/import]

Ok what you want to do to replay the level do something like this

local function checkWin ()  
If ballsIn == 1 then  
saveFile("currentLevel.txt", 4)  
level3unlocked = true  
elseif ballsIn == 1 and tonumber(currentLevel) \< 4 then  
saveFile("currentLevel.txt", 4)  
end  
Runtime:addEventListener("enterframe", checkWin)  

You can try doing that
[import]uid: 17058 topic_id: 30500 reply_id: 122247[/import]

i recommend using ice it is much easier to use-

http://developer.coronalabs.com/code/ice

happy coding

-Boxie [import]uid: 113909 topic_id: 30500 reply_id: 122242[/import]

Ok what you want to do to replay the level do something like this

local function checkWin ()  
If ballsIn == 1 then  
saveFile("currentLevel.txt", 4)  
level3unlocked = true  
elseif ballsIn == 1 and tonumber(currentLevel) \< 4 then  
saveFile("currentLevel.txt", 4)  
end  
Runtime:addEventListener("enterframe", checkWin)  

You can try doing that
[import]uid: 17058 topic_id: 30500 reply_id: 122247[/import]

@ Sebittas

It didn´t do the trick unfortunately, I still just unlock level 4 after having come back to play level three with level 6,7,8 etc. unlocked. I think I would need some code in the “elseif” logic that tracks the last level unlocked.

Any ideas ?
@ boxie

Thanks for the tip. If it really doesn´t work out with ICE I´ll definitelycheck it out. [import]uid: 126207 topic_id: 30500 reply_id: 122981[/import]

Just use another variable alongside currentLevel, say unlocked, which increments every time a level is completed for the first time:

[lua]local function checkWin ()
if ballsIn == 1 then

if level3unlocked == false then
level3unlocked = true
unlocked = unlocked + 1
saveFile(“currentLevel.txt”, 4)
saveFile(“unlocked.txt”, unlocked)
end
end

end[/lua]
[lua]local function checkForFile ()
currentLevel = loadFile (“currentLevel.txt”)
if currentLevel == “empty” then
currentLevel = 1
unlocked = 1
saveFile(“currentLevel.txt”, currentLevel)
saveFile(“unlocked.txt”, unlocked)
end

– no need to check the unlocked statuses to get current level, it will either be loaded from disk or be = 1
end

end[/lua]

[lua]local function setupLevels()
for i = 1, 10 do
if tonumber(unlocked) >= i then
level[i] = display.newCircle( startX*i, 50, 20 )
level[i]:setFillColor(220, 220, 120)
localGroup:insert( level[i] )
level[i].scene = “level”…i…""
level[i]:addEventListener(“touch”, onSceneTouch)
elseif tonumber(unlocked) < i then
level[i] = display.newImageRect(“images/lock.png”,50,50)
level[i]:setFillColor(255, 255, 255)
level[i].x = startX*i
level[i].y = 50

localGroup:insert( level[i] )
end
end
end

I would recommend looking into using arrays rather than level1unlocked, level2unlocked etc [import]uid: 93133 topic_id: 30500 reply_id: 122983[/import]

Thanks Nick_sherman

I tried it out but I get an error that the tonumber(unlocked) failed to compare to a number (a nil value).

I know from previous tampering that it´s a pretty fragile function. Might you have an idea why the function doesn´t read the variable “unlocked”, not even from the settings from the checkfile() function ?

Also, in your code, don´t the “level2unlocked” etc. vars become redundant? [import]uid: 126207 topic_id: 30500 reply_id: 122991[/import]

I think unlocked will be nil because the part of the code that initialises it to 1 is only run when the app first runs (after that currentLevel will never be ‘empty’). Also I forgot to add in the line which loads the unlocked number from the file:

[lua]local function checkForFile ()
currentLevel = loadFile (“currentLevel.txt”)
unlocked = loadFile(“unlocked.txt”)

– currentLevel = “empty” – uncomment this flag to simulate the game being run for the first time
if currentLevel == “empty” then

currentLevel = 1
unlocked = 1
saveFile(“currentLevel.txt”, currentLevel)
saveFile(“unlocked.txt”, unlocked)
end

– no need to check the unlocked statuses to get current level, it will either be loaded from disk or be = 1
end

end [/lua] [import]uid: 93133 topic_id: 30500 reply_id: 122993[/import]

Works like a charm. Thanks a lot man!! [import]uid: 126207 topic_id: 30500 reply_id: 123005[/import]

Hi,

sorry that I have to get back to you guys, but some new testing has uncovered a new problem with the code. When I play the game and unlock for instance level 2 and I replay level 1 where I trigger the Check Win conditions again, sometimes level 3 also opens, even without having played level 2.

I would of thought the level1unlocked vars would of prevented that ?

Any ideas ?

Grtz,

Jan [import]uid: 126207 topic_id: 30500 reply_id: 123178[/import]

Hi,

sorry that I have to get back to you guys, but some new testing has uncovered a new problem with the code. When I play the game and unlock for instance level 2 and I replay level 1 where I trigger the Check Win conditions again, sometimes level 3 also opens, even without having played level 2.

I would of thought the level1unlocked vars would of prevented that ?

Any ideas ?

Grtz,

Jan [import]uid: 126207 topic_id: 30500 reply_id: 123179[/import]

@ Sebittas

It didn´t do the trick unfortunately, I still just unlock level 4 after having come back to play level three with level 6,7,8 etc. unlocked. I think I would need some code in the “elseif” logic that tracks the last level unlocked.

Any ideas ?
@ boxie

Thanks for the tip. If it really doesn´t work out with ICE I´ll definitelycheck it out. [import]uid: 126207 topic_id: 30500 reply_id: 122981[/import]

Just use another variable alongside currentLevel, say unlocked, which increments every time a level is completed for the first time:

[lua]local function checkWin ()
if ballsIn == 1 then

if level3unlocked == false then
level3unlocked = true
unlocked = unlocked + 1
saveFile(“currentLevel.txt”, 4)
saveFile(“unlocked.txt”, unlocked)
end
end

end[/lua]
[lua]local function checkForFile ()
currentLevel = loadFile (“currentLevel.txt”)
if currentLevel == “empty” then
currentLevel = 1
unlocked = 1
saveFile(“currentLevel.txt”, currentLevel)
saveFile(“unlocked.txt”, unlocked)
end

– no need to check the unlocked statuses to get current level, it will either be loaded from disk or be = 1
end

end[/lua]

[lua]local function setupLevels()
for i = 1, 10 do
if tonumber(unlocked) >= i then
level[i] = display.newCircle( startX*i, 50, 20 )
level[i]:setFillColor(220, 220, 120)
localGroup:insert( level[i] )
level[i].scene = “level”…i…""
level[i]:addEventListener(“touch”, onSceneTouch)
elseif tonumber(unlocked) < i then
level[i] = display.newImageRect(“images/lock.png”,50,50)
level[i]:setFillColor(255, 255, 255)
level[i].x = startX*i
level[i].y = 50

localGroup:insert( level[i] )
end
end
end

I would recommend looking into using arrays rather than level1unlocked, level2unlocked etc [import]uid: 93133 topic_id: 30500 reply_id: 122983[/import]

Thanks Nick_sherman

I tried it out but I get an error that the tonumber(unlocked) failed to compare to a number (a nil value).

I know from previous tampering that it´s a pretty fragile function. Might you have an idea why the function doesn´t read the variable “unlocked”, not even from the settings from the checkfile() function ?

Also, in your code, don´t the “level2unlocked” etc. vars become redundant? [import]uid: 126207 topic_id: 30500 reply_id: 122991[/import]

I think unlocked will be nil because the part of the code that initialises it to 1 is only run when the app first runs (after that currentLevel will never be ‘empty’). Also I forgot to add in the line which loads the unlocked number from the file:

[lua]local function checkForFile ()
currentLevel = loadFile (“currentLevel.txt”)
unlocked = loadFile(“unlocked.txt”)

– currentLevel = “empty” – uncomment this flag to simulate the game being run for the first time
if currentLevel == “empty” then

currentLevel = 1
unlocked = 1
saveFile(“currentLevel.txt”, currentLevel)
saveFile(“unlocked.txt”, unlocked)
end

– no need to check the unlocked statuses to get current level, it will either be loaded from disk or be = 1
end

end [/lua] [import]uid: 93133 topic_id: 30500 reply_id: 122993[/import]

Works like a charm. Thanks a lot man!! [import]uid: 126207 topic_id: 30500 reply_id: 123005[/import]

Hi,

sorry that I have to get back to you guys, but some new testing has uncovered a new problem with the code. When I play the game and unlock for instance level 2 and I replay level 1 where I trigger the Check Win conditions again, sometimes level 3 also opens, even without having played level 2.

I would of thought the level1unlocked vars would of prevented that ?

Any ideas ?

Grtz,

Jan [import]uid: 126207 topic_id: 30500 reply_id: 123178[/import]

Hi,

sorry that I have to get back to you guys, but some new testing has uncovered a new problem with the code. When I play the game and unlock for instance level 2 and I replay level 1 where I trigger the Check Win conditions again, sometimes level 3 also opens, even without having played level 2.

I would of thought the level1unlocked vars would of prevented that ?

Any ideas ?

Grtz,

Jan [import]uid: 126207 topic_id: 30500 reply_id: 123179[/import]