use-cannon icon indicating copy to clipboard operation
use-cannon copied to clipboard

Add body existence check in step function

Open alex-shortt opened this issue 3 years ago • 6 comments
trafficstars

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.

alex-shortt avatar Sep 07 '22 12:09 alex-shortt

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.

alex-shortt avatar Sep 07 '22 12:09 alex-shortt

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.

darkopetrovic avatar Sep 13 '22 13:09 darkopetrovic

yeah i can second this ^ seems to happen when objects are removed from the scene

alex-shortt avatar Sep 15 '22 15:09 alex-shortt

Could you share a reproduction with us then we can be sure to fix it.

bjornstar avatar Sep 19 '22 09:09 bjornstar

@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: https://github.com/pmndrs/use-cannon/blob/9259f8700ef96e0555381afd4ec96e54e3bf507b/packages/cannon-worker-api/src/worker/index.ts#L48-L52

alex-shortt avatar Sep 19 '22 12:09 alex-shortt

now fixed in #412

alex-shortt avatar Sep 21 '22 14:09 alex-shortt

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.

mattblackdev avatar Dec 14 '22 17:12 mattblackdev