CORONA SDK SAVING AND LOADING IN THE SAME SESSION.....

Hello Coronians,

I have a problem. I have setup a level select screen and used the loadsave module to save the progress. The problem is, When i complete the level and go back to the level select scene it doesn’t show the completed icon but when i restart the app, Then only it shows the progress.

My code is … in levelselect.lua

local \_w = display.contentWidth / 2 local \_h = display.contentHeight / 2 local loadsave = require("loadsave") require("levelFunctions") B = loadsave.loadTable("btLevels.json") Btlvls = B.btlevels images ={ { getFile = "pl.png", types = "done"}, { getFile = "ul.png", types = "play"}, } ------------------------------------------------------------------------------- --Creates the display groups and the event listeners the create method function scene:create( event ) local sceneGroup = self.view local bg = display.newImage( "normalBg.png" ) bg.x = \_w bg.y = \_h sceneGroup:insert(bg) local tbg = display.newImage("tbg.png") tbg.x = \_w tbg.y = \_h + 40 sceneGroup:insert(tbg) local intro = display.newImage("btbg.png") intro.x = \_w intro.y = \_h - 190 sceneGroup:insert(intro) local bckBtn = display.newImage("bckBtn.png") bckBtn.height = 65 bckBtn.width = 65 bckBtn.x = \_w - 300 bckBtn.y = \_h - 190 sceneGroup:insert(bckBtn) local function buttonHit(event) composer.gotoScene ( event.target.destination, {effect = "fade"} ) print( event.target.destination) return true end local levelIndex =0 for i=0,3 do for j=1,5 do tablePlace = i\*5 + j levelIndex = levelIndex + 1 local imagesId = Btlvls[levelIndex] levelImg = display.newImage(images[imagesId].getFile , 70, 70 ) levelImg.x = \_w/10 + (j\*120) levelImg.y = \_h-100+ (i\*90) sceneGroup:insert(levelImg) leveltxt = display.newText(tostring(tablePlace), 0,0, "sniglet", 40) leveltxt.x = \_w/10 + (j\*120) leveltxt .y = \_h-100+ (i\*90) leveltxt:setTextColor (50/255, 50/255, 0/255) sceneGroup:insert(leveltxt) levelImg.destination = "bl"..tostring(tablePlace) levelImg:addEventListener("tap", buttonHit) end end end function scene:show( event ) local sceneGroup = self.view local phase = event.phase if phase == "will" then local sceneGroup = self.view Btlvls = nil Btlvls = loadsave.loadTable("btLevels.json") end if phase == "did" then end end function scene:hide( event ) local sceneGroup = self.view local phase = event.phase if event.phase == "will" then local sceneGroup = self.view banner:hide() elseif phase == "did" then end end function scene:destroy( event ) local sceneGroup = self.view end ------------------------------------------------------------------------------- -- Listener setup scene:addEventListener( "create", scene ) scene:addEventListener( "show", scene ) scene:addEventListener( "hide", scene ) scene:addEventListener( "destroy", scene ) ------------------------------------------------------------------------------- return scene

in the levelFunctions.lua

loadsave = require("loadsave") Bt = loadsave.loadTable("btLevels.json") ud = loadsave.loadTable("udLevels.json") btLevel = Bt.btlevels udLevel = ud.udlevels function BtSaveLevel(ln,n) btLevel[ln] = n loadsave.saveTable(Bt,"btLevels.json",system.DocumentsDirectory) end function udSaveLevel(ln,n) udLevel[ln] = n loadsave.saveTable(ud,"udLevels.json",system.DocumentsDirectory) end

When i complete an level i do this in game win function - 

BtSaveLevel(2,2)

Although its works but it shows the completed level icon on the level select screen only after i restart the game…

I need a way so that i can show the completed level icon in the same session…

Please help Corona Geniuses … :slight_smile:

When you return to a scene, the scene:create function no longer executes (unless you remove it).  The code to update the level images should be in scene:show, in the “will” section.  Looking at your scene:show block, I do not see any code to update these images.

Also, this has nothing to do with your current issue, but I would consider moving the addEventListener out of the create function.  Adds should be placed in scene:show and removes should be placed in scene"hide.  This will rpevent any event listener bleed through to other scenes, which may cause issues as your game is being played.  I also do not see a removeEventListener call anywhere, so you could potentially be calling adds over and over again, which will cause problems.

Hope this helps,

–John

I’ve got this i called composer.removeScene() in my scene change function.

And John thanks for your reply

When you return to a scene, the scene:create function no longer executes (unless you remove it).  The code to update the level images should be in scene:show, in the “will” section.  Looking at your scene:show block, I do not see any code to update these images.

Also, this has nothing to do with your current issue, but I would consider moving the addEventListener out of the create function.  Adds should be placed in scene:show and removes should be placed in scene"hide.  This will rpevent any event listener bleed through to other scenes, which may cause issues as your game is being played.  I also do not see a removeEventListener call anywhere, so you could potentially be calling adds over and over again, which will cause problems.

Hope this helps,

–John

I’ve got this i called composer.removeScene() in my scene change function.

And John thanks for your reply