I need help with "physics.addBody()"

    print("test1")
    timer.performWithDelay(10000, addRB(CurrentObject, r))
    print("test2")
end
.
.
.
function addRB(ObjToChange, radius)
    physics.addBody( ObjToChange, "static", {radius=radius, isSensor = true })
end

I call the addbody on collision but I add performWithDelay like it says we should. It gave these errors ayways:

23:47:14.206 test1
23:47:14.206 ERROR: C:\Users\semih\Documents\Corona Projects\Hope\main.lua:89: physics.addBody() cannot be called when the world is locked and in the middle of number crunching, such as during a collision event
23:47:14.206 test2
23:47:14.206 test1
23:47:14.206 ERROR: C:\Users\semih\Documents\Corona Projects\Hope\main.lua:89: physics.addBody() cannot be called when the world is locked and in the middle of number crunching, such as during a collision event
23:47:14.206 test2
23:47:14.206 test1
23:47:14.206 ERROR: C:\Users\semih\Documents\Corona Projects\Hope\main.lua:89: physics.addBody() cannot be called when the world is locked and in the middle of number crunching, such as during a collision event
23:47:14.206 test2
23:47:14.206 test1
23:47:14.206 ERROR: C:\Users\semih\Documents\Corona Projects\Hope\main.lua:89: physics.addBody() cannot be called when the world is locked and in the middle of number crunching, such as during a collision event
23:47:14.206 test2
.
.
.

Hi!

Two things…

The way timer.performWithDelay works is by passing the function as a variable. The way your passing your function is actually calling it right away, and I can see why you did it that way because you have parameters in your function. In your case, it should be passed inside an anonymous function like so:

timer.performWithDelay(10000, function() addRB(CurrentObject, r) end)

The other issue is, I don’t recall whether you “shouldn’t” or “can’t” do physics body changes inside a collision listener, but you should definitely do it outside the collision listener.

EDIT:
If you’re calling the timer inside your collision listener then should be OK, as long as the object is not colliding 10 seconds later. :slightly_smiling_face:

Thanks a lot!!! It works now :slight_smile: I made the delay 10000 to make it obvious it’s not working, I will be using it as 100 :smiley:

You can also just use timer.performWithDelay( 1, .... ) instead of 100. This just means the function gets called the very next frame. There’s no need to wait a certain amount of time. The only physics engine related limitation is that you can’t destroy a physics body during the collision event itself, so any other time works (and the sooner the better).

Thanks! I didn’t know that

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.