Physics and storyboard

Howdy,

I’ve done a lot of searching on the forum but I still havent been able to find out the answer to this problem.

I am using storyboard and the physics engine.  main.lua points to “level1” at which point i declare the physics library at the top before enterScene:

local storyboard = require( “storyboard” )
local scene = storyboard.newScene()

local physics = require (“physics”)
physics.setDrawMode(“hybrid”)
physics.start()

physics.setGravity(0,0)

tileMap = require (“tiles”)
level = require (“levelData”)

enterScene starts here calling functions from within “tiles” and “leveldata”

I then go on to add functions, graphics, level data etc and everything in “level1” works fine, ie applying physics forces to a character to make them move about.

after a certain event occurs storyboard jumps out to “level2”.  In exitScene I remove all event listeners and presumably the display groups are nilled upon exit via storyboard API.  The next level data all loads correctly and i can see any objects I declare either prior to create Scene in level2 or within it.

The physics engine no longer works in this scene - even the hybrid or debug view is not active.  I have tried several solutions - declaring physics and turning it on in main.lua as a global, i have copied and pasted exact same code into “level2.lua” as the code above but it refuses to work.  I have tried stopping and starting the engine in various places throughout the storyboard scenes.

In one configuration when i push a button that worked in level 1 which would apply a physics force to my character i get the message: attempt to call method: set linearimpulse which is a nil value.

Now i guess I’m missing something fundamental in how storyboard treats physics or libraries in general but I cannot figure this one out.  It also seems to crash the corona simulator which doesnt help!

I can post the code but i cant see it being relevant to this issue and there is a lot of it! 

Any advice massively appreciated!

Thanks

Iain
 

Im pretty new to corona too.

But if you just did

require (“physics”)

and not 

local physics = require (“physics”)

does that solve you issue?

Hi, thanks for the quick response!  I replaced it with what you suggested but this hasnt solved the problem.  I have a feeling the issue lies in how I am scoping my overall program but despite tinkering with it for the last hour havent got any closer to solving it. 

If I have a function in a module which i require at the start of a scene that refers to code within the scene, would I need to nil out those variables in exit scene or will just unrequiring the module sort this?  ie

  
    for i=game.numChildren,1,-1 do
               display.remove( game[i] ) ; game[i] = nil
        end

    for i=group.numChildren,1,-1 do
               display.remove( group[i] ) ; group[i] = nil
        end

    package.loaded[“tiles”] = nil
    package.loaded[“levelData”] = nil
    tileMap = nil
    level = nil
 

I have declared lot of local functions after create scene and before enter scene that are called from within the enterScene section.  Do I have to manually remove or nil out these functions before I change scenes or will corona clear these up for me?

Hi guys,

Remember that you must require the physics library (locally) in each scene that uses it, and be sure to scope it properly. You shouldn’t need to manually dispose of functions that are local to the scene… if these are, in fact, 100% local and not referenced elsewhere, they will be cleaned up by the Lua garbage collector when it’s able to.

Take care,

Brent Sorrentino

HI Brent,  thats what I was doing originally.  I have this code in both level1.lua and level2.lua before i start the createScene function of storyboard.  I have tried stopping and starting physics in the exit scene of level1 but still no joy.  Its definitly unloading the physics engine as level2 scene is no longer shown in hybrid or debug mode and I cant seem to restart it.

any help you can offer would be great!

code I use prior to create scene:

local storyboard = require( “storyboard” )
local scene = storyboard.newScene()

local physics = require (“physics”)
physics.setDrawMode(“hybrid”)
physics.start()
physics.setGravity(0,0)

tileMap = require (“tiles”)

level = require (“levelData”)

enterScene starts here calling functions from within “tiles” and “leveldata”

then level 2 is simply the same but with require files different:

local storyboard = require( “storyboard” )
local scene = storyboard.newScene()

local physics = require (“physics”)
physics.setDrawMode(“hybrid”)
physics.start()
physics.setGravity(0,0)

tileMap = require (“tiles2”)
level = require (“levelData2”)

enterScene starts here calling functions from within “tiles” and “leveldata”

leveldata file contains some physics body shapes which I pull into each level as well as tables relating to character speech.  These are contained within a function within each module.  I can put a stripped down version of the program up for review if thats helpful however I don’t think its a coding issue as level 1 works perfectly, more like a problem with my understanding of physics and storyboard!

HI Brent,  thats what I was doing originally.  I have this code in both level1.lua and level2.lua before i start the createScene function of storyboard.  I have tried stopping and starting physics in the exit scene of level1 but still no joy.  Its definitly unloading the physics engine as level2 scene is no longer shown in hybrid or debug mode and I cant seem to restart it.

any help you can offer would be great!

code I use prior to create scene:

local storyboard = require( “storyboard” )
local scene = storyboard.newScene()

local physics = require (“physics”)
physics.setDrawMode(“hybrid”)
physics.start()
physics.setGravity(0,0)

tileMap = require (“tiles”)

level = require (“levelData”)

enterScene starts here calling functions from within “tiles” and “leveldata”

then level 2 is simply the same but with require files different:

local storyboard = require( “storyboard” )
local scene = storyboard.newScene()

local physics = require (“physics”)
physics.setDrawMode(“hybrid”)
physics.start()
physics.setGravity(0,0)

tileMap = require (“tiles2”)
level = require (“levelData2”)

enterScene starts here calling functions from within “tiles” and “leveldata”

leveldata file contains some physics body shapes which I pull into each level as well as tables relating to character speech.  These are contained within a function within each module.  I can put a stripped down version of the program up for review if thats helpful however I don’t think its a coding issue as level 1 works perfectly, more like a problem with my understanding of physics and storyboard!

Hi @iainsanderson0504,

At a first look, the modules you’re calling like “levelData2” probably need to have some reference to the physics library as well. If you’re creating the bodies in those actual modules, the physics library needs to be “known” within them… OR you need to return the object data back to the scene which called it, where the physics library is already known, and then create the bodies using that data.

Hope this helps,

Brent

Hmmm interesting.  Theres only functions within those modules which are called in createScene(). They do create physics bodies, however its the same when level 1 is loaded so I dont understand how it works in the first scene but not the second…  Also I have enabled the physics engine before I call those modules so won’t those functions be aware the physics is on?  I’ll try returning the data from those functions but I’m not sure thats the answer as it already returns the right data in level1.  I suppose I could also try enabling the physics engine from within those modules and not the level1 module… heh, all fun and games!

cheers for advice, will keep trying!

Unless you are explicitly calling storyboard.removeScene(“level1”) in one of your other scenes, any code sitting in level1’s main chunk will never re-execute.  You must either removeScene() before going to level1 or in level1, put all of that code in “enterScene” or “willEnterScene”.

OK so i changed main.lua to point to level 2.lua. (bypassing level1)  and everything works perfectly.  The problem is the transition between scenes (level 1 to level2) and somehow losing the physics engine along the way. I tried putting everything (objects, functions, require physics as well the functions from my 2 modules (tiles and leveldata) into the enter scene function and lo and behold it works fine.  So whats the deal with create scene not working when you move between scenes but works ok when you drop in from main.lua?

cheers for advice, at least its working now even if im not following why!

Can you post your code inside of [code] and [/code] tags?  Limit it to your main chunk and the four main storyboard functions and anywhere you are starting and stopping physics.

well i’m still working on this one but i think i’ve solved it now thanks to your guidance.  The willEnterscene needs to be used as the createScene doesnt seem to be recalled upon returning to the original scene. Once i’ve applied the willenterscene to this app then i’ll drop in with a further update.  I tested this with a smaller app of mine and that solved the storyboard issues there as well.

The biggest problem i had with is that the storyboard template i downloaded didnt include the willenterscene.  So i just followed instructions of putting display objects in createScene and everything else in enterScene.  Which didnt work.  Cheers for all advice so far, slowly getting the hang of it now!

More news as it happens…

all solved now - i declare and turn on physics in main.lua and use the willEnterScene to load images and enterScene to fire event listeners. I don’t really touch the physics after that at all - Is there a need to stop and start physics between scenes? Doesnt seem to cause any issues…

Just converted a program that was using director to using storyboard instead so feeling pretty happy now :slight_smile:

Im pretty new to corona too.

But if you just did

require (“physics”)

and not 

local physics = require (“physics”)

does that solve you issue?

Hi, thanks for the quick response!  I replaced it with what you suggested but this hasnt solved the problem.  I have a feeling the issue lies in how I am scoping my overall program but despite tinkering with it for the last hour havent got any closer to solving it. 

If I have a function in a module which i require at the start of a scene that refers to code within the scene, would I need to nil out those variables in exit scene or will just unrequiring the module sort this?  ie

  
    for i=game.numChildren,1,-1 do
               display.remove( game[i] ) ; game[i] = nil
        end

    for i=group.numChildren,1,-1 do
               display.remove( group[i] ) ; group[i] = nil
        end

    package.loaded[“tiles”] = nil
    package.loaded[“levelData”] = nil
    tileMap = nil
    level = nil
 

I have declared lot of local functions after create scene and before enter scene that are called from within the enterScene section.  Do I have to manually remove or nil out these functions before I change scenes or will corona clear these up for me?

Hi guys,

Remember that you must require the physics library (locally) in each scene that uses it, and be sure to scope it properly. You shouldn’t need to manually dispose of functions that are local to the scene… if these are, in fact, 100% local and not referenced elsewhere, they will be cleaned up by the Lua garbage collector when it’s able to.

Take care,

Brent Sorrentino

HI Brent,  thats what I was doing originally.  I have this code in both level1.lua and level2.lua before i start the createScene function of storyboard.  I have tried stopping and starting physics in the exit scene of level1 but still no joy.  Its definitly unloading the physics engine as level2 scene is no longer shown in hybrid or debug mode and I cant seem to restart it.

any help you can offer would be great!

code I use prior to create scene:

local storyboard = require( “storyboard” )
local scene = storyboard.newScene()

local physics = require (“physics”)
physics.setDrawMode(“hybrid”)
physics.start()
physics.setGravity(0,0)

tileMap = require (“tiles”)

level = require (“levelData”)

enterScene starts here calling functions from within “tiles” and “leveldata”

then level 2 is simply the same but with require files different:

local storyboard = require( “storyboard” )
local scene = storyboard.newScene()

local physics = require (“physics”)
physics.setDrawMode(“hybrid”)
physics.start()
physics.setGravity(0,0)

tileMap = require (“tiles2”)
level = require (“levelData2”)

enterScene starts here calling functions from within “tiles” and “leveldata”

leveldata file contains some physics body shapes which I pull into each level as well as tables relating to character speech.  These are contained within a function within each module.  I can put a stripped down version of the program up for review if thats helpful however I don’t think its a coding issue as level 1 works perfectly, more like a problem with my understanding of physics and storyboard!

HI Brent,  thats what I was doing originally.  I have this code in both level1.lua and level2.lua before i start the createScene function of storyboard.  I have tried stopping and starting physics in the exit scene of level1 but still no joy.  Its definitly unloading the physics engine as level2 scene is no longer shown in hybrid or debug mode and I cant seem to restart it.

any help you can offer would be great!

code I use prior to create scene:

local storyboard = require( “storyboard” )
local scene = storyboard.newScene()

local physics = require (“physics”)
physics.setDrawMode(“hybrid”)
physics.start()
physics.setGravity(0,0)

tileMap = require (“tiles”)

level = require (“levelData”)

enterScene starts here calling functions from within “tiles” and “leveldata”

then level 2 is simply the same but with require files different:

local storyboard = require( “storyboard” )
local scene = storyboard.newScene()

local physics = require (“physics”)
physics.setDrawMode(“hybrid”)
physics.start()
physics.setGravity(0,0)

tileMap = require (“tiles2”)
level = require (“levelData2”)

enterScene starts here calling functions from within “tiles” and “leveldata”

leveldata file contains some physics body shapes which I pull into each level as well as tables relating to character speech.  These are contained within a function within each module.  I can put a stripped down version of the program up for review if thats helpful however I don’t think its a coding issue as level 1 works perfectly, more like a problem with my understanding of physics and storyboard!

Hi @iainsanderson0504,

At a first look, the modules you’re calling like “levelData2” probably need to have some reference to the physics library as well. If you’re creating the bodies in those actual modules, the physics library needs to be “known” within them… OR you need to return the object data back to the scene which called it, where the physics library is already known, and then create the bodies using that data.

Hope this helps,

Brent

Hmmm interesting.  Theres only functions within those modules which are called in createScene(). They do create physics bodies, however its the same when level 1 is loaded so I dont understand how it works in the first scene but not the second…  Also I have enabled the physics engine before I call those modules so won’t those functions be aware the physics is on?  I’ll try returning the data from those functions but I’m not sure thats the answer as it already returns the right data in level1.  I suppose I could also try enabling the physics engine from within those modules and not the level1 module… heh, all fun and games!

cheers for advice, will keep trying!