need help with the storyboard api

Hi there,

since i added the storyboard api to my game, i’ve run into a lot of problems.

Simply put, i don’t even know where to start - so here’s a github link to the source:

https://github.com/paranoiax/storyboard-test

Whenever i try to unload (purge / remove)  a scene i run into a lot of errors.

I fixed some of them on my own but now i really have no clue what to do any further.

The idea is:

If you lose the game (by pointing the “player” into one of the red walls, you lose and will be brought back to the main menu. If you then press “Play Now”, the game should start again. Unfortunately i encounter quite some errors while doing so.

Please anyone help me out.

best regards.

Here are a few things to think about.

1.  If you are doing removeScene() you don’t need to do purgeScene().  Calling them back to back shouldn’t cause issues, but…

  1. Your likely culprit is this:

Runtime:addEventListener(“collision”, onCollision)

Runtime:addEventListener(“touch”, onTouch)

Runtime:addEventListener(“enterFrame”, update)

in your createScene() function.  You never remove them.  So once you leave your screen, your update function is still trying to run and when you call removeScene() that code gets removed but the enterFrame is still trying to exit.  I also am not 100% sure the physics bodies get removed by removeScene() so they could still be interacting and triggering your onCollision function which, also got removed.  The first one for certain, and very possibly the 2nd one would likely cause a segment violation crashing the simulator.    As a side note you typically turn on event listeners that run in the background like the collision and enterFrame during the enterScene() function.  The touch handler is okay to have in createScene().  That way you make sure to remove those two during exitScene().

Finally in your collision handler, you call storyboard.gotoScene().  There are somethings you shouldn’t do while collisions are happening (like remove the objects).  It might be safer to use a timer.performWithDelay() with a short 10 millisecond delay to let the collision function finish before you try to leave the scene.

I followed your advice and everything is now working as expected. Hats off to you, sir. Thanks a lot. :slight_smile:

Well, another problem just occured.

I’ll try my best to describe it:

In main.lua i set the following variable:

storyboard.currentLevel = 1  

in game.lua i use the following function to load my “mapdata”:

local map = require ('level'..storyboard.currentLevel) for i,v in pairs{sensors=addSensor, walls=addWall} do     for \_, data in ipairs(map[i]) do         v(unpack(data))     end end  

(the important part should only be the first line here)

Now whenever you lose or win the game the following function gets called:

local function restart()     if fileExists('level'..storyboard.currentLevel + 1 .. ".lua") then         storyboard.currentLevel = storyboard.currentLevel + 1     else         storyboard.currentLevel = 1     end     storyboard.gotoScene( "menu", "zoomInOutFade", 500 ) end

This leads to to next level by triggering the following function in menu.lua:

local function onPlayBtnRelease()          -- go to level1.lua scene     storyboard.gotoScene( "game", "zoomInOutFade", 500)          return true    -- indicates successful touch end  

On the Simulator everything works as expected. The game iterates through all the levels (16) and when you finished level 16, it goes back to level 1.

However if I do test on Android Device (HTC ONE if that matters), it behaves kinda strange.

Some levels are loaded twice, and after some levels it occasionally resets to level 1. As I said this only occurs on device, not in the simulator.

The Github link is a different one this time:

https://github.com/paranoiax/Instablix

Hope you can help me out again.

best regards.

have you done some print statements to make sure what your values are?

Have you made sure your fileExists() function doesn’t have any issues or your files don’t have a case sensitivity problem?

my fileExists() function is this:

local function fileExists(fileName, base)     assert(fileName, "fileName is missing")     local base = base or system.ResourceDirectory     local filePath = system.pathForFile( fileName, base )     local exists = false     if (filePath) then         local fileHandle = io.open( filePath, "r" )     if (fileHandle) then         exists = true         io.close(fileHandle)     end end  

All files are lowercase. I checked with print statements but as I said everything works fine in the simulator.

EDIT: I connected the device to my PC. According to the debuging console, the variable “currentLevel” indeed seems to reset after a while. Unfortunately this doesn’t help me at all. It runs just fine on the simulator. This inconsistency is really annoying.

are you maybe getting a low memory warning purging something that you’re not expecting? 

Can you take the value out of the storyboard table and put it into a “global” (using the mydata.lua trick instead of it actually being global)?

I’m not getting a low memory warning and even the mydata.lua trick (from “Goodbye Globals”, i assume?!) doesn’t make a difference, not even a global. Updated source on github again: https://github.com/paranoiax/Instablix

I can’t explain why this works in the sim and not on device.  But you’re loading the level in createScene() an createScene() is executed when the scene’s view is destroyed. 

I would consider putting a storyboard.removeScene(“game”) in your restart function in menu.lua

same outcome, i can’t think about anything wrong with my code…

I’m really confused about this code:

local function fileExists(fileName, base)     assert(fileName, "fileName is missing")     local base = base or system.ResourceDirectory     local filePath = system.pathForFile( fileName, base )     local exists = false       if (filePath) then -- file may exist. won't know until you open it         local fileHandle = io.open( filePath, "r" )     if (fileHandle) then -- nil if no file found         exists = true         io.close(fileHandle)     end end

I encountered that “scene loaded twice” thing as well.

As Rob stated above, wrapping the gotoScene inside a timer fix that (at least on my app). I did like

[lua]timer.performWithDelay(300, function() storyboard.gotoScene( “menu”, “zoomInOutFade”, 500 ) end, 1)[/lua]

Try to change that 300 to different number. I think it will vary based on your memory device ?

Here are a few things to think about.

1.  If you are doing removeScene() you don’t need to do purgeScene().  Calling them back to back shouldn’t cause issues, but…

  1. Your likely culprit is this:

Runtime:addEventListener(“collision”, onCollision)

Runtime:addEventListener(“touch”, onTouch)

Runtime:addEventListener(“enterFrame”, update)

in your createScene() function.  You never remove them.  So once you leave your screen, your update function is still trying to run and when you call removeScene() that code gets removed but the enterFrame is still trying to exit.  I also am not 100% sure the physics bodies get removed by removeScene() so they could still be interacting and triggering your onCollision function which, also got removed.  The first one for certain, and very possibly the 2nd one would likely cause a segment violation crashing the simulator.    As a side note you typically turn on event listeners that run in the background like the collision and enterFrame during the enterScene() function.  The touch handler is okay to have in createScene().  That way you make sure to remove those two during exitScene().

Finally in your collision handler, you call storyboard.gotoScene().  There are somethings you shouldn’t do while collisions are happening (like remove the objects).  It might be safer to use a timer.performWithDelay() with a short 10 millisecond delay to let the collision function finish before you try to leave the scene.

I followed your advice and everything is now working as expected. Hats off to you, sir. Thanks a lot. :slight_smile:

Well, another problem just occured.

I’ll try my best to describe it:

In main.lua i set the following variable:

storyboard.currentLevel = 1  

in game.lua i use the following function to load my “mapdata”:

local map = require ('level'..storyboard.currentLevel) for i,v in pairs{sensors=addSensor, walls=addWall} do     for \_, data in ipairs(map[i]) do         v(unpack(data))     end end  

(the important part should only be the first line here)

Now whenever you lose or win the game the following function gets called:

local function restart()     if fileExists('level'..storyboard.currentLevel + 1 .. ".lua") then         storyboard.currentLevel = storyboard.currentLevel + 1     else         storyboard.currentLevel = 1     end     storyboard.gotoScene( "menu", "zoomInOutFade", 500 ) end

This leads to to next level by triggering the following function in menu.lua:

local function onPlayBtnRelease()          -- go to level1.lua scene     storyboard.gotoScene( "game", "zoomInOutFade", 500)          return true    -- indicates successful touch end  

On the Simulator everything works as expected. The game iterates through all the levels (16) and when you finished level 16, it goes back to level 1.

However if I do test on Android Device (HTC ONE if that matters), it behaves kinda strange.

Some levels are loaded twice, and after some levels it occasionally resets to level 1. As I said this only occurs on device, not in the simulator.

The Github link is a different one this time:

https://github.com/paranoiax/Instablix

Hope you can help me out again.

best regards.

have you done some print statements to make sure what your values are?

Have you made sure your fileExists() function doesn’t have any issues or your files don’t have a case sensitivity problem?

my fileExists() function is this:

local function fileExists(fileName, base)     assert(fileName, "fileName is missing")     local base = base or system.ResourceDirectory     local filePath = system.pathForFile( fileName, base )     local exists = false     if (filePath) then         local fileHandle = io.open( filePath, "r" )     if (fileHandle) then         exists = true         io.close(fileHandle)     end end  

All files are lowercase. I checked with print statements but as I said everything works fine in the simulator.

EDIT: I connected the device to my PC. According to the debuging console, the variable “currentLevel” indeed seems to reset after a while. Unfortunately this doesn’t help me at all. It runs just fine on the simulator. This inconsistency is really annoying.

are you maybe getting a low memory warning purging something that you’re not expecting? 

Can you take the value out of the storyboard table and put it into a “global” (using the mydata.lua trick instead of it actually being global)?

I’m not getting a low memory warning and even the mydata.lua trick (from “Goodbye Globals”, i assume?!) doesn’t make a difference, not even a global. Updated source on github again: https://github.com/paranoiax/Instablix

I can’t explain why this works in the sim and not on device.  But you’re loading the level in createScene() an createScene() is executed when the scene’s view is destroyed. 

I would consider putting a storyboard.removeScene(“game”) in your restart function in menu.lua