External Modules reloading everytime I re-enter a scene. URGENT. Deadline related.

Hi all, I’m using Physics Editor for creating some complex objects. So, at the top of my StoryBoard scene, I require the data as an external module, like this:

local physicsData = (require "shapedef4").physicsData(1.0)

Where shapedef4 is the lua file and physicsData is it’s table, containing various physics bodies, bits and masks.

After a gameOver event (the scene swaps back to the main menu) I have an interim scene that removes the main game scene via storyboard.removeScene(“gameScene”).

The problem I seem to be having, is everytime I re-enter the gameScene, the physics bodies (that don’t seem to have been removed in the first place) keep reloading on top of each other, so the more I re-enter the gameScene the more the game slows down.

I’ve tried every method I can think of to destroy the scene, including removing the bodies (and their listeners) manually, deactivating the bodies and even tried unloading the external module (which isn’t included as a storyboard asset. So, like this:

package.loaded["shapedef4"] = nil

I must be missing something… The physics bodies aren’t hanging around when I swap to ‘menu’, but they still appear to be in the gameScene after I re-enter.

This one’s really killing me. Any idea?

How are you removing the bodies when the scene is exited?

storyboard.removeScene() does a: package.loaded[“shapedef4”] = nil for you.

Are your objects being added into the storyboard scene correctly?  Are you pausing/stopping physics before you leave the scene?

Rob

Hi guys, thanks for the reply… I’m adding the body like this:

function scene:createScene( event ) local group = self.view thing = display.newImageRect( "images/thing.png", 474, 474 ) thing.x = display.contentCenterX thing.y = display.contentCenterY physics.addBody( thing, "dynamic", physicsData:get("thing") ) end

And including that in a display group

I’ve tried removing the body AND making it innactive… I stop.physics in my interim scene, but none of those approaches is working… Would anyone fancy taking a look at the code?

Thanks

**Update

It’s not the module that’s causing the ‘slow down’… I’ve just removed it and I’m still getting the same problem. The more I re-enter the scene, the more things slow down. Weird.

FWIW I’ve always found that neither storyboard nor composer do a reliable job of removing objects and variables from memory. Your reference to detrimental performance over time would suggest your physics bodies aren’t being removed. I’d suggest removing your physics bodies manually when the scene exits.

I’ve tried removing everything manually, like:

physics.removeBody( ring4 ) physics.removeBody( ball ) physics.removeBody(sensor1) physics.removeBody(sensor2) physics.removeBody(sensor3) physics.removeBody(sensor4) physics.removeBody(scoreCircle) sensor1:removeEventListener( "collision", sensor1 ) sensor2:removeEventListener( "collision", sensor2 ) sensor3:removeEventListener( "collision", sensor3 ) sensor4:removeEventListener( "collision", sensor4 ) Runtime:removeEventListener("enterFrame", onEnterFrame1) ball:removeEventListener( "collision", ball ) rotateRight:removeEventListener( "touch", turnRight ) scoreCircle:removeEventListener( "collision", scoreCircle )

That’s pretty much everything in the scene… I also call removeScene, in an interim level… The gameover dialogue is triggered by a collision, so I’ve even used timerPerformWithDelay before the exitScene code is executed (just in case it was a collision issue)… No luck.

As you can see, I have a ball in the scene… It has a linear velocity applied to it, it also slows down in a cyclical manner (I’ve tried swapping for linearImpulse with a runtime listener, but still didn’t fix it).

I’m beginning to wonder if it’s a simulator problem. Surely not?

It must be something to do with my destoy scene code… Nightmare.

Strangely enough Alex, I think you may have helped with a similar problem before:

http://forums.coronalabs.com/topic/41933-memory-leak-or-something-else/

What can I say, I get around :wink:

Using removeBody doesn’t actually remove the reference or the object, it just removes the physics body which is only part of the battle. To confirm, you can stick in print statements for your objects to see if they are being niled out.

Removing an object from memory necessitates using the display.remove/removeSel() APIs, and then niling the reference to the variable:

Ex:
Thing = display.newImage(“onething.png”)

– sometime later when no longer needing Thing object:

Thing:removeSelf()
Thing=nil

Sorry for lack of formatting, I’m mobile right now. Let me know if that makes sense and it helps resolve the issue.

I really thought Storyboard would handle the objects held within the display group (especially as I’m calling removeScene… I’ll have a play around with removing the objects manually, but I’m beginning to wonder if that’s the problem at all.

I have a ‘return to game’ button on my menu, like the problem ‘linked’ above… And the frame-rate is steadily slowing down in exactly the same way. I’ve just tried running the code without ANY runtime listeners, but that hasn’t fixed it. This is a weird one.

Hi,

Is thing local?

If not try,

local  thing = display.newImageRect( “images/thing.png”, 474, 474 )

Good Luck!

burhan

I would call physics.stop() (or physics.pause()) in the exitScene() function.  I would also explicitly remove the collision Listener in exitScene as well.  This means you need to call physics.start() in enterScene as well as add the collision listener there.

Rob

From the code you have posted thus far, i dont see you inserting your objects into storyboards display group anywhere? If you are not doing that, it won’t remove them for you

Oh dear, yes… My group was under the wrong executable. I suck.

How are you removing the bodies when the scene is exited?

storyboard.removeScene() does a: package.loaded[“shapedef4”] = nil for you.

Are your objects being added into the storyboard scene correctly?  Are you pausing/stopping physics before you leave the scene?

Rob

Hi guys, thanks for the reply… I’m adding the body like this:

function scene:createScene( event ) local group = self.view thing = display.newImageRect( "images/thing.png", 474, 474 ) thing.x = display.contentCenterX thing.y = display.contentCenterY physics.addBody( thing, "dynamic", physicsData:get("thing") ) end

And including that in a display group

I’ve tried removing the body AND making it innactive… I stop.physics in my interim scene, but none of those approaches is working… Would anyone fancy taking a look at the code?

Thanks

**Update

It’s not the module that’s causing the ‘slow down’… I’ve just removed it and I’m still getting the same problem. The more I re-enter the scene, the more things slow down. Weird.

FWIW I’ve always found that neither storyboard nor composer do a reliable job of removing objects and variables from memory. Your reference to detrimental performance over time would suggest your physics bodies aren’t being removed. I’d suggest removing your physics bodies manually when the scene exits.

I’ve tried removing everything manually, like:

physics.removeBody( ring4 ) physics.removeBody( ball ) physics.removeBody(sensor1) physics.removeBody(sensor2) physics.removeBody(sensor3) physics.removeBody(sensor4) physics.removeBody(scoreCircle) sensor1:removeEventListener( "collision", sensor1 ) sensor2:removeEventListener( "collision", sensor2 ) sensor3:removeEventListener( "collision", sensor3 ) sensor4:removeEventListener( "collision", sensor4 ) Runtime:removeEventListener("enterFrame", onEnterFrame1) ball:removeEventListener( "collision", ball ) rotateRight:removeEventListener( "touch", turnRight ) scoreCircle:removeEventListener( "collision", scoreCircle )

That’s pretty much everything in the scene… I also call removeScene, in an interim level… The gameover dialogue is triggered by a collision, so I’ve even used timerPerformWithDelay before the exitScene code is executed (just in case it was a collision issue)… No luck.

As you can see, I have a ball in the scene… It has a linear velocity applied to it, it also slows down in a cyclical manner (I’ve tried swapping for linearImpulse with a runtime listener, but still didn’t fix it).

I’m beginning to wonder if it’s a simulator problem. Surely not?

It must be something to do with my destoy scene code… Nightmare.