Mystic Error between scenes

Hey there, I implemented next logic in my game(see attached file): 

  1. I have there scene for RESTART lvl where I remove previous scene and go back to previous scene.

  2. I have scene with transition to NEXT lvl which transfers us to next scene and deletes previous scene

  3. And also here are 2 GAME scenes where directly actions are happening. In each scene I have var which responsible for counting TIME and ATTEMPTS. So if I’d have success in passing level I’d get this var, write it and make it zero(var=0) .

So… When I enter in first scene all works good, when I restart it - also works good. Var writes and rewrites. But when I go to next level it begins to work only once and after restarting var doesn’t rewrites, see attached file with logs.

There we can see On first screen_log three numbers where 1st is TIMER, 2nd - ATTEMPTS,3d - Sum of them and after them string"it was restart scene". 

On second screen I have 3 numbers but they weren’t rewrites and moreover it feels like vars don’t redefine during restart on 2nd game scene. So how could it be?? I can’t find where trouble is. I need TIME var be rewritten every restart. Also I delete previous scenes.

And If I choose level from LEVEL MENU it also works good. Trouble is appearing ONLY on transition to NEXT lvl

I’m not sure how helpful this will be, but let me give you a couple of things to think about.

You need a restart scene because you can’t remove the scene you’re in and that’s, from a programmer perspective the easiest way to reset a scene. This is working for you. If you need to go to the next level, you could just go there and bypass your nextlevel.lua scene completely. 

Next, work you do in the main chunk (and I wish you would have used copy/paste to post code that I could copy from – can’t do that with screen shots), like:

local nextscn = prevscn + 1

Only executes when the module is first loaded. If this scene has been loaded once, that code will not execute again until the scene is explicitly un-required. composer.removeScene() does this. It looks like you are removing it (twice actually - since scene:show() gets called twice).  But something may be wrong preventing it from getting called.

The scene:create() function also may not get called if the scene’s view group already exists, but in this case it should. I’m concerned that you’re trying to change scenes in 1 millisecond. If there are any transitions that could be too quick. You could be in another scene while that scene is still trying to process it’s scene events.

Rob

Rob, thank you for answering.

Additional scenes between main game scenes just only for deleting previous scenes, there are no transitions there. I tried to avoid additional gotonextlvl scene and go straight to game scene but the problem is still the same. It error looks strange because of scene recreates but local vars doesn’t redefine. I attempt to describe my problem more intelligible.

local speed = 0 print(speed) -- try to print value of speed var but nothing happens local path1 = system.pathForFile("2attempts.txt", system.DocumentsDirectory) -- some numbers that writes and rewrites for saving user data local attemptsTxt = io.open(path1) if attemptsTxt == nil then attemptsTxt = io.open(path1, "w") attemptsTxt:write("1") io.close(attemptsTxt) else attemptsTxt = io.open(path1, "r") local att = attemptsTxt:read() att = att+1 io.close(attemptsTxt) attemptsTxt = io.open(path1, "w") attemptsTxt:write(tostring(att)) io.close(attemptsTxt) end attemptsTxt = io.open(path1, "r") local attempts = attemptsTxt:read() io.close(attemptsTxt) print(attempts) -- nothing happens too

here you can see that I try to print the value but… lua doesn’t even enter there (after restart scene) and doesn’t redefine local vars, therefore It’s the main problem - I can’t rewrite vars. But at the same time, scene creates without any conflicts.

Thus, main problem  is - scene are creating but local vars aren’t redefining in main chunk. 

You need to remove that scene before you go to it. If the code in the main chunk isn’t executing, its because that module has been previously loaded.

Rob

Yeah, I know it but it doesn’t want be removed and I don’t know why. There is a code in restart scene

local composer = require("composer") local scene = composer.newScene() local sn = composer.getSceneName("previous") print(sn) -- here it prints right scene print("it was restart scene") local function goto() composer.gotoScene(sn) end function scene:create() timer.performWithDelay(1, goto) end function scene:show() composer.removeScene(sn) -- but here, as I understood, it doesn't want to remove. end function scene:hide() end function scene:destroy() end scene:addEventListener("create", scene) scene:addEventListener("show", scene) scene:addEventListener("hide", scene) scene:addEventListener("destoy", scene) return scene

May be 1 milisecond is too little for deleting scene? or I need to delete composer module?

It wouldn’t hurt to give it more time since you’re potentially removing scenes while they are still trying to execute.

I don’t know… I remove restart scene, remove previous scenes, tried to increase timer between scenes, tried to delete composer module and some thigs I used to. Can’t find a problem… But noticed 1 thing, I added a couple of strings to code for checking loaded modules. And in 1st game scene corona prints tables of loaded modules but in 2nd scene writes “nil”(see screenshot)

local composer = require("composer") local scene = composer.newScene() local sn = composer.getSceneName("previous") print(sn) print("it was restart scene") local function goto() composer.gotoScene(sn) end function scene:create() print(package.loaded[sn]) -- writes NIL in 2nd scene timer.performWithDelay(1, goto) end function scene:show() composer.removeScene(sn) end function scene:hide() end function scene:destroy() print(package.loaded[sn]) -- writes NIL in 2nd scene print("restart destroyed") end scene:addEventListener("create", scene) scene:addEventListener("show", scene) scene:addEventListener("hide", scene) scene:addEventListener("destroy", scene) return scene

Does it mean that corona can’t find a required module(specifically, 2nd scene) ? Can’t find but can recreate…

There is one more thing - if I go to menu and then re-enter to necessary scene, restart begins to work good but only until transition to next scene…

Everything point out that some parts of 2nd scene still loaded and there is some mystical condition responsible for deleting it =)

Actually I don’t know how it works, but I resolved by next way: move my “composer.removeScene(“previousNameScene”)” in main chunk in restart scene and then doubled removing of each scene. And now it works correct!

I’m not sure how helpful this will be, but let me give you a couple of things to think about.

You need a restart scene because you can’t remove the scene you’re in and that’s, from a programmer perspective the easiest way to reset a scene. This is working for you. If you need to go to the next level, you could just go there and bypass your nextlevel.lua scene completely. 

Next, work you do in the main chunk (and I wish you would have used copy/paste to post code that I could copy from – can’t do that with screen shots), like:

local nextscn = prevscn + 1

Only executes when the module is first loaded. If this scene has been loaded once, that code will not execute again until the scene is explicitly un-required. composer.removeScene() does this. It looks like you are removing it (twice actually - since scene:show() gets called twice).  But something may be wrong preventing it from getting called.

The scene:create() function also may not get called if the scene’s view group already exists, but in this case it should. I’m concerned that you’re trying to change scenes in 1 millisecond. If there are any transitions that could be too quick. You could be in another scene while that scene is still trying to process it’s scene events.

Rob

Rob, thank you for answering.

Additional scenes between main game scenes just only for deleting previous scenes, there are no transitions there. I tried to avoid additional gotonextlvl scene and go straight to game scene but the problem is still the same. It error looks strange because of scene recreates but local vars doesn’t redefine. I attempt to describe my problem more intelligible.

local speed = 0 print(speed) -- try to print value of speed var but nothing happens local path1 = system.pathForFile("2attempts.txt", system.DocumentsDirectory) -- some numbers that writes and rewrites for saving user data local attemptsTxt = io.open(path1) if attemptsTxt == nil then attemptsTxt = io.open(path1, "w") attemptsTxt:write("1") io.close(attemptsTxt) else attemptsTxt = io.open(path1, "r") local att = attemptsTxt:read() att = att+1 io.close(attemptsTxt) attemptsTxt = io.open(path1, "w") attemptsTxt:write(tostring(att)) io.close(attemptsTxt) end attemptsTxt = io.open(path1, "r") local attempts = attemptsTxt:read() io.close(attemptsTxt) print(attempts) -- nothing happens too

here you can see that I try to print the value but… lua doesn’t even enter there (after restart scene) and doesn’t redefine local vars, therefore It’s the main problem - I can’t rewrite vars. But at the same time, scene creates without any conflicts.

Thus, main problem  is - scene are creating but local vars aren’t redefining in main chunk. 

You need to remove that scene before you go to it. If the code in the main chunk isn’t executing, its because that module has been previously loaded.

Rob

Yeah, I know it but it doesn’t want be removed and I don’t know why. There is a code in restart scene

local composer = require("composer") local scene = composer.newScene() local sn = composer.getSceneName("previous") print(sn) -- here it prints right scene print("it was restart scene") local function goto() composer.gotoScene(sn) end function scene:create() timer.performWithDelay(1, goto) end function scene:show() composer.removeScene(sn) -- but here, as I understood, it doesn't want to remove. end function scene:hide() end function scene:destroy() end scene:addEventListener("create", scene) scene:addEventListener("show", scene) scene:addEventListener("hide", scene) scene:addEventListener("destoy", scene) return scene

May be 1 milisecond is too little for deleting scene? or I need to delete composer module?

It wouldn’t hurt to give it more time since you’re potentially removing scenes while they are still trying to execute.

I don’t know… I remove restart scene, remove previous scenes, tried to increase timer between scenes, tried to delete composer module and some thigs I used to. Can’t find a problem… But noticed 1 thing, I added a couple of strings to code for checking loaded modules. And in 1st game scene corona prints tables of loaded modules but in 2nd scene writes “nil”(see screenshot)

local composer = require("composer") local scene = composer.newScene() local sn = composer.getSceneName("previous") print(sn) print("it was restart scene") local function goto() composer.gotoScene(sn) end function scene:create() print(package.loaded[sn]) -- writes NIL in 2nd scene timer.performWithDelay(1, goto) end function scene:show() composer.removeScene(sn) end function scene:hide() end function scene:destroy() print(package.loaded[sn]) -- writes NIL in 2nd scene print("restart destroyed") end scene:addEventListener("create", scene) scene:addEventListener("show", scene) scene:addEventListener("hide", scene) scene:addEventListener("destroy", scene) return scene

Does it mean that corona can’t find a required module(specifically, 2nd scene) ? Can’t find but can recreate…

There is one more thing - if I go to menu and then re-enter to necessary scene, restart begins to work good but only until transition to next scene…

Everything point out that some parts of 2nd scene still loaded and there is some mystical condition responsible for deleting it =)

Actually I don’t know how it works, but I resolved by next way: move my “composer.removeScene(“previousNameScene”)” in main chunk in restart scene and then doubled removing of each scene. And now it works correct!