Remove/add body to change filter

Hello,

So I have some code that is working fine in some cases, but if I try to trigger it from a colision instead of from timer check or user interaction I get the infernal “ERROR: physics.removeBody() cannot be called when the world is locked and in the middle of number crunching,” error.

I tried adding delays of 5ms, 50ms and 500ms to the funciton call, but it still happens. The function is only being called once (adding a print statement to it shows me this to be true in the console).

I wouldn’t need to remove the body except I have to change the filters (bitmask) of the physics object mid-game so that it can move behind objects it was previously impacting and I haven’t found any way to change filters except when a physics object is created…?

Here are some code snippets:

--called from colision: function changeDepth (event)     timer.performWithDelay(50, event.spriteA.changeDepth(event.spriteB.lhUserCustomInfo.depth),1) end --this is spriteA's changeDepth function: objt.changeDepth = function (newDepth)    if newDepth == ("front") then        objt.curFilter = objt.frontFilter    elseif newDepth == ("back") then        objt.curFilter = objt.backFilter    changeDepth = timer.performWithDelay(500,objt.rebuild(),1) end --this is spriteA's rebuild function: objt.rebuild = function ()         --remove body fails:         physics.removeBody(objt)         --since the remove fails, I can't get this part to work either; presumably the same problem:         --local redCollisionFilter = { categoryBits = 2, maskBits = 2 }         --physics.addBody(objt, { density=objt.myWeight/2, friction=0.2, bounce=objt.myBounce, radius = objt.radiux+2, filter = redCollisionFilter},         --{ density=objt.myWeight/2, friction=0.2, bounce=objt.myBounce, radius = objt.radiux, filter = objt.curFilter}) end  

Any ideas? Driving me crazy and this is clinch-critical for my project.

Thank you!

Oddly the error appears instantly, even if I put in a delay of like 5,000,000ms I sitll get an immedaite error telling me I can’t use physics.removeBody() because it is locked and busy…? Why is it not respecting the timer delay?

Have you tried setting the physics body to isSensor = true and/or moving it off-screen and creating a new object?

Creating new physics bodies on the fly is quite a performance drain, so normally I create ALL of my physics bodies when loading a level, putting the ones I don’t want to interact with “off-screen”, and then triggering their activity at a later time. This keeps the load on the cpu down and allows for the game to no lag. 

Thanks for your help. I was able to get help on IRC and it turns out I was calling my timer funciton with functionName() instead of just: functionName!

Once I removed the parentacies it all works fine now, phew! That was a brain twister.

Oddly the error appears instantly, even if I put in a delay of like 5,000,000ms I sitll get an immedaite error telling me I can’t use physics.removeBody() because it is locked and busy…? Why is it not respecting the timer delay?

Have you tried setting the physics body to isSensor = true and/or moving it off-screen and creating a new object?

Creating new physics bodies on the fly is quite a performance drain, so normally I create ALL of my physics bodies when loading a level, putting the ones I don’t want to interact with “off-screen”, and then triggering their activity at a later time. This keeps the load on the cpu down and allows for the game to no lag. 

Thanks for your help. I was able to get help on IRC and it turns out I was calling my timer funciton with functionName() instead of just: functionName!

Once I removed the parentacies it all works fine now, phew! That was a brain twister.