Add body existence check in step function
alex-shortt opened this issue · 7 comments
Hello - something about my usage seems to be wrong and I'd love some help figuring that out, but an issue regardless of my setup is the error Cannot read properties of undefined (reading 'position')
coming from this line. For some reason my body subscriptions are losing values but not disappearing from state. There should definitely be a check there . I'll submit a pr in a second.
As for what may be wrong with my setup, I'm using the Physics
component in a separate component, seen here, which upon re-render may be losing internal state.
Submitted a pr here: #408
As for the cause, after looking at the source I can better describe it as unloaded bodies don't seem to remove subscriptions. So it's probably not the Physics
component itself that's resetting since it's still aware of the subscriptions, but that the body is being unloaded without the subscriptions being unloaded.
I've the same error when using api subscription and when objects are removed from the scene. Don't know how but then the api stops working for the other objects too.
yeah i can second this ^ seems to happen when objects are removed from the scene
Could you share a reproduction with us then we can be sure to fix it.
@bjornstar the problem seems clear, as pointed out here, subscriptions aren't being removed when the body is removed. am i missing something?
this is my fix in #411:
use-cannon/packages/cannon-worker-api/src/worker/index.ts
Lines 48 to 52 in 9259f87
now fixed in #412
I experienced this when I setup a subscription but did not handle the returned unsubscribe
function from subscribe
.
So kids, eat your veggies and don't forget to unsub
useEffect(() => {
const unsub = api.subscribe(...)
return unsub
}, [api])
so it gets called when your component unmounts.